private void waitStateChange(HubConnectionState expectedState, int timeout, string errorMessage) { #if DEBUG if (Debugger.IsAttached) { timeout = int.MaxValue; } #endif var startWaiting = Environment.TickCount; while (State != expectedState && Environment.TickCount - startWaiting < timeout) { Thread.Sleep(1); } if (Monitor.TryEnter(_sync, Math.Max(0, timeout - (Environment.TickCount - startWaiting)))) { Monitor.Exit(_sync); } if (State != expectedState) { throw new TimeoutException(errorMessage); } }
public ChannelReader <IEnumerable <RedditEntry> > Feed(string feedId) { var channel = Channel.CreateUnbounded <IEnumerable <RedditEntry> >(); var connection = new HubConnectionState { ConnectionId = Context.ConnectionId, FeedId = feedId, StreamChannel = channel, }; if (_feedService.TryGetFeed(feedId, out var feed)) { var data = feed.GetData(); if (data.Count() > 0) { _ = connection.StreamChannel.Writer.WriteAsync(data); connection.LastEntryName = feed.LastEntryName; } } else { _feedService.AddFeed(feedId, new RedditFeed { Id = feedId }); } _stateService.AddConnection(connection); return(channel.Reader); }
public async Task <bool> ConnectWithRetryAsync(CancellationToken token) { // Keep trying to until we can start or the token is canceled. while (true) { try { await _connection.StartAsync(token); // Debug.Assert(_connection.State == HubConnectionState.Connected); _logger.LogInformation($"The signalr client connected at {DateTime.Now.ToString()}"); state = HubConnectionState.Connected; await _connection.InvokeAsync("JoinHub", _appSettings.MachineID); return(true); } catch (Exception ex) { _logger.LogError(ex.Message); // Failed to connect, trying again in 5000 ms. // Debug.Assert(_connection.State == HubConnectionState.Disconnected); await Task.Delay(5000); _logger.LogInformation($"The signalr client is reconnecting at {DateTime.Now.ToString()}"); } if (state == HubConnectionState.Connected) { return(true); } } }
private async Task <bool> ConnectWithRetryAsync() { // Keep trying to until we can start or the token is canceled. while (true) { try { await _client.StartAsync(); // Debug.Assert(_connection.State == HubConnectionState.Connected); _logger.LogInformation($"The signalr client connected at {DateTime.Now.ToString()}"); state = HubConnectionState.Connected; return(true); } catch { // Failed to connect, trying again in 5000 ms. // Debug.Assert(_connection.State == HubConnectionState.Disconnected); await System.Threading.Tasks.Task.Delay(3000); _logger.LogInformation($"The signalr client is reconnecting at {DateTime.Now.ToString()}"); } if (state == HubConnectionState.Connected) { return(true); } } }
// Update is called once per frame void Update() { if (_connection.State != _lastConnectionState) { Debug.Log($"Hub connection state changed from {_lastConnectionState} to {_connection.State}"); _lastConnectionState = _connection.State; } }
/// <summary> /// Changes the state. /// </summary> /// <param name="state">State to change.</param> private void SetStateChanged(HubConnectionState state) { _connectionState = state; StateChanged?.Invoke(this, state); if (state == HubConnectionState.Disconnected) { Disconnected?.Invoke(this, null); } }
private static State?MapToState(HubConnectionState state) { return(state switch { HubConnectionState.Disconnected => State.SignIn, HubConnectionState.Connecting => State.Connecting, HubConnectionState.Connected => State.Chatting, HubConnectionState.Reconnecting => State.Connecting, _ => null });
/// <summary> /// Changes the state. /// </summary> /// <param name="state">State to change.</param> private void SetStateChanged(HubConnectionState state) { Logger.LogInformation(state.ToString()); _connectionState = state; StateChanged?.Invoke(this, state); if (state == HubConnectionState.Disconnected) { Disconnected?.Invoke(this, null); } }
public static async Task Subscribe() { if (HubConnectionState.Equals(ConnectionState.Open)) { return; } var connection = await GetConnection().ConfigureAwait(false); connection.On <TextMoodModel>(SignalRConstants.SendNewTextMoodModelName, textMoodModel => { var textResultsListViewModel = GetTextResultsListViewModel(); textResultsListViewModel?.AddTextMoodModelCommand?.Execute(textMoodModel); }); }
private void ClosedHub() { _client.Closed += async(error) => { //await System.Threading.Tasks.Task.Delay(new Random().Next(0, 5) * 1000); _logger.LogInformation($"Envent: Closed - The signalr client is restarting!"); state = HubConnectionState.Disconnected; while (true) { if (await ConnectWithRetryAsync()) { break; } } }; }
private void ReconnectedHub() { _client.Reconnected += async(connectionId) => { _logger.LogInformation($"Connection successfully reconnected. The ConnectionId is now: {connectionId}"); state = HubConnectionState.Connected; while (true) { if (await ConnectWithRetryAsync()) { break; } } }; }
public bool IsHubConnected() { try { if (hubConnection == null) { ResetConnection(); } HubConnectionState state = hubConnection.State; return(state == HubConnectionState.Connected); } catch (Exception ex) { Crashes.TrackError(ex, Logic.GetErrorProperties(ex)); return(false); } }
private void ReconnectingHub() { _client.Reconnecting += async error => { _logger.LogInformation($"Connection started reconnecting due to an error: {error.Message}"); _logger.LogInformation($"State Hub {_client.State} - State Global {state}"); if (_client.State == HubConnectionState.Reconnecting) { state = HubConnectionState.Disconnected; } while (true) { if (await ConnectWithRetryAsync()) { break; } } }; }
public StateChange(HubConnectionState oldState, HubConnectionState newState) { OldState = oldState; NewState = newState; }
public static void AttemptingStateTransition(ILogger logger, HubConnectionState expectedState, HubConnectionState newState) { _attemptingStateTransition(logger, expectedState, newState, null); }
public static void StateTransitionFailed(ILogger logger, HubConnectionState expectedState, HubConnectionState newState, HubConnectionState actualState) { _stateTransitionFailed(logger, expectedState, newState, actualState, null); }
public void AddConnection(HubConnectionState state) { _data.TryAdd(state.ConnectionId, state); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _connection = new HubConnectionBuilder() .WithUrl(_appSettings.SignalrUrl) .WithAutomaticReconnect() .Build(); MyConsole.Info(JsonConvert.SerializeObject(_appSettings)); _connection.On <string, string, string>("Welcom", (scalingMachineID, message, unit) => { if (scalingMachineID == _appSettings.MachineID.ToString()) { var receiveValue = JsonConvert.SerializeObject(new { scalingMachineID, message, unit }); MyConsole.Info($"#### ### Data => {receiveValue}"); } }); _connection.Closed += async(error) => { _logger.LogError(error.Message); await Task.Delay(new Random().Next(0, 5) * 1000); _logger.LogError($"Envent: Closed - The signalr client is restarting!"); await _connection.StartAsync(); }; _connection.Reconnecting += async error => { _logger.LogInformation($"State Hub {_connection.State} - State Global {state}"); _logger.LogInformation($"Connection started reconnecting due to an error: {error.Message}"); if (_connection.State == HubConnectionState.Reconnecting) { state = HubConnectionState.Disconnected; } while (state == HubConnectionState.Disconnected) { if (await ConnectWithRetryAsync(stoppingToken)) { break; } } }; _connection.Reconnected += async(connectionId) => { _logger.LogInformation($"Connection successfully reconnected. The ConnectionId is now: {connectionId}"); state = HubConnectionState.Connected; while (true) { if (await ConnectWithRetryAsync(stoppingToken)) { break; } } }; while (state == HubConnectionState.Disconnected) { if (await ConnectWithRetryAsync(stoppingToken)) { break; } } _logger.LogInformation($"#### ### ClientId: {_connection.ConnectionId}"); string message; StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; Thread readThread = new Thread(async() => await Read() ); // Create a new SerialPort object with default settings. _logger.LogInformation($"#### ### Serial Port is established"); _serialPort = new SerialPort(); // Allow the user to set the appropriate properties. string port = _appSettings.PortName; _serialPort.PortName = port; string scanPortName = ScanPortName(); while (!port.Equals(scanPortName)) { if (state == HubConnectionState.Connected) { _logger.LogWarning($"#### ### The system is scanning {port} at {DateTime.Now.ToString()}"); scanPortName = ScanPortName(); await Task.Delay(1000, stoppingToken); } } _logger.LogInformation($"#### ### {port}"); _serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500; _serialPort.Open(); _logger.LogWarning($"#### ### #### ### Serial Port is already open at {DateTime.Now.ToString()}"); _continue = true; readThread.Start(); while (_continue) { // emit ve client message = Console.ReadLine(); if (stringComparer.Equals("quit", message)) { _continue = false; } else { //_serialPort.WriteLine( // String.Format("<{0}>: {1}", name, message)); } } readThread.Join(); _serialPort.Close(); }
public HubStateChangeEventArgs(HubConnectionState state, Exception?exception) { State = state; Exception = exception; }
public static partial void AttemptingStateTransition(ILogger logger, HubConnectionState expectedState, HubConnectionState newState);
public static partial void StateTransitionFailed(ILogger logger, HubConnectionState expectedState, HubConnectionState newState, HubConnectionState actualState);