我尝试通过websocket和polling运行我的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
这段日志不断重复,同时稳定地增加超时时间。
答案 0 :(得分:3)
我发现这是因为Socket.IO版本1.0+实际上不允许在[websocket and polling]
之外设置传输。将轮询设置为您的传输默认为polling-xhr,除非您在选项中将属性forceJSONP
和/或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"]}