Esempio n. 1
0
        private static void PreesureTest()
        {
            ConsoleHelper.WriteLine("回车开始连接测试...");
            ConsoleHelper.ReadLine();

            var count = 60 * 1000;

            Sockets.Core.StressTestingClient stressTestingClient;
            Task.Run(() =>
            {
                ConsoleHelper.WriteLine("单机连接测试正在初始化...");

                stressTestingClient = new Sockets.Core.StressTestingClient(count, "127.0.0.1");

                ConsoleHelper.WriteLine("单机连接测试初始化完成,正在建立连接...");

                Task.Run(() =>
                {
                    while (stressTestingClient.Connections < count)
                    {
                        Thread.Sleep(1000);
                        ConsoleHelper.WriteLine($"单机连接测试已建立连接:{stressTestingClient.Connections} 未连接:{stressTestingClient.Lost}");
                    }
                    ConsoleHelper.WriteLine($"单机{count}连接已完成!");
                });

                stressTestingClient.Connect();
            });
        }
Esempio 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();
            ConsoleHelper.WriteLine("单机连接测试...");

            var count = 60 * 1000;

            Sockets.Core.StressTestingClient stressTestingClient;
            Task.Run(() =>
            {
                ConsoleHelper.WriteLine("单机连接测试正在初始化...");

                stressTestingClient = new Sockets.Core.StressTestingClient(count);

                ConsoleHelper.WriteLine("单机连接测试初始化完成,正在建立连接...");

                Task.Run(() =>
                {
                    while (stressTestingClient.Connections < count)
                    {
                        Thread.Sleep(1000);
                        ConsoleHelper.WriteLine($"单机连接测试已建立连接:{stressTestingClient.Connections} 未连接:{stressTestingClient.Lost}");
                    }
                    ConsoleHelper.WriteLine($"单机{count}连接已完成!");
                });

                stressTestingClient.Connect();
            });



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



            ConsoleHelper.ReadLine();
            ConsoleHelper.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();
            ConsoleHelper.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();
            ConsoleHelper.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);


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