diff --git a/Signal-Windows.Lib/SignalWebSocket.cs b/Signal-Windows.Lib/SignalWebSocket.cs index 2c06052..84e13b4 100644 --- a/Signal-Windows.Lib/SignalWebSocket.cs +++ b/Signal-Windows.Lib/SignalWebSocket.cs @@ -51,6 +51,7 @@ private void WebSocket_Closed(IWebSocket sender, WebSocketClosedEventArgs args) private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) { + Logger.LogTrace("WebSocket_MessageReceived()"); try { using (var data = args.GetDataStream()) @@ -75,6 +76,7 @@ public void Close(ushort code, string reason) public async Task ConnectAsync() { + Logger.LogTrace("ConnectAsync()"); var locked = await SemaphoreSlim.WaitAsync(0, Token); // ensure no threads are reconnecting at the same time if (locked) { @@ -83,6 +85,7 @@ public async Task ConnectAsync() try { CreateMessageWebSocket(); + Logger.LogTrace("WebSocket.ConnectAsync()"); await WebSocket.ConnectAsync(SignalWSUri).AsTask(Token); SemaphoreSlim.Release(); break; @@ -100,6 +103,10 @@ public async Task ConnectAsync() } } } + else + { + Logger.LogTrace("ConnectAsync() not reconnecting: Reconnect in progress"); + } } public void Dispose() @@ -109,11 +116,21 @@ public void Dispose() public async Task SendMessage(byte[] data) { - using (var dataWriter = new DataWriter(WebSocket.OutputStream)) + Logger.LogTrace("SendMessage()"); + try + { + using (var dataWriter = new DataWriter(WebSocket.OutputStream)) + { + dataWriter.WriteBytes(data); + await dataWriter.StoreAsync(); + dataWriter.DetachStream(); + } + } + catch (OperationCanceledException) { } + catch (Exception e) { - dataWriter.WriteBytes(data); - await dataWriter.StoreAsync(); - dataWriter.DetachStream(); + Logger.LogError($"SendMessage() failed: {e.Message}\n{e.StackTrace}"); + var t = Task.Run(ConnectAsync); } } }