Ejemplo n.º 1
0
        public async Task Connect(string name, string uri)
        {
            _close = false;
            //var channel = Channel.CreateBounded<SentMessage>(new BoundedChannelOptions(5) { SingleReader = true, SingleWriter = false });
            _webSocketClient = new WebSocketClient(new Uri(uri), logger: _logger, sender: new SingleQueueSender(), name: name);

            _disposables = new CompositeDisposable();

            BuildStream(_disposables);

            await _webSocketClient.ConnectAsync();

            _webSocketClient.Send(Encoding.UTF8.GetBytes(name));

            // for debug code.
            //_ = Task.Run(async () =>
            //{
            //    int i = 0;
            //    while (!_close)
            //    {
            //        this.Send("continuous sending test." + ++i);
            //        await Task.Delay(TimeSpan.FromMilliseconds(1));
            //    }
            //});
        }
Ejemplo n.º 2
0
        public Task OpenAsync(CancellationToken cancellationToken = default)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(WebSocketConnection));
            }

            return(_socketClient.ConnectAsync(cancellationToken: cancellationToken));
        }
Ejemplo n.º 3
0
        public IObservable <IWsIQClient> Connect(CancellationToken cancellationToken)
        {
            var cookies = new CookieContainer();

            cookies.Add(new Uri("https://iqoption.com"),
                        new Cookie("platform", "9"));
            cookies.Add(new Uri("https://iqoption.com"),
                        new Cookie("platform_version", "1009.13.5397.release"));

            return(_ws.ConnectAsync("wss://iqoption.com/echo/websocket", cookies, cancellationToken)
                   .ToObservable()
                   .FlatMap(Observable.Return(this)));
        }
        internal override async Task ConnectInternalAsync()
        {
            /*if (LoginState != LoginState.LoggedIn)
             *  throw new InvalidOperationException("Client is not logged in.");*/

            ConnectionState = ConnectionState.Connecting;
            try
            {
                _stateCancelToken = new CancellationTokenSource();
                if (_webSocketClient != null)
                {
                    _webSocketClient.SetCancelToken(_stateCancelToken.Token);
                }

                bool   success = false;
                int    port;
                string uuid = Guid.NewGuid().ToString();

                for (port = DiscordRpcConfig.PortRangeStart; port <= DiscordRpcConfig.PortRangeEnd; port++)
                {
                    try
                    {
                        string url = $"wss://{uuid}.discordapp.io:{port}/?v={DiscordRpcConfig.RpcAPIVersion}&client_id={_clientId}";
                        await _webSocketClient.ConnectAsync(url).ConfigureAwait(false);

                        success = true;
                        break;
                    }
                    catch (Exception)
                    {
                    }
                }

                if (!success)
                {
                    throw new Exception("Unable to connect to the RPC server.");
                }

                SetBaseUrl($"https://{uuid}.discordapp.io:{port}/");
                ConnectionState = ConnectionState.Connected;
            }
            catch (Exception)
            {
                await DisconnectInternalAsync().ConfigureAwait(false);

                throw;
            }
        }
Ejemplo n.º 5
0
        public async ValueTask ConnectAsync(Uri url, CancellationToken token)
        {
            ThrowIfDisposed();

            _limboCts?.Cancel();
            _limboCts?.Dispose();
            _limboCts = new Cts();
            _ws?.Dispose();
            _ws = _webSocketClientFactory.CreateClient();
            if (_supportsZLib)
            {
                _receiveZLibStream?.Dispose();
                _receiveZLibStream = _createZLibStream(_receiveStream);
            }
            await _ws.ConnectAsync(url, token).ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        public override async void OnCreate()
        {
            base.OnCreate();
            Log.Error("Service:", "WebSocketService STARTED");

            try {
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    using Notification notification =
                              new NotificationCompat.Builder(this, App.NonStopChannelIdForServices).SetContentTitle("Familia")
                              .SetContentText("Ruleaza in fundal").SetSmallIcon(Resource.Drawable.logo).SetOngoing(true)
                              .Build();

                    StartForeground(App.NonstopNotificationIdForServices, notification);
                }

                //RegisterReceiver(_charger, new IntentFilter(Intent.ActionHeadsetPlug));

                bool ok = int.TryParse(Utils.GetDefaults("UserType"), out int type);
                Log.Error("ok", ok.ToString());
                Log.Error("type", type.ToString());
                if (ok)
                {
                    if (type != 2)
                    {
                        await _socketClient.ConnectAsync(Constants.WebSocketAddress, Constants.WebSocketPort, this);
                    }
                }

                _webSocketLocation.Connect(Constants.WebSocketLocationAddress, Constants.WebSocketPort, this);
                await Task.Run(async() => {
                    try {
                        string result = await GetData();
                        if (result == "{}")
                        {
                            throw new Exception("Error geting configuration from GIS");
                        }
                        Utils.SetDefaults("IdPersoana", new JSONObject(result).GetInt("idPersoana").ToString());
                    } catch (Exception ex) {
                        Log.Error("Configuration Error", ex.Message);
                    }
                });
            } catch (Exception e) {
                Console.WriteLine(e);
                //throw;
            }
        }
        public async Task RunAsync()
        {
            try
            {
                if (await _socketClient.ConnectAsync(_arguments.Port))
                {
                    await SendAsync(new RegisterPluginMessage(_arguments.UUID));

                    await _socketClient.ReceiveAsync();
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                await _socketClient.DisconnectAsync();
            }
        }
Ejemplo n.º 8
0
        private async Task ConnectInternalAsync(string url)
        {
            ConnectionState = ConnectionState.Connecting;
            try
            {
                _connectCancelToken = new CancellationTokenSource();
                _webSocketClient.SetCancelToken(_connectCancelToken.Token);
                await _webSocketClient.ConnectAsync(url).ConfigureAwait(false);

                _udpRecieveTask = ReceiveAsync(_connectCancelToken.Token);

                ConnectionState = ConnectionState.Connected;
            }
            catch (Exception)
            {
                await DisconnectInternalAsync().ConfigureAwait(false);

                throw;
            }
        }
Ejemplo n.º 9
0
        public void ConnectAsync(IClientParams pParams)
        {
            if (!(pParams is ClientParams objParams))
            {
                Log.Error("Incorrect parameters for WebSocket client");
                throw new Exception("Incorrect parameters for WebSocket client");
            }
            Log.Info($"Connecting {ID} to {objParams.HostName}:{objParams.Port}");

            _objWebSocketClient.ConnectionTimeoutSeconds = objParams.ConnectionTimeoutSeconds;
            _objKeepAliveMonitor.SetInterval(objParams.ConnectionTimeoutSeconds);

            _objWebSocketClient.OnOpen    += Connection_OnOpen;
            _objWebSocketClient.OnClose   += Connection_OnClose;
            _objWebSocketClient.OnError   += Connection_OnError;
            _objWebSocketClient.OnMessage += Connection_OnMessage;

            _objWebSocketClient.ConnectAsync(objParams);

            _objKeepAliveMonitor.TimeOut      += _objKeepAliveMonitor_TimeOut;
            _objKeepAliveMonitor.UnResponsive += _objKeepAliveMonitor_UnResponsive;
        }
Ejemplo n.º 10
0
        public async ValueTask ConnectAsync(Uri url, CancellationToken token)
        {
            ThrowIfDisposed();

            _limboCts?.Cancel();
            _limboCts?.Dispose();
            _limboCts = new Cts();
            _ws?.Dispose();
            _ws = _webSocketClientFactory.CreateClient();
            if (_supportsZLib)
            {
                _receiveZLibStream?.Dispose();
                _receiveZLibStream =
#if NET5_0
                    CreateZLibStream(_receiveStream);
#else
                    new ZLibStream(_receiveStream, CompressionMode.Decompress, true);
#endif
            }

            await _ws.ConnectAsync(url, token).ConfigureAwait(false);
        }
Ejemplo n.º 11
0
        internal override async Task ConnectInternalAsync()
        {
            if (LoginState != LoginState.LoggedIn)
            {
                throw new InvalidOperationException("You must log in before connecting.");
            }
            if (_gatewayClient == null)
            {
                throw new NotSupportedException("This client is not configured with websocket support.");
            }

            ConnectionState = ConnectionState.Connecting;
            try
            {
                _connectCancelToken = new CancellationTokenSource();
                if (_gatewayClient != null)
                {
                    _gatewayClient.SetCancelToken(_connectCancelToken.Token);
                }

                if (_gatewayUrl == null)
                {
                    var gatewayResponse = await GetGatewayAsync().ConfigureAwait(false);

                    _gatewayUrl = $"{gatewayResponse.Url}?v={DiscordConfig.APIVersion}&encoding={DiscordSocketConfig.GatewayEncoding}";
                }
                await _gatewayClient.ConnectAsync(_gatewayUrl).ConfigureAwait(false);

                ConnectionState = ConnectionState.Connected;
            }
            catch (Exception)
            {
                _gatewayUrl = null; //Uncache  in case the gateway url changed
                await DisconnectInternalAsync().ConfigureAwait(false);

                throw;
            }
        }
Ejemplo n.º 12
0
 protected override Task ConnectAsync(Uri uri)
 {
     return(_client.ConnectAsync(uri));
 }