延迟socket.io响应,并"警告 - websocket连接无效"

Win*_*mes 1 httpd.conf node.js socket.io

我在AWS EC2实例上运行nodeJS + Express + socket.io + httpd应用程序.

加载页面后,记录器提供以下信息:

GET / 200 2ms - 1.23kb
GET /javascripts/script.js 304 1ms
   debug - served static content /socket.io.js
GET /stylesheets/style.css 304 1ms
GET /stylesheets/style.css 304 1ms
GET /images/052.png 304 1ms
GET /Candara.ttf 304 1ms
   debug - client authorized
   info  - handshake authorized EgWLPeyO2uiCTSui01CP
   debug - setting request GET /socket.io/1/websocket/EgWLPeyO2uiCTSui01CP
   debug - set heartbeat interval for client EgWLPeyO2uiCTSui01CP
   warn  - websocket connection invalid
   info  - transport end (undefined)
   debug - set close timeout for client EgWLPeyO2uiCTSui01CP
   debug - cleared close timeout for client EgWLPeyO2uiCTSui01CP
   debug - cleared heartbeat interval for client EgWLPeyO2uiCTSui01CP
Run Code Online (Sandbox Code Playgroud)

此时,事情将停止约5秒钟,之后:

   debug - setting request GET /socket.io/1/xhr-polling/EgWLPeyO2uiCTSui01CP?t=1376937523124
   debug - setting poll timeout
   debug - client authorized for 
   debug - clearing poll timeout
   debug - xhr-polling writing 1::
   debug - set close timeout for client EgWLPeyO2uiCTSui01CP
   debug - setting request GET /socket.io/1/xhr-polling/EgWLPeyO2uiCTSui01CP?t=1376937523490
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client EgWLPeyO2uiCTSui01CP
Run Code Online (Sandbox Code Playgroud)

在此之后,就我所知,事情开始运作良好,并且事件被触发并且没有延迟地接收(如果事件上述5秒延迟之前被触发,则在延迟完成之后它们将不被处理).问题是如何消除5秒的延迟.

客户端JS看起来像这样:

window.onload = function () {
  var socket = io.connect("http://xxx.us-west-2.compute.amazonaws.com", {resource: "/socket.io"});
Run Code Online (Sandbox Code Playgroud)

服务器端JS看起来像这样:

var express = require('express');
var app = express();
var io = require("socket.io").listen(app.listen(3000));
Run Code Online (Sandbox Code Playgroud)

我的httpd.conf文件如下所示:

ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/

<Proxy *>
    Order deny,allow
    Allow from all
</Proxy>

<Location />
    allow from all
</Location>
Run Code Online (Sandbox Code Playgroud)

如何避免初始延迟并建立与socket.io的健康连接?

Win*_*mes 5

我通过将允许的传输方法限制为"xhr-polling"和"jsonp-polling"来解决问题.我相信这是因为Apache/2.2.25不支持其他人.

服务器端添加了相关代码:

io.set("transports", ["xhr-polling", "jsonp-polling"]);
Run Code Online (Sandbox Code Playgroud)

  • 当我看到这个被发布时,我只是评论:).这会起作用,但是你失去了websockets的好处.您还需要确保,如果/当您开始运行多个服务器时,您的负载均衡器配置为粘性会话,或者您持久保存会话(redis.io或其他),以便可以在任何服务器上进行请求. (4认同)