Esempio n. 1
0
        private void Reconnect(object state)
        {
            lock (_sync)
            {
                try
                {
                    var now = DateTimeOffset.Now;

                    if (!IsConnected)
                    {
                        if ((now - _lastConnected).TotalSeconds > 5)
                        {
                            Start(true);
                        }
                    }
                    else if (_lastPing > _lastPong && (now - _lastPong).TotalSeconds > 20)
                    {
                        MainSocket?.Close();
                    }
                    else if ((now - _lastPing).TotalSeconds > 5)
                    {
                        _lastPing = DateTimeOffset.Now;
                        MainSocket.C2S_KeepAlive();
                    }
                }
                catch { }
                finally
                {
                    _reconnectTimer.Change(100, Timeout.Infinite);
                }
            }
        }