gpt4 book ai didi

javascript - Socket.io 问题 : initializing a ton of clients using 'xhr-polling'

转载 作者:行者123 更新时间:2023-12-02 20:28:09 27 4
gpt4 key购买 nike

我正在尝试设置 socket.io,但我 90% 确定遇到了问题。我正在运行我的服务器,它说它已连接,然后我在 2-3 秒后收到一堆新客户端。这是我的终端输出:

21 Dec 17:36:53 - socket.io ready - accepting connections
21 Dec 17:37:13 - Initializing client with transport "websocket"
21 Dec 17:37:13 - Client 4786583543755114 connected
21 Dec 17:37:18 - Client 4786583543755114 disconnected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 377916906028986 connected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 3885312571655959 connected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 38271573395468295 connected

每 1-2 秒就会有更多。我正在使用 safari,它支持 websockets,这从第一个响应中就可以看出。

这是我的服务器代码:

server = http.createServer(function(req, res){ 
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});

var path = url.parse(req.url).pathname;

// console.log(__dirname + path);
fs.readFile(__dirname + path, function(error, data) {
res.end(data);
});


});

server.listen(80);

// socket.io, I choose you
var socket = io.listen(server);

socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ });
client.on('disconnect', function(){ });
});

..以及客户端代码:

    <script> 

var socket = new io.Socket();
socket.connect();

socket.on('message', function(obj){
alert('got some data ' + obj);
});

socket.on('connect', function() {
console.log("We've connected!");
socket.send('some data');
})


</script>

任何线索或帮助都会很棒。谢谢!

最佳答案

事实证明,这是一个非常小的修复,却带来了巨大的不同 - 不要忘记添加 HTML5 文档类型 <!doctype html>到您的客户的顶部!

关于javascript - Socket.io 问题 : initializing a ton of clients using 'xhr-polling' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4505012/

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