【问题标题】:Receiving error "connection to ws://localhost:1337/socket.io/1/websocket" was interrupted while the page was loading"接收错误“与 ws://localhost:1337/socket.io/1/websocket 的连接”在页面加载时被中断”
【发布时间】:2015-01-08 06:01:37
【问题描述】:

在页面加载时收到错误“与 ws://localhost:1337/socket.io/1/websocket 的连接”被中断”我该如何解决这个问题?我试图在打开这个新的之前关闭套接字连接套接字连接,但是我仍然收到此错误。请建议。

【问题讨论】:

    标签: socket.io


    【解决方案1】:

    你总是可以在加载前关闭连接,

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

    您可以订阅 websocket 的 onclose 来处理 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/

    【讨论】:

    • 感谢您的回答。这让我通过了我遇到的问题。现在我有一个不同的问题,表单是 xhr-polling 并且已经 xhr-polling 一段时间了。 xhr-polling 何时停止并将我重定向到我需要去的地方。 xhr-polling 找不到打开的套接字后连接超时。
    猜你喜欢
    • 2013-03-05
    • 1970-01-01
    • 2014-08-10
    • 2021-09-16
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    相关资源
    最近更新 更多