How to close libhv websocket client ?
Hi ithewei,
I have created a websocketclient connected to a remote WSS server.
However I am unable to close/stop the client from the client side, even after calling stop method of hv::WebSocketClient
Because of this my client keeps running even when the code is all executed.
Normally, WebSocketClient::close or stop will closesocket. You can try the examples/websocket_client_test.cpp
I have put debug points.
Only
Here1 gets printed.
Here2 is never reached!
Stop will join and wait EventLoopThread exit, you can add some print in EventLoopThread::loop_thread function to diagnose why the thread didn't exit.
Here is my conclusion based on my understanding after several trials:
hv::WebSocketClient will end any event loop on its own, but keeping socket connection open, if the event loop passsed to it is not in running state. Same applies for the default loop created by the hv::WebSocketClient (i.e. without passing any eventloop) , it ends this loop immediately. But for some reason the socket connection is not released. That is why the program is not exiting.
If the libhv is expected to behave this way, than I am happy to close these issue.
Ok so I fixed the issue at my end as a work around.
So I created a hv::EventLoopPtr inside a seperate thread and made it in running status inside this thread.
Than passed this running event loop pointer to hv::WebSocketClient, which now do not automatically closes a running event loop pointer(which would otherwise if the event loop pointer was not in running status, before passing to its constructor)
Now one can uses it to create the websockets:
wsc=std::shared_ptr<hv::WebSocketClient>(new hv::WebSocketClient(loop));
as the loop is already in running state so it won't stop ths loop on its own.