对于IE,我需要使用jsonp-polling,对于Firefox,则需要使用xhr-polling,因此我尝试在客户端定义传输类型,如下所示:
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits); var socket = io.connect(VG.NODE_SERVER_URL,{ transports:['xhr-polling'] }); } else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ var socket = io.connect(VG.NODE_SERVER_URL,{ transports:['jsonp-polling'] }); } else { var socket = io.connect(VG.NODE_SERVER_URL); }
我在Firefox上进行了测试,并在socket.io-client lib上添加了日志记录。在
https://github.com/LearnBoost/socket.io- client/blob/master/dist/socket.io.js#L1509
option.transports是["xhr-polling", "flashsocket", "htmlfile", "xhr-polling", "jsonp-polling"],这是正确的。但是,在
["xhr-polling", "flashsocket", "htmlfile", "xhr-polling", "jsonp-polling"]
https://github.com/LearnBoost/socket.io- client/blob/master/dist/socket.io.js#L1679
我不知道为什么将传输更改为["htmlfile", "jsonp- polling", "xhr- polling"],其顺序与我在服务器端定义的顺序相同。
["htmlfile", "jsonp- polling", "xhr- polling"]
为什么不使用上一个选项?
该错误现已在socket.io 0.9.6版中修复,我可以使用它,并且可以正常工作:
socket = io.connect(HOST_REMOTE, { transports: ['xhr-polling'] });
在1.0.0及更高版本中:
socket = io.connect(HOST_REMOTE, { transports: ['polling'] });