LazWebsockets icon indicating copy to clipboard operation
LazWebsockets copied to clipboard

ReceiveMessageThread is still running after calling StopReceiveMessageThread method of communicator

Open ifloppy opened this issue 1 year ago • 2 comments

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

ifloppy avatar Jul 30 '23 03:07 ifloppy

My fault. I should use this first: FCommunicator.WriteMessage(wmtClose).Free;

ifloppy avatar Aug 01 '23 10:08 ifloppy

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.

Warfley avatar Aug 29 '23 18:08 Warfley