Esempio n. 1
0
 public void Connect()
 {
     ConnectionFailed = false;
     Task.Run(() => {
         try
         {
             if (Ip == null)
             {
                 return;
             }
             OnConnecting?.Invoke(this, null);
             client?.Dispose();
             client = new TcpClient(Ip.ToString(), Port)
             {
                 NoDelay = true
             };
             nwStream = client.GetStream();
             if (TurnOnWhenConnected)
             {
                 Turn(true);
             }
             OnConnect?.Invoke(this, null);
         }
         catch (Exception)
         {
             ConnectionFailed = true;
             OnConnectFail?.Invoke(this, null);
         }
     });
 }
Esempio n. 2
0
        private IObservable <string> ConnectToServer()
        {
            return(Observable.Create <string>(async o =>
            {
                try
                {
                    OnConnecting?.Invoke(this, new EventArgs());
                    await _wsConnection.ConnectAsync(WebSocketUrl, CancellationToken.None).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    string error = ex.Message;

                    WebSocketException wex = FetchSocketException(ex);
                    if (wex == null && ex.InnerException != null)
                    {
                        error = $"{ex.InnerException.Message}";
                    }
                    else if (wex != null)
                    {
                        error = $"{wex.Message}";
                    }

                    OnConnectFail?.Invoke(this, new EventArgs());

                    Thread.Sleep(RetryInterval);

                    o.OnError(ex);
                }

                return Disposable.Empty;
            }));
        }