socket.io-client-cpp icon indicating copy to clipboard operation
socket.io-client-cpp copied to clipboard

Can't connect to NodeJS socket io server from C++ client

Open AnushaPulichintha opened this issue 4 years ago • 4 comments

Hi Everyone, I am trying to connect NodeJS socket io server from socket io c++ client, But I keep getting below error on c++ side.

[2021-10-05 15:32:55] [error] handle_read_frame error: asio.system:104 (Connection reset by peer)
[2021-10-05 15:32:55] [info] asio async_shutdown error: asio.system:32 (Broken pipe)
[2021-10-05 15:32:55] [disconnect] Disconnect close local:[1006,Connection reset by peer] remote:[1006]

Below is my socket io server which is attached to existing https server, which is running on docker container

const fs = require("fs");
const httpServer = require("https").createServer({  key: fs.readFileSync("/tmp/key.pem"),  cert: fs.readFileSync("/tmp/cert.pem")});
const io = require("socket.io")(httpServer, {           
           allowEIO3: true,
            cors: {
                methods: 'GET,POST,PATCH,DELETE,OPTIONS',
                optionsSuccessStatus: 200,
                origin: true,
                credentials: true,
                allowedHeaders: ["content-type"]
            }});
io.on("connection", socket => { /* ... */ });
httpServer.listen(3000);

I have integrated socketio c++ client as static library in cmake.

find_library(sioclient_location NAMES libsioclient_tls.a)
message(STATUS ${sioclient_location})

add_library(sioclient_tls STATIC IMPORTED)
set_target_properties(sioclient_tls PROPERTIES IMPORTED_LOCATION ${sioclient_location})
target_link_libraries(TestNode  sioclient_tls)

I tried to create c++ client almost like console example using lock and conditional wait after calling connect method.

sio::client m_client

m_client.set_open_listener(
            std::bind(&TestNode::onConnected, this));
m_client.set_close_listener(
            std::bind(&TestNode::onClose, this, std::placeholders::_1));
m_client.set_fail_listener(
            std::bind(&TestNode::onFail, this));

m_client.connect("https://localhost:3000"); // I did try using "0.0.0.0" and I exposed the port in my docker compose
m_client. socket()->on(
            "new_message",
            std::bind(&TestNode::OnMessage, this, std::placeholders::_1));

Any ideas?

AnushaPulichintha avatar Oct 05 '21 07:10 AnushaPulichintha

Any update on fix or workaround of this issue? I am facing similar issue where I am unable to connect to a NodeJS socketio server. The server runs socket.io version 4.4.1. The error I see from my c++ client:

Error: No active session
[2022-02-27 22:45:08] [connect] Successful connection
[2022-02-27 22:45:09] [error] handle_read_http_response error: websocketpp.transport:7 (End of File)
Error: No active session

hrishibawane avatar Feb 27 '22 17:02 hrishibawane

Hi @AnushaPulichintha , I figured out the issue for me which could be a possible issue in this case. The issue is that when using EIO v3 the server sends the CONNECT message packet. However, for EIO v4, the client has to send the CONNECT message packet. CONNECT message packet is identified by text 40 where, 4 => engine.io packet type MESSAGE 0 => socket.io packet type CONNECT

This issue seems to be in handling of the engine.io protocol, after the shift from v3 to v4. If this is identified as a bug, it needs to be fixed.

hrishibawane avatar Feb 28 '22 07:02 hrishibawane

Hi @hrishibawane could you point me to the part of the engine.io documentation where this is addressed? From the socket.io-protocol documentation (link) I see that the client is the one that should always send the CONNECT message packet

jmigual avatar Mar 10 '22 10:03 jmigual

@jmigual Yes it does but somehow when I was using EIO v3 I (as client) didn't need to send CONNECT message packet to the server as I was receiving it from the server (verified from a Postman client too). In case of EIO v3, the client was not sending any extra messages after the initial connect attempt. The namespace is default namespace.

If the client explicitly needs to send a CONNECT even for a default namespace, I am not sure why the server sent it in the above case.

hrishibawane avatar Mar 12 '22 19:03 hrishibawane