gpt4 book ai didi

websocket - Firefox 上的 Socket.io 为 "websocket was interrupted while page is loading"

转载 作者:行者123 更新时间:2023-12-02 19:24:50 25 4
gpt4 key购买 nike

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>

最佳答案

发生这种情况是因为您没有关闭打开的 websocket。

此代码将消除此错误:

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

关于websocket - Firefox 上的 Socket.io 为 "websocket was interrupted while page is loading",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14140414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com