libhv icon indicating copy to clipboard operation
libhv copied to clipboard

How to close libhv websocket client ?

Open anuragvohraec opened this issue 1 year ago • 4 comments

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

image

Because of this my client keeps running even when the code is all executed.

anuragvohraec avatar Aug 08 '24 12:08 anuragvohraec

Normally, WebSocketClient::close or stop will closesocket. You can try the examples/websocket_client_test.cpp

ithewei avatar Aug 08 '24 13:08 ithewei

I have put debug points. image Only Here1 gets printed. Here2 is never reached!

anuragvohraec avatar Aug 08 '24 15:08 anuragvohraec

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.

ithewei avatar Aug 09 '24 02:08 ithewei

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) image

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.

anuragvohraec avatar Aug 11 '24 10:08 anuragvohraec