gpt4 book ai didi

javascript - 在 socket.io 中从客户端到服务器的连接失败

转载 作者:搜寻专家 更新时间:2023-11-01 00:25:54 25 4
gpt4 key购买 nike

我是 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']);

关于javascript - 在 socket.io 中从客户端到服务器的连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730369/

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