コード例 #1
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();
                }
        }
コード例 #2
0
        public static void RunWebSocketServer(ChannelProvider provider)
        {
            using (var server = new EchoWebSocketServer())
            {
                switch (provider)
                {
                case ChannelProvider.Libuv:
                    server.StartLibuv(IPAddress.Loopback, 5001);
                    break;

                case ChannelProvider.ManagedSockets:
                    server.StartManagedSockets(IPAddress.Loopback, 5001);
                    break;
                }

                Console.WriteLine($"Running on {server.IP}:{server.Port}...");
                CancellationTokenSource cancel = new CancellationTokenSource();

                bool keepGoing = true, writeLegend = true, writeStatus = true;
                while (keepGoing)
                {
                    if (writeLegend)
                    {
                        Console.WriteLine("c: start client");
                        Console.WriteLine("c ###: start ### clients");
                        Console.WriteLine("b: broadbast from server");
                        Console.WriteLine("b ###: broadcast ### bytes from server");
                        Console.WriteLine("s: send from clients");
                        Console.WriteLine("s ###: send ### bytes from clients");
                        Console.WriteLine("p: ping");
                        Console.WriteLine("l: toggle logging");
                        Console.WriteLine("cls: clear console");
                        Console.WriteLine("xc: close at client");
                        Console.WriteLine("xs: close at server");
                        Console.WriteLine($"bf: toggle {nameof(server.BufferFragments)}");
                        Console.WriteLine("frag: send fragmented message from clients");
                        Console.WriteLine("q: quit");
                        Console.WriteLine("stat: write status");
                        Console.WriteLine("?: help");
                        writeLegend = false;
                    }
                    if (writeStatus)
                    {
                        Console.WriteLine($"clients: {ClientCount}; server connections: {server.ConnectionCount}");
                        writeStatus = false;
                    }

                    var line = Console.ReadLine();
                    switch (line)
                    {
                    case null:
                    case "cls":
                        Console.Clear();
                        break;

                    case "q":
                        keepGoing = false;
                        break;

                    case "stat":
                        writeStatus = true;
                        break;

                    case "?":
                        writeLegend = true;
                        break;

                    case "l":
                        logging = !logging;
                        Console.WriteLine("logging is now " + (logging ? "on" : "off"));
                        break;

                    case "bf":
                        server.BufferFragments = !server.BufferFragments;
                        Console.WriteLine($"{nameof(server.BufferFragments)} is now " + (server.BufferFragments ? "on" : "off"));
                        break;

                    case "xc":
                        CloseAllClients(cancel.Token).ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Closed {t.Result} clients");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    case "xs":
                        server.CloseAllAsync("nuked from orbit").ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Closed {t.Result} connections at the server");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    case "b":
                        server.BroadcastAsync("hello to all clients").ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Broadcast to {t.Result} clients");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    case "s":
                        SendFromClients(cancel.Token).ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Sent from {t.Result} clients");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    case "frag":
                        SendFragmentedFromClients(cancel.Token).ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Sent fragmented from {t.Result} clients");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    case "cc":
                        StartChannelClients();
                        break;

                    case "c":
                        StartClients(cancel.Token);
                        break;

                    case "p":
                        server.PingAsync("ping!").ContinueWith(t =>
                        {
                            try
                            {
                                Console.WriteLine($"Pinged {t.Result} clients");
                            }
                            catch (Exception e)
                            {
                                WriteError(e);
                            }
                        });
                        break;

                    default:
                        Match match;
                        int   i;
                        if ((match = Regex.Match(line, "c ([0-9]+)")).Success && int.TryParse(match.Groups[1].Value, out i) && i.ToString() == match.Groups[1].Value && i >= 1)
                        {
                            StartClients(cancel.Token, i);
                        }
                        else if ((match = Regex.Match(line, "s ([0-9]+)")).Success && int.TryParse(match.Groups[1].Value, out i) && i.ToString() == match.Groups[1].Value && i >= 1)
                        {
                            SendFromClients(cancel.Token, new string('#', i)).FireOrForget();
                        }
                        else if ((match = Regex.Match(line, "b ([0-9]+)")).Success && int.TryParse(match.Groups[1].Value, out i) && i.ToString() == match.Groups[1].Value && i >= 1)
                        {
                            server.BroadcastAsync(new string('#', i)).ContinueWith(t =>
                            {
                                try
                                {
                                    Console.WriteLine($"Broadcast to {t.Result} clients");
                                }
                                catch (Exception e)
                                {
                                    WriteError(e);
                                }
                            });
                        }
                        else
                        {
                            writeLegend = true;
                        }
                        break;
                    }
                }
                Console.WriteLine("Shutting down...");
                cancel.Cancel();
            }
        }