Exemple #1
0
        static void Init1()
        {
            ConsoleHelper.WriteLine("WSServer 正在初始化....", ConsoleColor.Green);
            _server            = new WSServer();
            _server.OnMessage += Server_OnMessage;
            _server.Start();
            ConsoleHelper.WriteLine("WSServer 就绪,回车启动客户端", ConsoleColor.Green);

            ConsoleHelper.ReadLine();

            WSClient client = new WSClient();

            client.OnPong         += Client_OnPong;
            client.OnMessage      += Client_OnMessage;
            client.OnError        += Client_OnError;
            client.OnDisconnected += Client_OnDisconnected;

            ConsoleHelper.WriteLine("WSClient 正在连接到服务器...", ConsoleColor.DarkGray);

            var connected = client.Connect();

            if (connected)
            {
                ConsoleHelper.WriteLine("WSClient 连接成功,回车测试消息", ConsoleColor.DarkGray);
                ConsoleHelper.ReadLine();
                //client.Close();
                //ConsoleHelper.ReadLine();


                var loop = true;

                Task.Run(() =>
                {
                    while (loop)
                    {
                        ConsoleHelper.WriteLine("WSClient 正在发送消息...", ConsoleColor.DarkGray);

                        client.Send($"hello world!{DateTime.Now.ToString("HH:mm:ss.fff")}");

                        Thread.Sleep(1000);
                    }
                });

                ConsoleHelper.ReadLine();
                loop = false;
                ConsoleHelper.WriteLine("WSClient 正在ping服务器...", ConsoleColor.DarkGray);
                Thread.Sleep(2000);
                client.Ping();


                ConsoleHelper.ReadLine();
                ConsoleHelper.WriteLine("WSClient 正在断开连接...");
                Thread.Sleep(1000);
                client.Close();
            }
            else
            {
                ConsoleHelper.WriteLine("WSClient 连接失败", ConsoleColor.DarkGray);
            }
        }
Exemple #2
0
        static void Init1()
        {
            WebHost webHost = new WebHost(port: 18080, root: "Html");

            webHost.Start();


            ConsoleHelper.WriteLine("WSServer 正在初始化....", ConsoleColor.Green);
            _server                 = new WSServer();
            _server.OnMessage      += Server_OnMessage;
            _server.OnDisconnected += _server_OnDisconnected;
            _server.Start();
            ConsoleHelper.WriteLine("WSServer 就绪,回车启动客户端", ConsoleColor.Green);

            ConsoleHelper.ReadLine();

            WSClient client = new WSClient();

            client.OnPong         += Client_OnPong;
            client.OnMessage      += Client_OnMessage;
            client.OnError        += Client_OnError;
            client.OnDisconnected += Client_OnDisconnected;

            ConsoleHelper.WriteLine("WSClient 正在连接到服务器...", ConsoleColor.DarkGray);

            var connected = client.Connect();

            if (connected)
            {
                ConsoleHelper.WriteLine("WSClient 连接成功,回车测试消息", ConsoleColor.DarkGray);
                ConsoleHelper.ReadLine();
                //client.Close();
                //ConsoleHelper.ReadLine();


                ConsoleHelper.WriteLine("WSClient 正在发送消息...", ConsoleColor.DarkGray);

                client.Send($"hello world!{DateTime.Now.ToString("HH:mm:ss.fff")}");
                ConsoleHelper.ReadLine();


                ConsoleHelper.WriteLine("WSClient 正在ping服务器...", ConsoleColor.DarkGray);
                client.Ping();


                ConsoleHelper.ReadLine();
                ConsoleHelper.WriteLine("WSClient 正在断开连接...");
                client.Close();

                ConsoleHelper.ReadLine();
            }
            else
            {
                ConsoleHelper.WriteLine("WSClient 连接失败", ConsoleColor.DarkGray);
            }
        }
Exemple #3
0
        static void Init4()
        {
            ConsoleHelper.WriteLine("WSClient 正在连接到WorkMan服务器...", ConsoleColor.DarkGray);

            var url = "ws://123.207.136.134:9010/ajaxchattest";

            WSClient client = new WSClient(url, SubProtocolType.Empty, "http://coolaf.com");

            client.OnPong         += Client_OnPong;
            client.OnMessage      += Client_OnMessage;
            client.OnError        += Client_OnError;
            client.OnDisconnected += Client_OnDisconnected;

            var connected = client.Connect();

            if (connected)
            {
                ConsoleHelper.WriteLine("WSClient 连接成功,回车测试消息", ConsoleColor.DarkGray);
                ConsoleHelper.ReadLine();

                client.Ping();

                ConsoleHelper.WriteLine("WSClient 正在发送消息...", ConsoleColor.DarkGray);

                client.Send($"1111");

                client.Send($"1111");

                client.Send($"1111");

                ConsoleHelper.WriteLine("WSClient 已发送消息", ConsoleColor.DarkGray);



                ConsoleHelper.WriteLine("回车WSClient 断开连接");

                ConsoleHelper.ReadLine();

                client.Close();
            }
            else
            {
                ConsoleHelper.WriteLine("WSClient 连接失败", ConsoleColor.DarkGray);
            }
        }
Exemple #4
0
        static void Init3()
        {
            ConsoleHelper.WriteLine("WSClient 正在连接到WorkMan服务器...", ConsoleColor.DarkGray);

            var url = "ws://120.79.233.58:7272";

            WSClient client = new WSClient(url, SubProtocolType.Json);

            client.OnPong         += Client_OnPong;
            client.OnMessage      += Client_OnMessage;
            client.OnError        += Client_OnError;
            client.OnDisconnected += Client_OnDisconnected;

            var connected = client.Connect();

            if (connected)
            {
                ConsoleHelper.WriteLine("WSClient 连接成功,回车测试消息", ConsoleColor.DarkGray);
                ConsoleHelper.ReadLine();

                client.Ping();

                ConsoleHelper.WriteLine("WSClient 正在发送消息...", ConsoleColor.DarkGray);

                client.Send($"hello world!{DateTime.Now.ToString("HH:mm:ss.fff")}");

                ConsoleHelper.WriteLine("WSClient 已发送消息", ConsoleColor.DarkGray);

                ConsoleHelper.ReadLine();



                ConsoleHelper.WriteLine("回车WSClient 断开连接");

                ConsoleHelper.ReadLine();

                client.Close();

                ConsoleHelper.ReadLine();
            }
            else
            {
                ConsoleHelper.WriteLine("WSClient 连接失败", ConsoleColor.DarkGray);
            }
        }