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

Fail to receive binary message

Open linsinan1995 opened this issue 4 years ago • 6 comments

I have a similar problem as https://github.com/socketio/socket.io-client-cpp/issues/287 says when receiving the binary message from my server. It fails to parse binary payload, and raises an error terminating with uncaught exception of type std::invalid_argument: stoi: no conversion.

My case is that somehow the payload in packet_manager::put_payload function from sio_packet.cpp doesn't have any 'prefix' or 'frame', which means payload[0] is not a digit and thus packet::is_binary_message(payload) (https://github.com/socketio/socket.io-client-cpp/blob/master/src/internal/sio_packet.cpp#L485 ) return a false. This is also why stoi leads to an error.

I have a quick patch for my issue. Hope it helps.

diff --git a/src/internal/sio_packet.cpp b/src/internal/sio_packet.cpp
index 4b81098..4b2e475 100755
--- a/src/internal/sio_packet.cpp
+++ b/src/internal/sio_packet.cpp
@@ -246,7 +246,6 @@ namespace sio
     bool packet::parse_buffer(const string &buf_payload)
     {
         if (_pending_buffers > 0) {
-            assert(is_binary_message(buf_payload));//this is ensured by outside.
             _buffers.push_back(std::make_shared<string>(buf_payload.data(),buf_payload.size()));
             _pending_buffers--;
             if (_pending_buffers == 0) {
@@ -482,7 +481,7 @@ namespace sio
                     break;
                 }
             }
-            else if(packet::is_binary_message(payload))
+            else if(packet::is_binary_message(payload) || (m_partial_packet && !isdigit(payload[0])))
             {
                 if(m_partial_packet)
                 {

linsinan1995 avatar Mar 30 '21 18:03 linsinan1995

I have the same issue

ferdinandmsu avatar Jul 25 '21 10:07 ferdinandmsu

I have the same issue

I have stopped using cppclient for a while, but it seems my fix works fine for my cases. Maybe you can have a try?

linsinan1995 avatar Jul 26 '21 06:07 linsinan1995

yes your patch works fine for me :+1:

ferdinandmsu avatar Jul 28 '21 11:07 ferdinandmsu

I'm currently having the same problem in the latest version of the lib. Sending a binary message from C++ to a NodeJS works fine, but when receiving it (even without handlers), it crashes on std::stoi (from what I can tell, it passes an empty string).

Darkratos avatar Dec 26 '23 19:12 Darkratos

I'm currently having the same problem in the latest version of the lib. Sending a binary message from C++ to a NodeJS works fine, but when receiving it (even without handlers), it crashes on std::stoi (from what I can tell, it passes an empty string).

have you found a fix, i have the same error

LowBoBYT avatar Mar 24 '24 13:03 LowBoBYT

I'm currently having the same problem in the latest version of the lib. Sending a binary message from C++ to a NodeJS works fine, but when receiving it (even without handlers), it crashes on std::stoi (from what I can tell, it passes an empty string).

have you found a fix, i have the same error

I think I just implemented the fix proposed by OP... Not sure if I've done anything else.

Darkratos avatar Mar 24 '24 13:03 Darkratos