[英]Socket.io is not connecting Node.js server to React.js client
[英]Node.js and Socket.io moving from local development server to live server, not connecting
我已經在本地開發服務器上使用Node.js和Socket.io構建了一個私人聊天系統,它可以正常工作,發送消息,每當客戶端接受消息事件時更新數據庫。 但是,過去兩天我一直試圖將其推送到我的實時Rackspace服務器中。 我一直在和Rackspace聊天,他們啟動了我的節點服務器,我有一個console.log打印“服務器正在偵聽端口:xxxx”。 我將發布我的node.js服務器代碼以及一些客戶端代碼。 我只是不確定哪里出了問題,因為它對我的本地開發工作正常。 Chrome控制台給我的錯誤是“未定義io”,這使我認為它沒有從節點服務器加載socket.io/socket.io.js。 但是,我認為這樣會使GET請求出錯。
我是Node.js和Socket.io的新手,並不了解它的所有內容。 我正在嘗試在vhost主機上使用它,這可能會引起問題嗎?
var express = require('express');
app = require('http').createServer(handler),
//server = require('http').createServer(app),
io = require('socket.io')(app);
fs = require('fs');
app.listen(3000, function() {
console.log("Server listening on: 3000");
});
var clients = {};
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
//app.get('/', function(req, res){
// res.sendFile(__dirname + '/index.html');
//});
io.sockets.on('connection', function(socket){
console.log("Connected!");
socket.emit('news', { hello: 'world' });
socket.on('add-user', function(data){
clients[data.username] = {"socket" : socket.id};
console.log(data.username+' / '+clients[data.username].socket);
});
socket.on('private-message', function(data){
console.log("Sending: " + data.content + " to " + data.username);
if (clients[data.username]){
io.sockets.connected[clients[data.username].socket].emit("add- message", data);
} else {
console.log("User does not exist: " + data.username);
}
});
//Removing the socket on disconnect
socket.on('disconnect', function() {
for(var name in clients) {
if(clients[name].socket === socket.id) {
delete clients[name];
break;
}
}
});
});
客戶代碼:
var socket = io;
socket.connect('x.x.x.x:3000');
var viewingUser = $('#viewingUserID').val();
socket.on("news", function (data) {
console.log(data);
});
socket.emit("add-user", {"username" : viewingUser });
So I replaced "<script type="text/javascript" src="xx.xxx.xxx.xxx:3000/socket.io/socket.io.js">" with "<script type="text/javascript" src="https://cdn.socket.io/socket.io-1.3.5.js"></script>"
這是錯誤:
GET http://23.253.247.166:3000/socket.io/?EIO=3&transport=polling&t=1439565622045-29 net::ERR_CONNECTION_REFUSED
您的服務器沒有針對socket.io javascript文件的請求處理程序。
您瀏覽到您的網站,並且服務器收到請求,然后通過發送index.html文件來處理該請求。
當瀏覽器正在處理index.html文件時,它將找到socket.io腳本標簽並向服務器發出請求,但是您只能通過發送index.html文件來響應請求。
要么:
要么
如果您必須提供靜態內容(例如網頁/ javascript / css /圖片/...),express會非常容易:
app.use(express.static('public'));
public
將是您的靜態資產的文件夾。
服務器代碼:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
// Socket stuff goes here. but try this for a start
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('Server listening on:3000');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.