public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
        {
            if (_ws != null)
            {
                _ws.Dispose();
            }

            //ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) =>
            //{
            //    //// local dev, just approve all certs
            //    //if (development) return true;
            //    //return errors == SslPolicyErrors.None;
            //    return true;
            //};
            _ws = CreateClient();
            //var cert = new X509Certificate2(@"C:\Users\41608\Downloads\cert\client1-crt.pem");
            //var privateKey = cert.PrivateKey as RSACryptoServiceProvider;
            //privateKey.en
            //_ws.Options.ClientCertificates.Add(cert);
            _connectionToken = new CancellationTokenSource();
            await _ws.ConnectAsync(uri, _connectionToken.Token);

            await Task.Factory.StartNew(ListenAsync, _connectionToken.Token);

            await Task.Factory.StartNew(ListenStateAsync, _connectionToken.Token);
        }
Exemplo n.º 2
0
        public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
        {
            _ws = new System.Net.WebSockets.ClientWebSocket();
            _connectionToken = new CancellationTokenSource();
            await _ws.ConnectAsync(uri, _connectionToken.Token);

            await Task.Factory.StartNew(ListenAsync, _connectionToken.Token);

            await Task.Factory.StartNew(ListenStateAsync, _connectionToken.Token);
        }
Exemplo n.º 3
0
        public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
        {
            _client = new WebsocketClient(uri)
            {
                IsReconnectionEnabled = false,
                ReconnectTimeout      = options.ConnectionTimeout
            };
            // _client.MessageReceived.Subscribe()
            _client.MessageReceived.Subscribe(_messageReceivedObserver);
            _client.DisconnectionHappened.Subscribe(_disconnectionHappened);

            await _client.Start();
        }
Exemplo n.º 4
0
        public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
        {
            if (_ws != null)
            {
                _ws.Dispose();
            }
            _ws = new System.Net.WebSockets.ClientWebSocket();
            //var cert = new X509Certificate2(@"C:\Users\41608\Downloads\cert\client1-crt.pem");
            //var privateKey = cert.PrivateKey as RSACryptoServiceProvider;
            //privateKey.en
            //_ws.Options.ClientCertificates.Add(cert);
            _connectionToken = new CancellationTokenSource();
            await _ws.ConnectAsync(uri, _connectionToken.Token);

            await Task.Factory.StartNew(ListenAsync, _connectionToken.Token);

            await Task.Factory.StartNew(ListenStateAsync, _connectionToken.Token);
        }
        public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
        {
            _client = new WebsocketClient(uri)
            {
                IsReconnectionEnabled = false,
                ReconnectTimeout      = options.ConnectionTimeout
            };

            _client.MessageReceived.Subscribe(message =>
            {
                if (message.MessageType == WebSocketMessageType.Text)
                {
#if DEBUG
                    Trace.WriteLine($"⬇ {DateTime.Now} {message.Text}");
#endif
                    _parser.Unpack(message.Text);
                }
                else if (message.MessageType == WebSocketMessageType.Binary)
                {
#if DEBUG
                    Trace.WriteLine($"⬇ {DateTime.Now} {JsonConvert.SerializeObject(message.Binary)}");
#endif
                    _io.InvokeBytesReceived(message.Binary.Skip(1).ToArray());
                }
            });

            _client.DisconnectionHappened.Subscribe(disconnectionInfo =>
            {
                string reason = null;
                switch (disconnectionInfo.Type)
                {
                case DisconnectionType.Exit:
                    break;

                case DisconnectionType.Lost:
                    reason = "transport close";
                    break;

                case DisconnectionType.NoMessageReceived:
                    break;

                case DisconnectionType.Error:
                    break;

                case DisconnectionType.ByUser:
                    break;

                case DisconnectionType.ByServer:
                    break;

                default:
                    break;
                }
                if (reason != null)
                {
                    _io.InvokeDisconnect(reason);
                }
            });

            await _client.Start();
        }