From 539edaa50ea3fde4c9c8913de9ccc5acc61e1c22 Mon Sep 17 00:00:00 2001 From: Tim Miller Date: Sun, 19 Jan 2025 16:38:07 +0900 Subject: [PATCH] More tweaks --- src/FishyFlip/ATWebSocketProtocol.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/FishyFlip/ATWebSocketProtocol.cs b/src/FishyFlip/ATWebSocketProtocol.cs index 88be3a5..12ad4aa 100644 --- a/src/FishyFlip/ATWebSocketProtocol.cs +++ b/src/FishyFlip/ATWebSocketProtocol.cs @@ -157,7 +157,7 @@ private void Dispose(bool disposing) { if (disposing) { - // this.webSocketWrapper.DisposeAsync(); + this.webSocketWrapper.DisposeAsync().GetAwaiter().GetResult(); } this.disposedValue = true; @@ -281,7 +281,13 @@ void HandleProgressStatus(CarProgressStatusEvent e) private Task OnMessageReceived(ReadOnlySequence message) { var newMessage = message.ToArray(); - Task.Run(() => this.HandleMessage(newMessage)).FireAndForgetSafeAsync(this.logger); + Task.Run(() => + { + if (this.IsConnected) + { + this.HandleMessage(newMessage); + } + }).FireAndForgetSafeAsync(this.logger); return Task.CompletedTask; } @@ -323,6 +329,11 @@ public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken = de public async Task CloseAsync(CancellationToken cancellationToken = default) { + if (this.webSocket.State == WebSocketState.Closed || this.webSocket.State == WebSocketState.Aborted) + { + return; + } + this.logger?.LogInformation("WSS: Closing WebSocket connection."); await this.webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", cancellationToken); } @@ -351,7 +362,7 @@ private async Task StartReceiveLoop() { try { - while (!this.cts.Token.IsCancellationRequested && this.webSocket.State == WebSocketState.Open) + while (!this.cts.Token.IsCancellationRequested || this.webSocket.State == WebSocketState.Open) { var memory = this.pipe.Writer.GetMemory(8192); #if NETSTANDARD