Exemplo n.º 1
0
        private static async void StartWebSocketAsync()
        {
            using (var websocketClient = new MessageWebSocketRx())
            {
                System.Console.WriteLine("Start");

                var websocketLoggerSubscriber = websocketClient.ObserveConnectionStatus.Subscribe(
                    s =>
                {
                    System.Console.WriteLine(s.ToString());
                });

                _subscribeToMessagesReceived = websocketClient.ObserveTextMessagesReceived.Subscribe(
                    msg =>
                {
                    System.Console.WriteLine($"Reply from test server: {msg}");
                },
                    () =>
                {
                    System.Console.WriteLine($"Subscription Completed");
                });

                // ### Optional Subprotocols ###
                // The echo.websocket.org does not support any sub-protocols and hence this test does not add any.
                // Adding a sub-protocol that the server does not support causes the client to close down the connection.
                List <string> subprotocols = null; //new List<string> {"soap", "json"};


                //await websocketClient.ConnectAsync(
                //    new Uri("ws://192.168.0.7:3000/socket.io/?EIO=2&transport=websocket"),
                //    //new Uri("wss://echo.websocket.org:443"),
                //    //cts,
                //    origin: null,
                //    ignoreServerCertificateErrors: true,
                //    subprotocols: subprotocols,
                //    tlsProtocolVersion: TlsProtocolVersion.Tls12);

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


                //await websocketClient.CloseAsync();


                await websocketClient.ConnectAsync(
                    //new Uri("ws://localhost:3000/socket.io/?EIO=2&transport=websocket"),
                    new Uri("wss://echo.websocket.org:443"),
                    //cts,
                    ignoreServerCertificateErrors : true,
                    subprotocols : subprotocols,
                    tlsProtocolVersion : TlsProtocolVersion.Tls12);

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


                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);

                //await websocketClient.CloseAsync();

                //await websocketClient.ConnectAsync(
                //    new Uri("ws://192.168.0.7:3000/socket.io/?EIO=2&transport=websocket"),
                //    //new Uri("wss://echo.websocket.org:443"),
                //    //cts,
                //    ignoreServerCertificateErrors: true,
                //    subprotocols: subprotocols,
                //    tlsProtocolVersion: TlsProtocolVersion.Tls12);

                //await websocketClient.SendTextAsync("Test localhost");

                //await websocketClient.CloseAsync();
            }
        }
Exemplo n.º 2
0
    private static async Task StartWebSocketAsync(CancellationTokenSource innerCancellationTokenSource)
    {
        using (var tcpClient = new TcpClient {
            LingerState = new LingerOption(true, 0)
        })
            using (var websocketClient = new MessageWebSocketRx(tcpClient)
            {
                IgnoreServerCertificateErrors = true,
                Headers = new Dictionary <string, string> {
                    { "Pragma", "no-cache" }, { "Cache-Control", "no-cache" }
                },
                TlsProtocolType = SslProtocols.Tls12,
            })
            {
                //websocketClient.ExcludeZeroApplicationDataInPong = false;
                Console.WriteLine("Start");

                var disposableWebsocketStatus = websocketClient.ConnectionStatusObservable.Subscribe(
                    s =>
                {
                    System.Console.WriteLine(s.ToString());
                    if (s == ConnectionStatus.Disconnected ||
                        s == ConnectionStatus.Aborted ||
                        s == ConnectionStatus.ConnectionFailed)
                    {
                        innerCancellationTokenSource.Cancel();
                    }
                },
                    ex =>
                {
                    Console.WriteLine($"Connection status error: {ex}.");
                    innerCancellationTokenSource.Cancel();
                },
                    () =>
                {
                    Console.WriteLine($"Connection status completed.");
                    innerCancellationTokenSource.Cancel();
                });

                var disposableMessageReceiver = websocketClient.MessageReceiverObservable.Subscribe(
                    msg =>
                {
                    Console.WriteLine($"Reply from test server: {msg}");
                },
                    ex =>
                {
                    Console.WriteLine(ex.Message);
                    innerCancellationTokenSource.Cancel();
                },
                    () =>
                {
                    System.Console.WriteLine($"Message listener subscription Completed");
                    innerCancellationTokenSource.Cancel();
                });


                await websocketClient.ConnectAsync(new Uri("wss://echo.websocket.org"));


                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);

                    await websocketClient.DisconnectAsync();

                    disposableMessageReceiver.Dispose();
                    disposableWebsocketStatus.Dispose();

                    await Task.Delay(TimeSpan.FromDays(1));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    innerCancellationTokenSource.Cancel();
                }
            }
    }
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();
            }
        }
    }