Ama*_*mar 49 websocket 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
Run Code Online (Sandbox Code Playgroud)
我是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);
});
});
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
小智 47
它发生的原因是,你没有关闭你的开放websocket.
此代码将删除此错误:
$(window).on('beforeunload', function(){
socket.close();
});
Run Code Online (Sandbox Code Playgroud)
use*_*062 13
这似乎是Firefox中的一个漏洞(截至2015-03-29):
https://bugzilla.mozilla.org/show_bug.cgi?id=712329
正如亚历山大指出的那样,解决方法(现在)是在beforeunload上调用websocket上的close().
更新2016-04:根据Bugzilla的说法,这将在Firefox 48中修复
这对您的申请有什么影响?我的猜测是,在控制台中看到错误并不好。
这里的问题是,您看到 Firefox 登录时出现此错误,但您对此无能为力。无法使用try...catch块或通过websocket.onerror/捕获此错误websocket.onclose。
有关的:
| 归档时间: |
|
| 查看次数: |
49379 次 |
| 最近记录: |