protected async void Run(CancellationToken cancellationToken, Uri uri) { WebSocket = new ClientWebSocket(); OnConnecting(); WebSocket = await Connection.OpenConnection(WebSocket, uri); if (WebSocket.State == WebSocketState.Open) { OnConnected(); } while (WebSocket.State == WebSocketState.Open) { if (cancellationToken.IsCancellationRequested) { await Cancel(WebSocket); break; } ICommand message = null; lock (queueLock) { message = OutgoingMessages.Any() ? OutgoingMessages.Dequeue() : null; } if (message != null) { await SendMessage(WebSocket, message); } } System.Diagnostics.Trace.WriteLine($"Connection closed. Reason: {WebSocket.CloseStatus}, Details: {WebSocket.CloseStatusDescription}"); OnDisconnect(); }