示例#1
0
        public StreamFlowDriverSignalR(ISignalRService signalRService)
        {
            SignalRService = signalRService;

            SignalRService.Connection.Reconnected  += async(e) => OnReconnected?.Invoke();
            SignalRService.Connection.Reconnecting += async(e) => OnReconnecting?.Invoke();
            SignalRService.Connection.Closed       += async(e) => OnDisconnected?.Invoke();
        }
 public virtual async Task Disconnect()
 {
     while (_ws?.State == WebSocketState.Open)
     {
         await _ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
     }
     OnReconnecting?.Invoke();
     _ws?.Dispose();
 }
示例#3
0
        private bool ConnectToServer()
        {
            //建立IPEndPoint
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(_ip), _port);

            TcpClient = new TcpClient();

            //開始連線
            try
            {
                TcpClient.Connect(ipe);
                if (TcpClient.Connected)
                {
                    if (_keepAlive)   // 保持連線
                    {
                        TcpClient.Client.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 1000, 1000), null);
                    }
                    Manager = factory.CreateManager(TcpClient);
                    OnConnected?.Invoke(this, Manager);
                    _manualClose = false;
                    Task.Run(() =>
                    {
                        Manager.Communicate();
                        Manager = null;
                        while (!_manualClose && AutoReconnect)
                        {
                            Thread.Sleep(AutoReconnectDelay);
                            OnReconnecting?.Invoke(this, EventArgs.Empty);
                            if (ConnectToServer())
                            {
                                OnReconnected(this, Manager);
                                break;
                            }
                            OnReconnectError?.Invoke(this, EventArgs.Empty);
                        }
                    }).ConfigureAwait(false);
                    return(true);
                }
            }
            catch (Exception)
            {
                TcpClient.Close();
            }
            OnConnectError?.Invoke(this, EventArgs.Empty);
            return(false);
        }
        private async void SocketIO_OnDisconnected(object sender, string e)
        {
            if (_options.Reconnection)
            {
                double delayDouble = _options.ReconnectionDelay;
                int    attempt     = 0;
                while (true)
                {
                    int delay = (int)delayDouble;
                    Trace.WriteLine($"{DateTime.Now} Reconnection wait {delay} ms");
                    await Task.Delay(delay);

                    Trace.WriteLine($"{DateTime.Now} Delay done");
                    try
                    {
                        if (!Connected && Disconnected)
                        {
                            OnReconnecting?.Invoke(this, ++attempt);
                            await ConnectAsync();
                        }
                        break;
                    }
                    catch (Exception ex)
                    {
                        if (ex is TaskCanceledException || ex is System.Net.WebSockets.WebSocketException)
                        {
                            if (delayDouble < _options.ReconnectionDelayMax)
                            {
                                delayDouble += 2 * _options.RandomizationFactor;
                            }
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
            }
        }
示例#5
0
 public void RaiseOnReconnecting()
 {
     OnReconnecting?.Invoke();
 }
示例#6
0
 public Task Disconnect()
 {
     OnReconnecting?.Invoke();
     _subscription?.Dispose();
     return(Task.CompletedTask);
 }
示例#7
0
 public void InvokeReconnecting()
 {
     OnReconnecting?.Invoke("");
 }