【发布时间】:2012-12-17 21:41:59
【问题描述】:
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>
【问题讨论】:
-
您需要包含导致此问题的代码。否则没有意义。
-
ethrbunny 我已添加代码检查它请...帮帮我...
-
面临几乎类似的问题。请帮忙。 stackoverflow.com/questions/16880454/…