websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

The websocket client is closed, but messages are still being received.

Open levi-lei opened this issue 1 year ago • 1 comments

The websocket client is closed, but there are still messages being received. Our business wants the old messages to be discarded, but we have not found the code to clear the message queue. Is this a special scenario? Or in most scenarios, after the connection is closed, we still want the client to process the received messages.

levi-lei avatar Jun 07 '24 07:06 levi-lei

Below is the code and log. After receiving the message that the server closes the websocket, the message can still be received and processed. But I don't want to continue processing messages after closing Websocket client code:

_socket = new WebSocket(url) {Compression = CompressionMethod.Deflate};

_socket.OnClose += SocketOnOnClose;
_socket.OnMessage += SocketOnOnMessage;
_socket.OnOpen += SocketOnOnOpen;
_socket.OnError += SocketOnOnError;
if (url.StartsWith("wss"))
{
    if (_config.IsSkipCertificateValidation)
    {
        _socket.SslConfiguration.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
    }
    _socket.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
}
_logger.LogInfo($"\t\tConnection Start\t\t\turl: {url}");
_socket.Connect();
private void SocketOnOnClose(object sender, CloseEventArgs closeEventArgs)
{
    if (closeEventArgs.Code == (ushort) CloseStatusCode.Normal) return;
    _logger.LogInfo(
        $"\t\tConnection Closed by remote\t\tCode: {closeEventArgs.Code}, Reason: {closeEventArgs.Reason}");
    //StopConnectionAndClearConnectionUrl();
}
private void SocketOnOnMessage(object sender, MessageEventArgs messageEventArgs) =>
    HandleMessage(messageEventArgs.Data);

private void HandleMessage(string data)
{
    var info = data.ConvertXml<Data>();
    _logger.LogInfo($" log handle message info");
}

At this time, the websocket is already connected and the message is received.

(0017) (0017) 2024-06-04 16:16:34.619 +08:00 [Information] 	log handle message info
(0012) (0012) 2024-06-04 16:16:34.627 +08:00 [Information] 	log handle message info
(0017) (0017) 2024-06-04 16:16:34.651 +08:00 [Information] 	log handle message info
(0012) (0012) 2024-06-04 16:16:34.661 +08:00 [Information] 	log handle message info 
(0017) (0017) 2024-06-04 16:16:34.689 +08:00 [Information] 	log handle message info 
(0012) (0012) 2024-06-04 16:16:34.705 +08:00 [Information] 	log handle message info
(0017) (0017) 2024-06-04 16:16:34.725 +08:00 [Information] 	log handle message info

At this time, the websocket is closed.

(0018) (0018) 2024-06-04 16:16:34.730 +08:00 [Information] 	Connection Closed by remote		Code: 1001, Reason:  

At this time, the websocket has been closed, but messages can still be received.

(0017) (0017) 2024-06-04 16:16:34.765 +08:00 [Information] 	log handle message info
(0017) (0017) 2024-06-04 16:16:34.806 +08:00 [Information] 	 log handle message info
(0017) (0017) 2024-06-04 16:16:34.842 +08:00 [Information] 	 log handle message info
(0017) (0017) 2024-06-04 16:16:34.874 +08:00 [Information] 	log handle message info

levi-lei avatar Jun 14 '24 07:06 levi-lei