接收错误"连接到ws:// localhost:1337 / socket.io / 1 / websocket"在页面加载时被打断了#34;

时间:2015-01-07 21:57:28

标签: socket.io

接收错误“连接到ws:// localhost:1337 / socket.io / 1 / websocket”在页面加载时被中断“如何解决此问题?我尝试关闭套接字连接,然后再打开新的套接字连接,但我仍然收到此错误。请告知。

1 个答案:

答案 0 :(得分:0)

您始终可以在加载前关闭连接,

$(window).on('beforeunload', function(){
    socket.close();
});

你可以订阅onclose的websocket来处理javascript中的错误,如下所示:

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);
        };
        socket.onclose = function() {
            console.info( 'Socket is now closed.' );
        }
    } catch (e) {
        console.error('Sorry, the web socket at "%s" is un-available', url);
    }

setTimeout(function(){
    socket.send("Hello World");
}, 1000);

小提琴:http://jsfiddle.net/w5aAK/1/