相关疑难解决方法(0)

在客户端定义传输类型

我需要对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); 
    }
Run Code Online (Sandbox Code Playgroud)

我在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"],这是对的.但是,在

https://github.com/LearnBoost/socket.io-client/blob/master/dist/socket.io.js#L1679

我不知道为什么传输会改变["htmlfile", "jsonp- polling", "xhr-polling"],它与我在服务器端定义的顺序相同.

为什么不使用之前的选项?

cross-browser node.js socket.io

11
推荐指数
1
解决办法
1万
查看次数

如何捕获WebSocket连接中断?

在Firefox(至少)中,如果你按ESC,它将关闭所有打开的WebSockets连接.我需要捕获断开连接并尝试重新连接一旦它再次可用.

这是我试图实现的代码的一个示例,但是我能想出的任何内容都不会捕获错误并允许我优雅地处理它.

看看代码:http://jsfiddle.net/w5aAK/

var url = "ws://echo.websocket.org";
    try {
        socket = window['MozWebSocket'] ? new MozWebSocket(url) : new WebSocket(url);
        socket.onopen = function(){
            console.log('Socket is now open.');
        };
        socket.onerror = function (error) {
            console.error('There was an un-identified Web Socket error');
        };
        socket.onmessage = function (message) {
            console.info("Message: %o", message.data);
        };
    } catch (e) {
        console.error('Sorry, the web socket at "%s" is un-available', url);
    }

setTimeout(function(){
    socket.send("Hello World");
}, 1000);
Run Code Online (Sandbox Code Playgroud)

打开控制台并观察输出.

我在这里做错了,还是因为连接在JS脚本范围之外运行而不可能?

任何输入都会有所帮助.

谢谢!

javascript websocket

11
推荐指数
1
解决办法
2万
查看次数