Exemplo n.º 1
0
        private static void FunTest()
        {
            //===============================================================
            ConsoleHelper.WriteLine("回车开始私信测试开始...");
            ConsoleHelper.ReadLine();
            var cc1 = new MessageClient();

            cc1.OnPrivateMessage += Client_OnPrivateMessage;

            var cc2 = new MessageClient();

            cc2.OnPrivateMessage += Client_OnPrivateMessage;

            cc1.Connect();
            cc2.Connect();

            cc1.Login();
            cc2.Login();

            Task.Run(() =>
            {
                while (true)
                {
                    cc1.SendPrivateMsg(cc2.UserToken.ID, "你好呀,cc2!");
                    cc2.SendPrivateMsg(cc1.UserToken.ID, "你好呀,cc1!");
                    Thread.Sleep(500);
                }
            });

            //===============================================================
            ConsoleHelper.WriteLine("回车开始频道测试开始...");
            ConsoleHelper.ReadLine();

            MessageClient client = new MessageClient();

            client.OnChannelMessage += Client_OnChannelMessage;

            ConsoleHelper.WriteLine("客户正在连接...");

            string channelName = "频道测试一";

            client.Connect();

            ConsoleHelper.WriteLine("客户端就绪");

            client.Login();
            ConsoleHelper.WriteLine("客户端登录成功");

            client.Subscribe(channelName);
            ConsoleHelper.WriteLine("客户端订阅成功");

            Task.Run(() =>
            {
                while (true)
                {
                    client.SendChannelMsg(channelName, "hello!");

                    Thread.Sleep(500);
                }
            });

            //===============================================================
            ConsoleHelper.WriteLine("回车开始订阅频道消息...");
            ConsoleHelper.ReadLine();

            List <MessageClient> list = new List <MessageClient>();

            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    var c = new MessageClient();
                    if (i < 10)
                    {
                        c.OnChannelMessage += Client_OnChannelMessage2;
                    }
                    list.Add(c);
                    c.Connect();
                    c.Login();
                    c.Subscribe(channelName);
                }
            });
            //===============================================================

            ConsoleHelper.WriteLine("回车开始群组测试");
            ConsoleHelper.ReadLine();

            cc1.OnGroupMessage += Client_OnGroupMessage;
            cc2.OnGroupMessage += Client_OnGroupMessage;

            var groupName = "萌一号蠢";

            cc1.SendCreateGroup(groupName);
            cc2.SendAddMember(groupName);


            Task.Run(() =>
            {
                while (true)
                {
                    cc1.SendGroupMessage(groupName, "群主广播了!");
                    cc2.SendGroupMessage(groupName, "群主万岁!");
                    Thread.Sleep(100);
                }
            });



            ConsoleHelper.ReadLine();
            ConsoleHelper.WriteLine("The function test has been completed.");
            //===============================================================

            cc2.SendRemoveMember(groupName);
            cc1.SendRemoveGroup(groupName);
            client.Unsubscribe(channelName);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            ConsoleHelper.WriteLine("Message Test");

            ConsoleHelper.WriteLine("服务器正在启动...");

            MessageServer server = new MessageServer();

            server.Start();

            Task.Run(() =>
            {
                while (true)
                {
                    ConsoleHelper.Title = "MessageServer ClientCounts: " + server.ClientCounts;

                    Thread.Sleep(1000);
                }
            });

            ConsoleHelper.WriteLine("服务器就绪");

            //===============================================================

            MessageClient client = new MessageClient();

            client.OnChannelMessage += Client_OnChannelMessage;
            client.OnPrivateMessage += Client_OnPrivateMessage;

            ConsoleHelper.WriteLine("客户正在连接...");

            string channelName = "频道测试一";

            client.ConnectAsync((state) =>
            {
                ConsoleHelper.WriteLine("客户就绪");

                if (state == System.Net.Sockets.SocketError.Success)
                {
                    client.Login();
                    ConsoleHelper.WriteLine("客户登录成功");

                    client.Subscribe(channelName);
                    ConsoleHelper.WriteLine("客户订阅成功");

                    Task.Run(() =>
                    {
                        while (true)
                        {
                            client.SendChannelMsg(channelName, "hello!");

                            Thread.Sleep(60 * 1000);
                        }
                    });


                    ConsoleHelper.WriteLine("客户发送频道消息成功");
                }
            });


            //===============================================================



            ConsoleHelper.ReadLine();
            Console.WriteLine("单机连接测试...");
            List <MessageClient> slist = new List <MessageClient>();

            Task.Run(() =>
            {
                for (int i = 0; i < 50000; i++)
                {
                    var c = new MessageClient();
                    c.ConnectAsync((state) =>
                    {
                        slist.Add(c);
                    });
                    Thread.Sleep(1);
                    if (i > 1000)
                    {
                        Thread.Sleep(10);
                    }
                    if (i > 5000)
                    {
                        Thread.Sleep(100);
                    }
                    if (i > 10000)
                    {
                        Thread.Sleep(1000);
                    }
                }
                Console.WriteLine("单机5W连接就绪...");
            });



            //===============================================================



            ConsoleHelper.ReadLine();
            Console.WriteLine("开始订阅测试...");
            List <MessageClient> list = new List <MessageClient>();

            for (int i = 0; i < 100; i++)
            {
                var c = new MessageClient();
                if (i < 10)
                {
                    c.OnChannelMessage += Client_OnChannelMessage2;
                }
                c.ConnectAsync((state) =>
                {
                    c.Login();
                    c.Subscribe(channelName);
                    list.Add(c);
                });
                Thread.Sleep(10);
            }



            //===============================================================



            ConsoleHelper.ReadLine();
            Console.WriteLine("私信测试");

            var cc1 = new MessageClient();

            cc1.OnPrivateMessage += Client_OnPrivateMessage;

            var cc2 = new MessageClient();

            cc2.OnPrivateMessage += Client_OnPrivateMessage;

            cc1.ConnectAsync((state) =>
            {
                cc1.Login();

                Task.Run(() =>
                {
                    while (true)
                    {
                        if (cc2.Connected)
                        {
                            cc1.SendPrivateMsg(cc2.UserToken.ID, "你好呀,cc2!");
                        }
                        Thread.Sleep(1000);
                    }
                });

                cc1.Subscribe(channelName);

                list.Add(cc1);
            });


            cc2.ConnectAsync((state) =>
            {
                cc2.Login();

                Task.Run(() =>
                {
                    while (true)
                    {
                        if (cc1.Connected)
                        {
                            cc2.SendPrivateMsg(cc1.UserToken.ID, "你好呀,cc1!");
                        }
                        Thread.Sleep(1000);
                    }
                });

                cc2.Subscribe(channelName);

                list.Add(cc2);
            });



            //===============================================================



            ConsoleHelper.ReadLine();
            Console.WriteLine("群组测试");

            cc1.OnGroupMessage += Client_OnGroupMessage;
            cc2.OnGroupMessage += Client_OnGroupMessage;

            var groupName = "萌一号蠢";

            cc1.SendCreateGroup(groupName);
            cc2.SendAddMember(groupName);


            Task.Run(() =>
            {
                while (true)
                {
                    cc1.SendGroupMessage(groupName, "群主广播了!");
                    cc2.SendGroupMessage(groupName, "群主万岁!");
                    Thread.Sleep(100);
                }
            });



            //===============================================================



            ConsoleHelper.ReadLine();

            cc2.SendRemoveMember(groupName);
            cc1.SendRemoveGroup(groupName);
            client.Unsubscribe(channelName);


            Console.WriteLine("测试完成");
            ConsoleHelper.ReadLine();
        }