private static async Task StartAsync(MessagePublisher publisher)
        {
            Console.CancelKeyPress += async(sender, e) =>
            {
                await publisher.DisposeAsync();

                Environment.Exit(0);
            };

            ShowHelp();

            try
            {
                while (true)
                {
                    var argLine = Console.ReadLine();
                    if (argLine == null)
                    {
                        continue;
                    }
                    var args = argLine.Split(' ');

                    if (args.Length == 2 && args[0].Equals("broadcast"))
                    {
                        Console.WriteLine($"broadcast message '{args[1]}'");
                        await publisher.SendMessages(args[0], null, args[1]);
                    }
                    else if (args.Length == 4 && args[0].Equals("send"))
                    {
                        await publisher.SendMessages(args[1], args[2], args[3]);

                        Console.WriteLine($"{args[0]} message '{args[3]}' to '{args[2]}'");
                    }
                    else if (args.Length == 4 && args[0] == "usergroup")
                    {
                        await publisher.ManageUserGroup(args[1], args[2], args[3]);

                        var preposition = args[1] == "add" ? "to" : "from";
                        Console.WriteLine($"{args[1]} user '{args[2]}' {preposition} group '{args[3]}'");
                    }
                    else
                    {
                        Console.WriteLine($"Can't recognize command {argLine}");
                    }
                }
            }
            finally
            {
                await publisher.DisposeAsync();
            }
        }
Beispiel #2
0
        private static async Task StartAsync(MessagePublisher publisher)
        {
            Console.CancelKeyPress += async(sender, e) =>
            {
                await publisher.DisposeAsync();

                Environment.Exit(0);
            };

            ShowHelp();

            try
            {
                while (true)
                {
                    var argLine = Console.ReadLine();
                    if (argLine == null)
                    {
                        continue;
                    }
                    var args = argLine.Split(' ');

                    if (args.Length == 2 && args[0].Equals("broadcast"))
                    {
                        await publisher.SendMessages(args[0], null, args[1]);

                        Console.WriteLine($"broadcast message '{args[1]}'");
                    }
                    else if (args.Length == 4 && args[0].Equals("send"))
                    {
                        await publisher.SendMessages(args[1], args[2], args[3]);

                        Console.WriteLine($"{args[0]} message '{args[3]}' to '{args[2]}'");
                    }
                    else if (args.Length == 4 && args[0] == "usergroup")
                    {
                        await publisher.ManageUserGroup(args[1], args[2], args[3]);

                        var preposition = args[1] == "add" ? "to" : "from";
                        Console.WriteLine($"{args[1]} user '{args[2]}' {preposition} group '{args[3]}'");
                    }
                    else if (args.Length == 3 && args[0] == "close")
                    {
                        await publisher.CloseConnection(args[1], args[2]);

                        Console.WriteLine($"closed connection '{args[1]}' because '{args[2]}'");
                        //If you want client side see the reason, you need to turn on 'EnableDetailedErrors' option during client negotiation.
                    }
                    else if (args.Length == 3 && args[0] == "checkexist")
                    {
                        var exist = await publisher.CheckExist(args[1].ToLowerInvariant(), args[2]);

                        Console.WriteLine(exist ? "exists" : "not exist");
                    }
                    else if (args.Length == 2 && args[0] == "close")
                    {
                        await publisher.CloseConnection(args[1], null);

                        Console.WriteLine("closed");
                    }
                    else
                    {
                        Console.WriteLine($"Can't recognize command {argLine}");
                    }
                    Console.Write("> ");
                }
            }
            finally
            {
                await publisher.DisposeAsync();
            }
        }