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

"socket.io-client-cpp" nevers receives any message from the server

Open kadersin opened this issue 3 years ago • 1 comments

Hello all,

I have an issue with socket.io-client on Linux. I'm connecting to a C++ server I made using WebSocketpp. I absolutely must use "socket.io-client-cpp" to create the client. I made a mock-up client in WebSocketpp for testing, as I had issues with the one in "socket.io-client-cpp".

My problem is that the client written in WebSocketpp works well, but I have issues with the one in "socket.io-client-cpp".

The following client connects to my server, but it doesn't seam to receive any message from the server. Any messages sent by the client is never received by the server. Thus, nothing comes in or out of the client.

Do you have any idea? I'm kind of desperate at the moment...

Here's the full code of the client (it is an adaptation of an example):

#include "sio_client.h"

#include <functional>
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <string>
#include <thread>


using namespace sio;
using namespace std;
std::mutex _lock;

std::condition_variable_any _cond;
bool connect_finish = false;

class connection_listener
{
    sio::client &handler;

public:
    
    connection_listener(sio::client& h):
    handler(h)
    {
    }

    void on_connected()
    {
        std::cout<<"sio connected "<<std::endl;
        _lock.lock();
        _cond.notify_all();
        connect_finish = true;
        _lock.unlock();
    }
    void on_close(client::close_reason const& /*reason*/)
    {
        std::cout<<"sio closed "<<std::endl;
        exit(0);
    }
    
    void on_fail()
    {
        std::cout<<"sio failed "<<std::endl;
        exit(0);
    }
};

socket::ptr current_socket;

void bind_events(socket::ptr &mysocket)
{
    mysocket->on("hello", [&](sio::event& /*ev*/)
    {
        cout << "Received something!" << endl;
    });

	mysocket->on("hello_too", sio::socket::event_listener_aux([&](string const& name, message::ptr const& /*data*/, bool /*isAck*/,message::list &/*ack_resp*/)
    {
        cout << "Received: " << name << endl;
    }));
}

int main(int /*argc*/,const char* args[])
{
    sio::client h;
    connection_listener l(h);
    
    h.set_open_listener(std::bind(&connection_listener::on_connected, &l));
    h.set_close_listener(std::bind(&connection_listener::on_close, &l,std::placeholders::_1));
    h.set_fail_listener(std::bind(&connection_listener::on_fail, &l));
    h.connect("http://127.0.0.1:3000");

    _lock.lock();
    if(!connect_finish)
    {
        _cond.wait(_lock);
    }
    _lock.unlock();
	current_socket = h.socket();

    cout << "Binding..." << endl;
    bind_events(current_socket);

    cout << "Sending message" << endl;
    current_socket->emit("my message", std::string("patate!"));

    while(true)    
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(300));
    }

    h.sync_close();
    h.clear_con_listeners();
	return 0;
}

kadersin avatar Apr 19 '22 01:04 kadersin

Hi!

I'm connecting to a C++ server I made using WebSocketpp

The Socket.IO client implement the Socket.IO protocol and will not be able to reach a plain WebSocket server. Could it explain your problem?

darrachequesne avatar Apr 20 '22 20:04 darrachequesne