private async Task <bool> TryConnect() { if (SettingsService.Instance.IsLoggedIn == false) { return(false); } _webSocket = new MessageWebSocket(); _webSocket.Control.MessageType = SocketMessageType.Utf8; _webSocket.Closed += SocketClosed; _webSocket.MessageReceived += MessageRecieved; var tokenHeader = "X-ZUMO-AUTH"; var creds = Creds.FromUserIdAndToken(SettingsService.Instance.CredUserId, SettingsService.Instance.CredToken); _webSocket.SetRequestHeader(tokenHeader, creds.Token); try { var uri = new Uri("wss://greenhouseapi.azurewebsites.net/api/Websocket"); await _webSocket.ConnectAsync(uri); _messageWriter = new DataWriter(_webSocket.OutputStream); _reconnectionAttempt = 0; LoggingService.LogInfo("Websocket connected", Windows.Foundation.Diagnostics.LoggingLevel.Information); return(true); } catch (Exception ex) { 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); } }
private async Task <bool> TryConnect() { if (Settings.Instance.IsLoggedIn == false) { return(false); } _webSocket = new MessageWebSocket(); _webSocket.Control.MessageType = SocketMessageType.Utf8; _webSocket.Closed += SocketClosed; _webSocket.MessageReceived += MessageRecieved; var tokenHeader = "X-ZUMO-AUTH"; var creds = Creds.FromUserIdAndToken(Settings.Instance.CredUserId, Settings.Instance.CredToken); _webSocket.SetRequestHeader(tokenHeader, creds.Token); try { var uri = new Uri("wss://greenhouseapi.azurewebsites.net/api/Websocket"); await _webSocket.ConnectAsync(uri); _messageWriter = new DataWriter(_webSocket.OutputStream); _reconnectionAttempt = 0; Debug.WriteLine("Websocket connected"); return(true); } catch { Debug.WriteLine("Websocket connection failed"); _reconnectionAttempt++; CleanUp(); return(false); } }