通过 xhr-polling 和 jsonp-polling 超时连接

Connecting through xhr-polling and jsonp-polling timeout

我尝试 运行通过 websocket 和轮询连接我的 socket.io 程序,它们都有效。但是,当尝试通过 xhr-polling 运行 时,超时。这可能是什么原因造成的?

对于这个程序,我使用 socket.io 1.2.1.

var options = {"force new connection":true,
               "reconnect":false,
               "connect timeout":10000,
               "flash policy port":843,
               "auto connect":true,
               "path":"/sample/socket.io",
               "transports":["xhr-polling"]}

this.namespace = io.connect( 'http://localhost:8190/', options);

到目前为止,我的研究对这个问题一无所知。

编辑:

这是浏览器上的一段日志。

socket.io-client:url parse http://localhost:8190/ +0ms
socket.io.js:1284 socket.io-client ignoring socket cache for http://localhost:8190/ +0ms
socket.io.js:1284 socket.io-client:manager readyState closed +0ms
socket.io.js:1284 socket.io-client:manager opening http://localhost:8190/ +0ms
socket.io.js:3524 engine.io-client:socket creating transport "xhr-polling" +0ms
socket.io.js:1284 socket.io-client:manager connect attempt will timeout after 20000 +4ms
socket.io.js:1284 socket.io-client:manager readyState opening +1ms
socket.io.js:1284 socket.io-client:manager connect_error +3ms
socket.io.js:1284 socket.io-client:manager will wait 1000ms before reconnect attempt +2ms
socket.io.js:1284 socket.io-client:manager attempting reconnect +1s
socket.io.js:1284 socket.io-client:manager readyState closed +0ms
socket.io.js:1284 socket.io-client:manager opening http://localhost:8190/ +1ms
socket.io.js:1284 socket.io-client:manager connect attempt will timeout after 20000 +0ms

这段日志不断重复,同时不断增加超时。

我发现这是因为 Socket.IO 版本 1.0+ 实际上不允许设置 [websocket and polling] 之外的传输。将轮询设置为您的传输默认为 polling-xhr,除非您在选项中将属性 forceJSONP and/or jsonp 设置为 true

换句话说,代码应该是:

var options = {"force new connection":true,
               "reconnect":false,
               "connect timeout":10000,
               "flash policy port":843,
               "auto connect":true,
               "path":"/sample/socket.io",
               "transports":["polling"]}

this.namespace = io.connect( 'http://localhost:8190/', options);

还要注意 'auto connect' 现在是 autoConnect'force new connection' 现在是 forceNew'connect timeout' 现在是 timeout'reconnect' 现在是 'reconnection'.

var options = {forceNew :true,
               reconnection :false,
               timeout":10000,
               "flash policy port":843,
               autoConnect:true,
               "path":"/sample/socket.io",
               "transports":["polling"]}