无法连接到socket io服务器

时间:2018-05-31 21:57:02

标签: javascript node.js websocket socket.io

我有bot socket 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不执行任何操作。

到目前为止没有任何帮助:

  • connection
  • 中删除this.http_server侦听器
  • 更改用于测试的端口
  • 增加客户端尝试连接的延迟
  • 阅读socket.io文档。它令人困惑,不完整,不准确和不一致。

什么可以使套接字IO忽略所有这样的连接?也许服务器正在侦听来自不同路径的请求?如果是这种情况,如何检查IO监听的路径?

1 个答案:

答案 0 :(得分:1)

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

net TCP 服务器(第4层)。

所以他们不兼容。