コード例 #1
0
        public async Task ClientServerTest()
        {
            WebSocketListener server = new WebSocketListener(PORT);

            byte[] clientMessage = new byte[] { 100, 101, 102, 103 };
            byte[] serverMessage = new byte[] { 80, 81, 82, 83 };
            byte[] clientBuffer  = new byte[1024];

            server.Start();

            Task task = Task.Run(async() => {
                WebSocketStream socket = await server.AcceptWebSocketAsync();
                byte[] serverBuffer    = new byte[1024];

                int serverRead = await socket.ReadAsync(serverBuffer, 0, serverBuffer.Length);

                Assert.Equal(4, serverRead);
                Assert.Equal(clientMessage, serverBuffer.Take(4).ToArray());

                await socket.WriteAsync(serverMessage, 0, serverMessage.Length);

                serverRead = await socket.ReadAsync(serverBuffer, 0, serverBuffer.Length);

                Assert.Equal(0, serverRead);
            });

            WebSocketStream client = WebSocketStream.Connect($"ws://localhost:{PORT}");

            await client.WriteAsync(clientMessage, 0, clientMessage.Length);

            int clientRead = await client.ReadAsync(clientBuffer, 0, clientBuffer.Length);

            Assert.Equal(4, clientRead);
            Assert.Equal(serverMessage, clientBuffer.Take(4).ToArray());

            await client.CloseAsync();

            client.Dispose();
            server.Dispose();

            await task;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: dnobori/DNT-Jikken
        static async Task test_ssl__()
        {
            string hostname = "echo.websocket.org";
            int    port     = 443;

            using (TcpClient tc = new TcpClient())
            {
                WriteLine("connecting.");
                await tc.ConnectAsync(hostname, port);

                WriteLine("connected.");

                using (NetworkStream st = tc.GetStream())
                {
                    using (SslStream ssl = new SslStream(st, false, check_cert))
                    {
                        WriteLine("start ssl");
                        await ssl.AuthenticateAsClientAsync(hostname);

                        WriteLine("end ssl");

                        using (WebSocketStream s = new WebSocketStream(ssl))
                        {
                            await s.OpenAsync("wss://echo.websocket.org");

                            WriteLine("opened.");

                            while (true)
                            {
                                string hello       = "Hello World";
                                byte[] hello_bytes = hello.AsciiToByteArray();
                                await s.WriteAsync(hello_bytes.AsMemory());

                                WriteLine("Sent.");

                                byte[] recv_buffer = new byte[128];
                                int    recv_ret    = await s.ReadAsync(recv_buffer, 0, recv_buffer.Length);

                                WriteLine($"Recv: \"{recv_buffer.AsSpan(0, recv_ret).ToArray().ByteArrayToAscii()}\"");
                            }

                            Thread.Sleep(-1);
                        }
                    }
                }
            }
        }