简体   繁体   English

Socket.IO无法与Express一起使用

[英]Socket.IO won't work with Express

I'm using Socket.io 1.3.6 and express 4.13.1. 我正在使用Socket.io 1.3.6和Express 4.13.1。

I followed the example for express 3/4 on socket.io website: http://socket.io/docs/#using-with-express-3/4 我在socket.io网站上遵循了Express 3/4的示例: http ://socket.io/docs/#using-with-express-3/4

On server side: 在服务器端:

 18 /**
 19  * Create HTTP server.
 20  */
 21 
 22 var server = http.createServer(app);
 23 var io = require('socket.io')(server);
 24 io.on('connection', function(socket) {
 25   console.log(JSON.stringify(socket));
 26 });
 27 
 28 
 29 /**
 30  * Listen on provided port, on all network interfaces.
 31  */
 32 
 33 server.listen(port);

On client side: 在客户端:

 42   socket = io.connect(ChartServiceFrontConfig.socketHost, ChartServiceFrontConfig.socketPort);
 43 
 44   socket.on('data', function(data) {
 45     // render data
 46     var chart = charts[data.name];
 47     if (chart !== undefined)
 48       chart.series = data.series;
 49   });

Express just says the following 404 message: Express仅显示以下404消息:

GET /engine.io/?EIO=3&transport=polling&t=1440667610196-0 404 2.172 ms - 925
GET /engine.io/?EIO=3&transport=polling&t=1440667611046-1 404 2.627 ms - 925
GET /engine.io/?EIO=3&transport=polling&t=1440667612854-2 404 6.672 ms - 925
GET /engine.io/?EIO=3&transport=polling&t=1440667615955-3 404 6.362 ms - 925
GET /engine.io/?EIO=3&transport=polling&t=1440667620537-4 404 2.340 ms - 925

It looks Express does get that request, but socket.io doesn't interpret requests from http server. 看起来Express确实收到了该请求,但是socket.io不会解释来自http服务器的请求。 I tried the following code with no luck...: 我没有运气就尝试了以下代码...:

var io = require('socket.io').listen(server);

I searched around but there's no answer worked for me. 我四处搜寻,但没有答案适合我。 So I'm stuck here. 所以我被困在这里。 Any comments? 任何意见?

The problem is solved by changing the client side line #42 in my question. 这个问题是通过更改我的问题中的客户端第42行来解决的。 Socket.IO client doesn't accept 2 parameters, so just use: Socket.IO客户端不接受2个参数,因此只需使用:

socket = io.connect(ChartServiceFrontConfig.socketHost
  + ':'
  + ChartServiceFrontConfig.socketPort);

Now socket.io connects. 现在,socket.io已连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM