Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

how to initiate send message from client #94

Open
1355700334 opened this issue Jan 19, 2018 · 3 comments
Open

how to initiate send message from client #94

1355700334 opened this issue Jan 19, 2018 · 3 comments

Comments

@1355700334
Copy link

it seems client can only send message within a callback function like on_open(), because it requires "shared_ptrWssClient::Connection" from the on_open input parameter to make the "connection->send(send_stream);" action. But if in my application, the client side sometimes needs to initiate the message, something like this function:

void sendMsg(msg) {}

can the shared_ptrWssClient::Connection be made into a variable in the client class? or else how to do that? thanks.

@eidheim
Copy link
Owner

eidheim commented Jan 19, 2018

You can store the connection shared_ptr that you receive from the on_open handler, and use this shared_ptr to send messages outside of the handlers.

@koallen
Copy link

koallen commented May 16, 2018

I'm trying the same thing. It seems that once I call client.start(), the thread starts listening to events and I cannot do anything on this thread. I have to start another thread to initiate send from the client (after getting the Connection object).

Is this expected behavior? I'm new to WebSocket, but in traditional socket programming once we connect we can keep operating in the thread.

@eidheim
Copy link
Owner

eidheim commented May 18, 2018

Yes, this library is asynchronous, so this is expected behaviour. When you call client.start() the handlers (the on_message lambda for instance) are run in this thread.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants