Ejemplo n.º 1
0
        public void Connect_ServerExists_MessageEchoed()
        {
            // Arrange
            EchoWebSocketServer server = new EchoWebSocketServer();

            server.StartServer("0.0.0.0", 4040);

            WebSocketService service = new WebSocketService();

            string messageToSend   = "hello";
            string receivedMessage = string.Empty;

            service.MessageReceived += (message) =>
            {
                receivedMessage = message;
            };

            // Act
            service.Connect("ws://localhost:4040");
            service.Send(messageToSend);

            Thread.Sleep(100);

            // Assert
            Assert.Equal(messageToSend, receivedMessage);

            // Clean up
            server.Stop();
        }
Ejemplo n.º 2
0
        private static void RunBasicEchoServer()
        {
            using (var server = new EchoWebSocketServer())
                using (var client = new TrivialClient())
                {
                    var endpoint = new IPEndPoint(IPAddress.Loopback, 5002);
                    Console.WriteLine($"Starting server on {endpoint}...");
                    server.StartManagedSockets(IPAddress.Loopback, 5002);
                    Console.WriteLine($"Server running");

                    Thread.Sleep(1000); // let things spin up

                    Console.WriteLine($"Opening client to {endpoint}...");
                    client.ConnectAsync(endpoint).FireOrForget();
                    Console.WriteLine("Client connected");

                    Console.WriteLine("Write data to echo, or 'quit' to exit,");
                    Console.WriteLine("'killc' to kill from the client, 'kills' to kill from the server, 'kill' for both");
                    while (true)
                    {
                        var line = Console.ReadLine();
                        if (line == null || line == "quit")
                        {
                            break;
                        }
                        switch (line)
                        {
                        case "kill":
                            server.CloseAllAsync();
                            client.Close();
                            break;

                        case "killc":
                            client.Close();
                            break;

                        case "kills":
                            server.CloseAllAsync();
                            break;

                        default:
                            client.SendAsync(line).FireOrForget();
                            break;
                        }
                    }
                    server.Stop();
                }
        }