从客户端到socket.io中的服务器的连接失败
Connection Failed from Client to Server in socket.io
我是node js和socket.io的新手,我正在我的windows机器中尝试socket.io。
服务器代码
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
客户端代码
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8080/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
socket.on('connect', function () {
alert('connect');
});
socket.on('error', function (data) {
console.log(data || 'error');
});
socket.on('connect_failed', function (data) {
console.log(data || 'connect_failed');
});
</script>
在上面的脚本中,客户端无法连接到服务器(在控制台中记录了connect_failed),但同时服务器端显示了以下内容,
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized ALB0omsm3E2ZvPMn02x7
debug - setting request GET /socket.io/1/websocket/ALB0omsm3E2ZvPMn02x7
debug - set heartbeat interval for client ALB0omsm3E2ZvPMn02x7
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"news","args":[{"hello":"world"}]}
debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168523063
debug - setting poll timeout
debug - discarding transport
debug - cleared heartbeat interval for client ALB0omsm3E2ZvPMn02x7
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168533064&i=0
debug - setting poll timeout
debug - discarding transport
debug - clearing poll timeout
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168556555&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168576586&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168596600&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168616640&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - jsonppolling writing io.j[0]("8::");
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - jsonppolling closed due to exceeded duration
debug - setting request GET /socket.io/1/jsonp-polling/ALB0omsm3E2ZvPMn02x7?t
=1374168636656&i=0
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168523063
debug - setting poll timeout
debug - discarding transport
debug - clearing poll timeout
debug - clearing poll timeout
debug - xhr-polling writing 8::
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - xhr-polling closed due to exceeded duration
debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168663072
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - xhr-polling writing 8::
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - xhr-polling closed due to exceeded duration
debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168690890
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
debug - clearing poll timeout
debug - xhr-polling writing 8::
debug - set close timeout for client ALB0omsm3E2ZvPMn02x7
debug - xhr-polling closed due to exceeded duration
debug - setting request GET /socket.io/1/xhr-polling/ALB0omsm3E2ZvPMn02x7?t=1
374168710895
debug - setting poll timeout
debug - discarding transport
debug - cleared close timeout for client ALB0omsm3E2ZvPMn02x7
如何修复上述脚本中的错误,以成功运行socket.io的基本示例?
您可以根据需要配置socket.io设置。https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
我也遇到了同样的问题,发现添加下面的行可以解决的问题
io.set('transports',['xhr-polling']);
使用以下数据创建一个localhost.js文件:
var port = 80,
http = require('http'),
fs = require('fs'),
socket = require('socket.io'),
app = function (req, res) {
var url = req.url == '/' ? '/index.html' : req.url;
fs.readFile(__dirname + url, 'utf8', function (err, data) {
if (err) return console.log(err);
res.end(data);
});
},
httpServer = http.createServer(app).listen(port),
io = socket.listen(httpServer);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
和一个index.html文件,其中包含:
<html>
<head>
<script src="socket.min.js"></script>
</head>
<body>
<script>
var socket = io.connect('ws://localhost:80/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
socket.on('connect', function () {
console.log('connect');
});
socket.on('error', function (data) {
console.log(data || 'error');
});
</script>
</body>
</html>
将socket.min.js放在同一文件夹中,然后在cmd:中运行
node localhost.js
相关文章:
- 服务器上的Meteor客户端断开连接事件
- 可以't连接客户端和服务器import.io
- SignalR连接到多个服务器
- Javascript/web-dev:为什么你需要一个服务器来连接硬件
- 将服务器托管的Web应用程序连接到本地Web套接字
- 在 Joomla 3.3.6 中,单击会导致服务器连接重置
- 如何将sql服务器连接到我的Javascript游戏
- Socket.IO:重新连接导致服务器连接代码运行两次
- jQuery mobile:在页面导航期间,当建立服务器连接时,下一个页面的样式将应用于当前页面
- C/SSL/JQuery.ajax() 客户端 - >服务器连接重置,但发送了 1 个字节
- 将深流服务器连接到重新思考数据库实例
- 如何从节点.js Web 服务器连接到数据库服务器
- 用于 PhoneGap 的 JavaScript 代码的 XMPP 服务器连接
- 从不同服务器连接socket.io时出错
- webrtc通过websocket客户端/服务器连接问题
- 将javascript与本地主机服务器连接,但页面由其他服务器提供
- 关于构建服务器连接android应用程序的建议
- Nodejs服务器连接
- 当内联javascript执行时,web浏览器是否保持web服务器连接?
- 如何以可维护的方式使用React和Redux thunk处理服务器/连接异常