Skip to content

Commit

Permalink
improved WebSocket #83, improved logging to capture any errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirAkopyan committed Oct 8, 2017
1 parent 2eabfd3 commit b645cd5
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions Quickbird/Networking/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,25 @@ public async Task<bool> 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)
Expand All @@ -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)
Expand Down Expand Up @@ -307,9 +315,13 @@ private async Task<bool> 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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit b645cd5

Please sign in to comment.