我在 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
此时,事情将停止大约 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
在此之后,据我所知,事情开始运作良好,并且事件被无延迟地触发和接收(如果在上述 5 秒延迟之前触发事件,则在延迟完成之前不会处理它们)。问题是如何消除 5 秒的延迟。
客户端 JS 如下所示:
window.onload = function () {
var socket = io.connect("http://xxx.us-west-2.compute.amazonaws.com", {resource: "/socket.io"});
服务端 JS 如下所示:
var express = require('express');
var app = express();
var io = require("socket.io").listen(app.listen(3000));
我的 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>
如何避免初始延迟并建立与 socket.io 的健康连接?