From fba53f969944e7b3d6a52532e1c41972e1ca5732 Mon Sep 17 00:00:00 2001 From: Craig McLure Date: Fri, 9 Aug 2024 12:36:23 +0100 Subject: [PATCH] When disconnecting, prepare for a new connection. --- UtilityClient/Native/WebsocketClient.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/UtilityClient/Native/WebsocketClient.cs b/UtilityClient/Native/WebsocketClient.cs index e47a1e1..d8cfed7 100644 --- a/UtilityClient/Native/WebsocketClient.cs +++ b/UtilityClient/Native/WebsocketClient.cs @@ -74,6 +74,7 @@ private async Task ReceiveMessageTask() case WebSocketMessageType.Binary: default: + // This wont occur with the Utility, but we need to trigger DisconnectAsync to perform tidying up! await _client.CloseAsync(WebSocketCloseStatus.ProtocolError, "Only Text is supported.", CancellationToken.None); this.OnDisconnected?.Invoke(this, "Connection closed because server tried to send binary or invalid message."); break; @@ -94,8 +95,8 @@ private async Task ConnectionTask() case WebSocketState.Aborted: case WebSocketState.Closed: - if (this._isConnected) this.OnDisconnected?.Invoke(this, "Connection closed."); - this._isConnected = false; + // Trigger an internal disconnect to clean resources. + Task.Run(DisconnectAsync); break; default: @@ -126,7 +127,10 @@ public async Task ConnectAsync(Uri uri) public async Task DisconnectAsync() { - await this._client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); + // Only attempt to close the socket if it's not already closed + if (this._client.State != WebSocketState.Aborted && this._client.State != WebSocketState.Closed) { + await this._client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None); + } this.OnDisconnected?.Invoke(this, "Connection closed."); this._cancellationTokenSource.Cancel(); @@ -140,6 +144,15 @@ public async Task DisconnectAsync() this._receiveMessageTask.Dispose(); this._connectionTask.Dispose(); + + // Dispose of, and create a new client / Token for future connections + this._client.Dispose(); + this._client = new(); + this._cancellationTokenSource.Dispose(); + this._cancellationTokenSource = new(); + + // Flag the connection as disconnected + this._isConnected = false; } public async Task SendMessage(string message)