sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.73k stars 1.66k forks source link

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

Open levi-lei opened 5 months ago

levi-lei commented 5 months ago

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 commented 5 months ago

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