protected virtual void OnTimerPingTick(object state) { if (!_isPingRunning) { _isPingRunning = true; Task.Run(async() => { foreach (var connection in _connectionManager.GetAllConnections()) { try { if (connection.HasBeenPinged) { // Already been pinged, no response, disconnect await SendToConnectionRawAsync("No ping response - disconnected.", connection); await DisconnectConnectionAsync(connection); } else { connection.HasBeenPinged = true; await SendToConnectionRawAsync("Ping", connection); } } catch (Exception ex) { await FireEventAsync(this, new TcpErrorServerEventArgs { Connection = connection, Exception = ex, Message = ex.Message, }); } } _isPingRunning = false; }); } }
public override void Dispose() { foreach (var connection in _connectionManager.GetAllConnections()) { DisconnectConnectionAsync(connection).Wait(); } if (_handler != null) { _handler.ConnectionEvent -= OnConnectionEvent; _handler.MessageEvent -= OnMessageEventAsync; _handler.ErrorEvent -= OnErrorEvent; _handler.ServerEvent -= OnServerEvent; _handler.Dispose(); } base.Dispose(); }