From b645cd5e4bea4e4c544ff18ff14c454b4843a2c9 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sun, 8 Oct 2017 19:49:50 +0200 Subject: [PATCH] improved WebSocket #83, improved logging to capture any errors. --- Quickbird/Networking/WebSocketConnection.cs | 53 +++++++++++++-------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/Quickbird/Networking/WebSocketConnection.cs b/Quickbird/Networking/WebSocketConnection.cs index 5fbe4d0..c53ce6d 100644 --- a/Quickbird/Networking/WebSocketConnection.cs +++ b/Quickbird/Networking/WebSocketConnection.cs @@ -126,23 +126,25 @@ public async Task SendAsync(object toSend) { var jsonData = JsonConvert.SerializeObject(toSend); - if ((ConnectionState) Interlocked.Read(ref _connectionState) == ConnectionState.Connected) + if ((ConnectionState)Interlocked.Read(ref _connectionState) != ConnectionState.Connected) + return false; + else if (_messageWriter == null) + return false; + + try { - try - { - // Send the data as one complete message. - _messageWriter.WriteString(jsonData); - await _messageWriter.StoreAsync(); - return true; - } - catch (Exception ex) - { - LoggingService.LogInfo($"Error while sending websocket data, {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Error); - CleanUp(); - return false; - } + // Send the data as one complete message. + _messageWriter.WriteString(jsonData); + await _messageWriter.StoreAsync(); + return true; } - return false; + catch (Exception ex) + { + var error = WebSocketError.GetStatus(ex.HResult); + LoggingService.LogInfo($"Error while sending websocket data, {error}, full exception:{ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Warning); + CleanUp(); + return false; + } } private void MessageRecieved(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args) @@ -167,7 +169,13 @@ private void MessageRecieved(MessageWebSocket sender, MessageWebSocketMessageRec } catch (Exception ex) { - LoggingService.LogInfo($"error while reading websocket message: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Error); + var error = WebSocketError.GetStatus(ex.HResult); + if(error == Windows.Web.WebErrorStatus.ConnectionAborted) + { + LoggingService.LogInfo($"{Enum.GetName(typeof(Windows.Web.WebErrorStatus), error)} while recieving websocketData. Full exception: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Information); + return; + } + LoggingService.LogInfo($"error while reading websocket message {error}, full exception: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Error); //If it's not inc connected state, then probably someone is already cleaning it up if ( (ConnectionState) @@ -307,9 +315,13 @@ private async Task TryConnect() LoggingService.LogInfo("Websocket connected", Windows.Foundation.Diagnostics.LoggingLevel.Information); return true; } - catch + catch (Exception ex) { - LoggingService.LogInfo("Websocket connection failed", Windows.Foundation.Diagnostics.LoggingLevel.Warning); + var error = WebSocketError.GetStatus(ex.HResult); + if (error == Windows.Web.WebErrorStatus.CannotConnect) + { LoggingService.LogInfo($"Websocket cannot connect", Windows.Foundation.Diagnostics.LoggingLevel.Information); } + else + { LoggingService.LogInfo($"Websocket connection failed due to {error}, full exception: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Warning); } _reconnectionAttempt++; CleanUp(); return false; @@ -359,10 +371,11 @@ private void CleanUp() } catch (Exception ex) { - LoggingService.LogInfo($"Closing Websocket Failed, {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Error); + var error = WebSocketError.GetStatus(ex.HResult); + LoggingService.LogInfo($"Closing Websocket Failed, {error}, full exception: {ex.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Error); } - _messageWriter?.DetachStream(); + /// _messageWriter?.DetachStream(); TODO: Causes crashes occationally, see issue #83 https://github.com/quickbird-uk/QuickbirdUWPDashboard/issues/83 _messageWriter?.Dispose(); _messageWriter = null;