火狐浏览器中 Socket.io 报错:"页面加载时 WebSocket 被中断"

63
Error: The connection to <websocket> was interrupted while the page was loading.
Source File: localhost/socket.io/node_modules/socket.io-client/dist/socket.io.js
Line: 2371

我是一个有用的助手,可以翻译文本。

我是socket.io的新手,尝试搜索了一下,但没有得到答案。

当我在Firefox上刷新页面时,Websocket会中断。这就是为什么服务器端正在等待授权客户端。

这是代码:server.js

var app = require('http').createServer(handler),
  io = require('socket.io').listen(app),
  fs = require('fs')

app.listen(8080);

function handler(req, res) {
  fs.readFile(__dirname + '/index.html',
    function (err, data) {
      if (err) {
        res.writeHead(500);
        return res.end('Error loading index.html');
      }
      res.writeHead(200);
      res.end(data);
    });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', {
    hello: 'world'
  });
  socket.on('my other event', function (data) {
    //alert(JSON.stringify(data));  
    console.log(data);
  });

});

index.html

<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:8080');

  socket.on('news', function (data) {
    alert(JSON.stringify(data));
    console.log(data);
    socket.emit('my next event', { my: 'data' });
  });

</script>

1
你需要包含导致这个问题的代码,否则这句话就毫无意义。 - ethrbunny
ethrbunny,我已经添加了代码,请检查一下...帮帮我... - Amar
遇到了几乎相同的问题,请帮忙。 http://stackoverflow.com/questions/16880454/http-localhost8080-not-redirecting-to-opensso-login-page - Sayan
6个回答

50
因为您没有关闭已打开的WebSocket连接,所以会发生这种情况。
以下代码将解决此错误:
$(window).on('beforeunload', function(){
    socket.close();
});

我在 Chrome 中也遇到了这个问题。这似乎已经解决了它。 - theblang
3
实际上,我认为这个解决方法没有起作用。我还尝试了这个,但我也不认为这个解决方法有效。 - theblang
4
这对我没有解决问题。 Firefox <48中的bug是问题所在,像这样的解决方法在我的Firefox版本(41)上不起作用。 升级可以解决问题; 但不幸的是,如果您正在编写Web应用程序(我猜这就是我们大多数人对socket.io感兴趣的原因),您不能决定用户使用哪个版本的Firefox(或其他浏览器)。 :( - liminalisht
2
直到我发现服务器没有响应WebSocket的“close”消息,问题才得以解决。现在服务器已经很好地确认了关闭,并且Firefox(60.0)也很满意。 - Jan Wielemaker
2
我在2020年仍然遇到这个问题,但只出现在wss://上。 - Anton
@JanWielemaker:谢谢!我们的自定义基于Undertow的Web服务器多年来一直存在这个问题。我检查了我的套接字关闭处理程序代码(onFullCloseMessage),将其更新为调用AbstractReceiveListener中的超级方法,现在WebSocket可以干净地关闭。 - Lawrence Dol

17

这似乎是Firefox中的一个已知bug(截至2015-03-29):

https://bugzilla.mozilla.org/show_bug.cgi?id=712329

目前的解决方法是,在beforeunload事件上调用websocket的close()函数,正如Alexander所指出的那样。

更新 2016-04:根据Bugzilla的消息,这个问题将在Firefox 48中得到修复。


4
这个解决方案在使用最新版本的socket.io(1.3.5)时无效。 - Lukas Liesis
@LukasLiesis 我认为这与套接字服务器无关。我正在使用 PHP WebSockets 服务器。 - Victor
@Victor socket.io 在服务器和客户端两侧都被使用。 - Lukas Liesis
@LukasLiesis,但这仍然与socket.io产品本身无关。 - Victor
升级Firefox是唯一对我有效的方法。谢谢你。在Firefox 41上,“解决方法”对我没有用。 - liminalisht

4

我刚刚在学习Socket.IO的教程时遇到了这个确切的问题。我尝试了已发布的解决方案,但它们似乎根本不起作用。

经过一些摆弄、一些尖叫和一些橡皮鸭子的思考,我终于找到了问题所在。问题在于在socket变量正确初始化之前,它正在尝试连接到socket。JavaScript犯错#1。

如果您将文件修改为包括jQuery,并像这样包装函数:

    <script src="/socket.io/socket.io.js"></script>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
 <script>
    $(function(){
      var socket = io.connect('http://localhost:8080');

      socket.on('news', function (data) {
        alert(JSON.stringify(data));
        console.log(data);
        socket.emit('my next event', { my: 'data' });
      }); 
    });
    </script>

你会取得更大的成功。


这对我来说没有任何帮助。相同的错误消息仍然记录在控制台中。 - ThisClark

1

在服务器端,客户端未被授权,因此客户端不会传输任何数据。但是,在Internet Explorer 9中,它被授权并且还能传输数据,因为IE使用了xhr-polling。 - Amar
根据你的问题,似乎你想知道为什么会出现中断错误。如果不是,请更新你的问题。 - leggetter

1

多年来,我们基于Undertow的自定义Web服务器一直存在这个问题——我的问题是我的服务器没有响应套接字关闭消息。

根据Jan Wielemaker的评论,我检查了AbstractReceiveListener.onFullCloseMessage的套接字关闭处理程序代码,并意识到我没有调用超级方法。添加super.close()后,客户端可以正常关闭套接字,而不会发出任何错误。


-7
一个解决方案是在断开事件上设置超时。
         setTimeout(() => {
            $('#offlineModal').modal('show')
          }, 5000)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接