LazWebsockets
LazWebsockets copied to clipboard
ReceiveMessageThread is still running after calling StopReceiveMessageThread method of communicator
Excuse me. I created a websocket client to communicate with a server. I'm trying to write a destroy method of my class. Please tell me how to solve this problem, thanks a lot.
This is my source code: https://github.com/ifloppy/LibKookBotPas/blob/main/libkookbotpas.pas#L106
My fault. I should use this first: FCommunicator.WriteMessage(wmtClose).Free;
I reopen the issue because this is actually still a problem. Because the ReceiveMessageThread is a blocking loop. So the termination check is only done after the next message has been received, and until then it not stop (unless the stream gets closed).
Using wmtClose for you works, because the other side will respond to that Close message with another Close message, after which the ReceiveMessageThread can check the termination state and stop.
So while this works for you, not being able to stop the listening mid message is still a bit of a problem that I will try to fix in a future version.
PS: Instead of sending wmtClose, you could also just call the .Close
method. If you want to stop the receiving thread without closing the stream, you could also send a wmtPing
message, which will trigger a Pong
result, after which the ReceiveMessageThread will check the termination state.