Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
levi-lei opened this issue Jun 7, 2024 · 1 comment
Open

Comments

@levi-lei
Copy link

levi-lei commented Jun 7, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant