I have bot socket IO client and server running. I want to have client communicate with server.
Server initialization
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); })
Client initialization
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.") });
Socket IO debug output
I enabled debug, but socket IO debug output is not very useful anyway:
[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
Notice that the listener for connection that I added triggers, but socket IO does nothing.
What did not help so far:
- Removing the
connection
listener from thethis.http_server
- Changing port used for testing
- Increasing delay after which client tries to connect
- Reading socket.io documentation. It’s confusing, incomplete, inaccurate and inconsistent.
What could make socket IO ignore all connections like this? Maybe the server is listening for request from different paths? If that’s the case, how to check what path is IO listening on?
Advertisement
Answer
socket.io works with http/s servers like express (layer 7).
net is a TCP server (layer 4).
So they are not compatible.