Exemplo n.º 1
0
        public async Task ConnectAsync()
        {
            _initTcs = new TaskCompletionSource <bool>();
            IObservable <string> messageObserver = await _socket.CreateObservableMessageReceiver(_uri,
                                                                                                 ignoreServerCertificateErrors : true);

            _messageSubscription = messageObserver.Subscribe(msg => HandleMessage(JObject.Parse(msg)));
            await _initTcs.Task;
        }
Exemplo n.º 2
0
        private async Task connectToServer()
        {
            List <string> subProtocols = new List <string> {
                "json"
            };

            Dictionary <string, string> headers = new Dictionary <string, string> {
                { "Pragma", "no-cache" }, { "Cache-Control", "no-cache" }
            };

            client = new MessageWebSocketRx();

            observer = await client.CreateObservableMessageReceiver(new Uri("wss://api2.poloniex.com:443"), ignoreServerCertificateErrors : false, headers : headers, subProtocols : subProtocols);

            subscribe = observer.Subscribe(convert, ex => Console.WriteLine(ex.Message), () => Console.WriteLine("Subscription Closed"));
        }
Exemplo n.º 3
0
    private static async Task StartWebSocketAsync(CancellationTokenSource innerCancellationTokenSource)
    {
        using (var websocketClient = new MessageWebSocketRx())
        {
            System.Console.WriteLine("Start");

            var websocketLoggerSubscriber = websocketClient.ObserveConnectionStatus.Subscribe(
                s =>
            {
                System.Console.WriteLine(s.ToString());
                if (s == ConnectionStatus.Disconnected ||
                    s == ConnectionStatus.Aborted ||
                    s == ConnectionStatus.ConnectionFailed)
                {
                    innerCancellationTokenSource.Cancel();
                }
            },
                ex =>
            {
                innerCancellationTokenSource.Cancel();
            },
                () =>
            {
                innerCancellationTokenSource.Cancel();
            });

            List <string> subprotocols = null; //new List<string> {"soap", "json"};

            var headers = new Dictionary <string, string> {
                { "Pragma", "no-cache" }, { "Cache-Control", "no-cache" }
            };

            var messageObserver = await websocketClient.CreateObservableMessageReceiver(
                new Uri("wss://echo.websocket.org:443"),
                ignoreServerCertificateErrors : true,
                headers : headers,
                subProtocols : subprotocols,
                tlsProtocolType : TlsProtocolVersion.Tls12);

            var subscribeToMessagesReceived = messageObserver.Subscribe(
                msg =>
            {
                System.Console.WriteLine($"Reply from test server: {msg}");
            },
                ex =>
            {
                System.Console.WriteLine(ex.Message);
                innerCancellationTokenSource.Cancel();
            },
                () =>
            {
                System.Console.WriteLine($"Subscription Completed");
                innerCancellationTokenSource.Cancel();
            });

            try
            {
                System.Console.WriteLine("Sending: Test Single Frame");
                await websocketClient.SendTextAsync("Test Single Frame");

                await websocketClient.SendTextAsync("Test Single Frame again");

                await websocketClient.SendTextAsync(TestString(65538, 65550));

                var strArray = new[] { "Test ", "multiple ", "frames" };

                await websocketClient.SendTextAsync(strArray);

                await websocketClient.SendTextMultiFrameAsync("Start ", FrameType.FirstOfMultipleFrames);

                await Task.Delay(TimeSpan.FromMilliseconds(200));

                await websocketClient.SendTextMultiFrameAsync("Continue... #1 ", FrameType.Continuation);

                await Task.Delay(TimeSpan.FromMilliseconds(300));

                await websocketClient.SendTextMultiFrameAsync("Continue... #2 ", FrameType.Continuation);

                await Task.Delay(TimeSpan.FromMilliseconds(150));

                await websocketClient.SendTextMultiFrameAsync("Continue... #3 ", FrameType.Continuation);

                await Task.Delay(TimeSpan.FromMilliseconds(400));

                await websocketClient.SendTextMultiFrameAsync("Stop.", FrameType.LastInMultipleFrames);

                // Close the Websocket connection gracefully telling the server goodbye
                await websocketClient.CloseAsync();

                subscribeToMessagesReceived.Dispose();
                websocketLoggerSubscriber.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                innerCancellationTokenSource.Cancel();
            }
        }
    }