gpt4 book ai didi

javascript - 无法连接到套接字 io 服务器

转载 作者:行者123 更新时间:2023-11-29 23:21:05 24 4
gpt4 key购买 nike

我有 bot 套接字 IO 客户端和服务器正在运行。我想让客户端与服务器通信。

服务器初始化

this.http_server = net.createServer();
// this didn't work too. Exxentially socket io does not work at all
// this.io = io.listen(typeof port == "number" ? port : process.env.PORT);
console.log("[ManagementServer] Attaching socket IO to HTTP server.");
this.io = io(this.http_server);
/// The port does open
this.http_server.listen(typeof port == "number" ? port : process.env.PORT,
() => {
console.log('[ManagementServer] IO HTTP server listening on port', this.http_server.address().port)
});
/// The connection listener is triggered, but IO doesn't bother
/// replying or something
this.http_server.on("connection", (req, res) => {
console.log("[ManagementServer] Connection received, but IO ignores it.")
});
// Never happens
this.io.on("connection", (socket) => {
this.connection(socket);
})

客户端初始化

const srvurl = "http://" + this.remoteAddr + ":" + this.remotePort;

console.log("[UDP_socketio] Connecting to server: ", srvurl)
this.client = socketioclient(srvurl);

this.client.once("connect", () => {
console.log("[UDP_socketio] Connected to remote server, identificating.")
this.sendIdentification();
});

this.client.on("error", () => {
console.error("[UDP_socketio] Socket IO connection error.")
});

套接字IO调试输出

我启用了调试,但是套接字 IO 调试输出无论如何都不是很有用:

[ManagementServer] Attaching socket IO to HTTP server.
socket.io:server initializing namespace / +0ms
socket.io-parser encoding packet {"type":0,"nsp":"/"} +0ms
socket.io-parser encoded {"type":0,"nsp":"/"} as 0 +1ms
socket.io:server creating engine.io instance with opts {"serveClient":false,"path":"/","initialPacket":["0"]} +3ms
[ManagementServer] IO HTTP server listening on port 80
[UDP_socketio] Connecting to server: http://127.0.0.1:80
socket.io-client:url parse http://127.0.0.1:80 +0ms
socket.io-client new io instance for http://127.0.0.1:80 +0ms
socket.io-client:manager readyState closed +0ms
socket.io-client:manager opening http://127.0.0.1:80 +1ms
engine.io-client:socket creating transport "polling" +0ms
engine.io-client:polling polling +0ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: http://127.0.0.1/?EIO=3&transport=polling&t=MEuBb8c&b64=1 +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +10ms
socket.io-client:manager connect attempt will timeout after 20000 +12ms
socket.io-client:manager readyState opening +1ms
[ManagementServer] Connection received, but IO ignores it.
socket.io-client:manager connect attempt timed out after 20000 +20s
engine.io-client:socket socket close with reason: "forced close" +20s
engine.io-client:polling transport not open - deferring close +20s
engine.io-client:socket socket closing - telling transport to close +0ms
socket.io-client:manager connect_error +2ms
socket.io-client:manager cleanup +0ms
socket.io-client:manager will wait 1178ms before reconnect attempt +1ms
socket.io-client:manager attempting reconnect +1s
socket.io-client:manager readyState closed +0ms
socket.io-client:manager opening http://127.0.0.1:80 +1ms

请注意,我添加的连接监听器会触发,但套接字 IO 什么都不做。

目前没有什么帮助:

  • this.http_server 中删除 connection 监听器>
  • 更改用于测试的端口
  • 增加客户端尝试连接的延迟
  • 阅读 socket.io 文档。它令人困惑、不完整、不准确且前后矛盾。

是什么让套接字 IO 像这样忽略所有连接?也许服务器正在监听来自不同路径的请求?如果是这样,如何查看IO监听在什么路径?

最佳答案

socket.io 可与 http/s 服务器配合使用,例如 express(第 7 层)。

net 是一个 TCP 服务器(第 4 层)。

所以他们不兼容。

关于javascript - 无法连接到套接字 io 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50633363/

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