“页面加载时websocket被中断"在 Firefox for Socket.io 上 [英] "websocket was interrupted while page is loading" on Firefox for Socket.io

查看:79
本文介绍了“页面加载时websocket被中断"在 Firefox for Socket.io 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的新手,我试图搜索这个,但我没有得到答案.

I am new to socket.io and I have tried to search for this, but I didn't get an answer.

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

Websocket is interrupted when I refresh page on Firefox. That's why server side is waiting to authorise client.

代码如下: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>

推荐答案

这是因为,您没有关闭打开的 websocket.

It happens because, you are not closing your open websocket.

此代码将消除此错误:

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

这篇关于“页面加载时websocket被中断"在 Firefox for Socket.io 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆