private static void OnDisconnect(DisconnectionInfo info) { Console.WriteLine( $"---- Disconnection happened. ----\n" + $"Reason: {info.CloseStatus}\n" + $"Message: {info.CloseStatusDescription}\n" + $"Subprotocol: {info.SubProtocol}\n" + $"Exception: {info.Exception}\n" + $"---------------------------------"); if (success) { info.CancelReconnection = true; } else if (info.Exception != null) { info.CancelReconnection = true; SSOEventArgs e = new SSOEventArgs(); e.Exception = info.Exception; e.success = false; if (SSOFinished != null) { SSOFinished(null, e); } } }
private async Task Disconnected(DisconnectionInfo disconnectionInfo) { if (_reconnecting) { return; // disconnects are not valid during a reconnection is going on } _clientLogger.MessageLog($"Disconnect happened - Reason: {nameof(disconnectionInfo.Type)}"); await _clientListener.OnDisconnected(); }
public async Task OnClose_ShouldWorkCorrectly() { using (IWebsocketClient client = new WebsocketClient(WebsocketUrl)) { client.ReconnectTimeout = TimeSpan.FromSeconds(5); string received = null; var receivedCount = 0; var receivedEvent = new ManualResetEvent(false); var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.MessageReceived.Subscribe(msg => { receivedCount++; received = msg.Text; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; }); await client.Start(); #pragma warning disable 4014 Task.Run(async() => #pragma warning restore 4014 { await Task.Delay(2000); var success = await client.Stop(WebSocketCloseStatus.InternalServerError, "server error 500"); Assert.True(success); receivedEvent.Set(); }); receivedEvent.WaitOne(TimeSpan.FromSeconds(30)); Assert.NotNull(received); Assert.Equal(1, receivedCount); var nativeClient = client.NativeClient; Assert.NotNull(nativeClient); Assert.Equal(1, disconnectionCount); Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type); Assert.Equal(WebSocketCloseStatus.InternalServerError, disconnectionInfo.CloseStatus); Assert.Equal("server error 500", disconnectionInfo.CloseStatusDescription); Assert.Equal(WebSocketState.Aborted, nativeClient.State); Assert.Equal(WebSocketCloseStatus.InternalServerError, nativeClient.CloseStatus); Assert.Equal("server error 500", nativeClient.CloseStatusDescription); // check that reconnection is disabled await Task.Delay(7000); Assert.Equal(1, receivedCount); } }
public void OnClose(DisconnectionInfo info) { if (client != null) { client.Dispose(); } Console.WriteLine("Socket closed"); Application.Exit(); }
private void OnWebSocketDisconnected(object sender, DisconnectionInfo disconnectionInfo) { var webSocket = WebSockets.FirstOrDefault(f => f.Value == sender); if (webSocket.Value == null) { return; } OnWebSocketDisconnectedEvent?.Invoke(this, webSocket.Key); }
public async Task Stopping_ByServer_CancelWithReconnect_ShouldNotFinishClosing() { using (var client = _context.CreateClient()) { client.ReconnectTimeout = TimeSpan.FromSeconds(30); client.IsReconnectionEnabled = true; string received = null; var receivedCount = 0; var receivedEvent = new ManualResetEvent(false); var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.MessageReceived .Where(x => x.MessageType == WebSocketMessageType.Text) .Subscribe(msg => { _output.WriteLine($"Received: '{msg}'"); receivedCount++; received = msg.Text; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; x.CancelClosing = true; }); await client.Start(); _ = Task.Run(async() => { await Task.Delay(200); client.Send("close-me"); receivedEvent.Set(); }); receivedEvent.WaitOne(TimeSpan.FromSeconds(30)); // check that reconnection is disabled await Task.Delay(4000); Assert.Equal(2, receivedCount); Assert.InRange(disconnectionCount, 1, 2); Assert.Equal(DisconnectionType.Lost, disconnectionInfo.Type); Assert.Equal(WebSocketCloseStatus.NormalClosure, disconnectionInfo.CloseStatus); Assert.Equal("normal closure", disconnectionInfo.CloseStatusDescription); Assert.True(client.IsRunning); Assert.True(client.IsStarted); } }
public async Task Stopping_ShouldWorkCorrectly() { using (var client = _context.CreateClient()) { client.ReconnectTimeout = TimeSpan.FromSeconds(7); string received = null; var receivedCount = 0; var receivedEvent = new ManualResetEvent(false); var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.MessageReceived .Where(x => x.MessageType == WebSocketMessageType.Text) .Subscribe(msg => { _output.WriteLine($"Received: '{msg}'"); receivedCount++; received = msg.Text; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; }); await client.Start(); _ = Task.Run(async() => { await Task.Delay(200); client.IsReconnectionEnabled = false; await client.Stop(WebSocketCloseStatus.InternalServerError, "server error 500"); receivedEvent.Set(); }); receivedEvent.WaitOne(TimeSpan.FromSeconds(30)); // check that reconnection is disabled await Task.Delay(8000); Assert.Equal(1, receivedCount); Assert.InRange(disconnectionCount, 1, 2); Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type); Assert.Equal(WebSocketCloseStatus.InternalServerError, disconnectionInfo.CloseStatus); Assert.Equal("server error 500", disconnectionInfo.CloseStatusDescription); Assert.False(client.IsRunning); Assert.False(client.IsStarted); } }
public async Task StartOrFail_InvalidServer_ShouldThrowException() { using (var client = _context.CreateInvalidClient(new Uri("wss://google.com"))) { string received = null; var receivedCount = 0; var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; Exception causedException = null; client .MessageReceived .Subscribe(msg => { _output.WriteLine($"Received: '{msg}'"); receivedCount++; received = msg.Text; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; }); try { await client.StartOrFail(); } catch (WebsocketException e) { // expected exception _output.WriteLine($"Received exception: '{e.Message}'"); causedException = e; } await Task.Delay(1000); Assert.Equal(1, disconnectionCount); Assert.Equal(DisconnectionType.Error, disconnectionInfo.Type); Assert.NotNull(disconnectionInfo.Exception); Assert.Equal(causedException?.InnerException, disconnectionInfo.Exception); Assert.Equal(0, receivedCount); Assert.Null(received); } }
public async Task Stopping_ByUser_NormalClosure_ShouldntTriggerReconnect(bool reconnectionEnabled) { using (var client = _context.CreateClient()) { // independently of this config, if it is a normal expected closure by User, it shouldn't reconnect client.IsReconnectionEnabled = reconnectionEnabled; client.ReconnectTimeout = TimeSpan.FromSeconds(7); var reconnectionCount = 0; var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; }); client.ReconnectionHappened.Subscribe(x => { if (x.Type != ReconnectionType.Initial) { reconnectionCount++; } }); await client.Start(); client.Send("ping"); await client.Stop(WebSocketCloseStatus.NormalClosure, "Expected Closure"); // give some time to receive disconnection and reconnection messages await Task.Delay(5000); Assert.Equal(1, disconnectionCount); Assert.Equal(0, reconnectionCount); Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type); Assert.Equal(WebSocketCloseStatus.NormalClosure, disconnectionInfo.CloseStatus); Assert.Equal("Expected Closure", disconnectionInfo.CloseStatusDescription); Assert.False(client.IsRunning); Assert.False(client.IsStarted); } }
private void OnDisconnection(DisconnectionInfo info) { switch (info.Type) { case DisconnectionType.Error: _logger.LogError(info.Exception, "Disconnected with an exception"); _connection.OnNext( new ConnectionInfo( Result.FromFailure <ConnectionState, Exception>(info.Exception))); break; default: _logger.LogError($"'{info.Type}' disconnection event occured"); _connection.OnNext( new ConnectionInfo( Result.FromSuccess <ConnectionState, Exception>(ConnectionState.Disconnected))); break; } }
private async Task Reconnect(ReconnectionType type, bool failFast, System.Exception causedException) { IsRunning = false; if (_disposing) { return; } _reconnecting = true; var disType = TranslateTypeToDisconnection(type); var disInfo = DisconnectionInfo.Create(disType, _client, causedException); if (type != ReconnectionType.Error) { _disconnectedSubject.OnNext(disInfo); if (disInfo.CancelReconnection) { // reconnection canceled by user, do nothing Logger.Info(L($"Reconnecting canceled by user, exiting.")); } } _cancellation.Cancel(); _client?.Abort(); _client?.Dispose(); if (!IsReconnectionEnabled || disInfo.CancelReconnection) { // reconnection disabled, do nothing IsStarted = false; _reconnecting = false; return; } Logger.Debug(L("Reconnecting...")); _cancellation = new CancellationTokenSource(); await StartClient(_url, _cancellation.Token, type, failFast).ConfigureAwait(false); _reconnecting = false; }
private Task _OnDisconnectAsync(DisconnectionInfo info) { _Logger.LogInformation($"Stream disconnected: {info.Type}"); return(Task.CompletedTask); }
public override void OnError(object sender, DisconnectionInfo e) { Console.WriteLine($"Connection closed with error. Code: {e?.ToString()}."); Console.WriteLine($"Retyring again in {DelayBetweenEachReconnection}ms."); }
public override void OnClose(object sender, DisconnectionInfo e) { Console.WriteLine($"Closed connection. {e?.ToString()}."); Console.WriteLine($"Exception: {e?.Exception?.ToString()}."); Console.WriteLine($"Retyring again in {DelayBetweenEachReconnection}ms."); }
void OnClosed(DisconnectionInfo info) { Logger.Warning(this, $"Connection closed: {info.Type}, {info.CloseStatusDescription}", info.Exception?.ToString()); OnDisconnected(info.Type != DisconnectionType.ByUser); }
public async Task CancelingReconnection_ViaDisconnectionStream_ShouldWork() { using (var client = _context.CreateClient()) { var receivedCount = 0; var reconnectedCount = 0; var lastReconnectionType = ReconnectionType.NoMessageReceived; var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; client.IsReconnectionEnabled = true; client.ReconnectTimeout = null; client.MessageReceived .Where(x => x.MessageType == WebSocketMessageType.Text) .Subscribe(msg => { _output.WriteLine($"Received: '{msg}'"); receivedCount++; }); client.ReconnectionHappened.Subscribe(x => { _output.WriteLine($"Reconnected: '{x}'"); reconnectedCount++; lastReconnectionType = x.Type; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; if (disconnectionCount >= 2) { disconnectionInfo.CancelReconnection = true; } }); await client.Start(); await Task.Delay(1000); await client.Reconnect(); await Task.Delay(1000); await client.Reconnect(); await Task.Delay(1000); await client.Reconnect(); await Task.Delay(1000); Assert.Equal(2, disconnectionCount); Assert.Equal(DisconnectionType.ByUser, disconnectionInfo.Type); Assert.Null(disconnectionInfo.Exception); Assert.Equal(2, receivedCount); Assert.Equal(2, reconnectedCount); Assert.Equal(ReconnectionType.ByUser, lastReconnectionType); Assert.False(client.IsRunning); Assert.False(client.IsStarted); } }
#pragma warning restore CS1998 private Task OnDisconnect(DisconnectionInfo info) { UpdateStateDetails(info.Exception?.Message ?? info.Type.ToString()); _healthMonitor.ClearAllWorlds(); return(Task.CompletedTask); }
internal void InvokeDisconnected(Client client, DisconnectionInfo info) { Disconnected?.Invoke(client, new DisconnectedEventArgs(info)); }
private void WebsocketDisconnectionHappened(DisconnectionInfo info) { _settings.Controller.WriteTextToLCDSecondLine("UI24R disconnected. Try to reconnect"); }
public abstract void OnError(object sender, DisconnectionInfo e);
private void WsDisconnected(DisconnectionInfo inf) { _logger.Information("Disconnected from Jackbox games services."); _exitEvent?.Set(); }
private void Disconnected(DisconnectionInfo info) { OnDisconnectedEvent?.Invoke(this, info); }
private Task OnDisconnect(DisconnectionInfo info) { _logger.LogInformation($"Websocket Client Disconnected: {info.Type}"); return(Task.CompletedTask); }
public DisconnectedEventArgs(DisconnectionInfo info) { Info = info; }
private void OnDisconnect(DisconnectionInfo info) { Console.WriteLine(info.Exception); }
private void Disconnected(DisconnectionInfo obj) { OnClose?.Invoke(this, null); }
public abstract void OnClose(object sender, DisconnectionInfo e);
public async Task ManualReconnectOrFail_ShouldThrowException() { using (var client = _context.CreateClient()) { var receivedCount = 0; var reconnectedCount = 0; var lastReconnectionType = ReconnectionType.NoMessageReceived; var disconnectionCount = 0; DisconnectionInfo disconnectionInfo = null; Exception causedException = null; client.IsReconnectionEnabled = true; client.ReconnectTimeout = null; client.MessageReceived .Where(x => x.MessageType == WebSocketMessageType.Text) .Subscribe(msg => { _output.WriteLine($"Received: '{msg}'"); receivedCount++; }); client.ReconnectionHappened.Subscribe(x => { _output.WriteLine($"Reconnected: '{x}'"); reconnectedCount++; lastReconnectionType = x.Type; }); client.DisconnectionHappened.Subscribe(x => { disconnectionCount++; disconnectionInfo = x; }); await client.Start(); await Task.Delay(1000); client.Url = new Uri("wss://google.com"); try { await client.ReconnectOrFail(); } catch (WebsocketException e) { // expected exception _output.WriteLine($"Received exception: '{e.Message}'"); causedException = e; } await Task.Delay(1000); Assert.Equal(2, disconnectionCount); Assert.Equal(DisconnectionType.Error, disconnectionInfo.Type); Assert.NotNull(disconnectionInfo.Exception); Assert.Equal(causedException?.InnerException, disconnectionInfo.Exception); Assert.Equal(1, receivedCount); Assert.Equal(1, reconnectedCount); Assert.Equal(ReconnectionType.Initial, lastReconnectionType); } }
private void HandleDiscordWebSocketDisconnection(DisconnectionInfo disconnectionInfo) { ApplicationManager.Instance.MainViewModel.AppSettingsViewModel.UpdateDiscordWebSocketState(disconnectionInfo .Type); }
private void Ws_OnClose(DisconnectionInfo disconnectionInfo) { OnDisconnect?.Invoke(this, (WebsocketDisconnectStatus)disconnectionInfo.Type); }