Esempio n. 1
0
        static async Task Main(string[] args)
        {
            var theServer = await SocketBuilderFactory.GetUdpSocketBuilder(6003)
                            .OnClose(server =>
            {
                Console.WriteLine($"服务端关闭");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"服务端异常:{ex.Message}");
            })
                            .OnRecieve((server, point, bytes) =>
            {
                Console.WriteLine($"服务端:收到来自[{point.ToString()}]数据:{Encoding.UTF8.GetString(bytes)}");
                server.Send(bytes, point);
            })
                            .OnSend((server, point, bytes) =>
            {
                Console.WriteLine($"服务端发送数据:目标[{point.ToString()}]数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnStarted(server =>
            {
                Console.WriteLine($"服务端启动");
            }).BuildAsync();

            Console.ReadLine();
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            var theClient = await SocketBuilderFactory.GetTcpSocketClientBuilder("127.0.0.1", 6001)
                            .SetLengthFieldEncoder(2)
                            .SetLengthFieldDecoder(ushort.MaxValue, 0, 2, 0, 2)
                            .OnClientStarted(client =>
            {
                Console.WriteLine($"客户端启动");
            })
                            .OnClientClose(client =>
            {
                Console.WriteLine($"客户端关闭");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"异常:{ex.Message}");
            })
                            .OnRecieve((client, bytes) =>
            {
                Console.WriteLine($"客户端:收到数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnSend((client, bytes) =>
            {
                Console.WriteLine($"客户端:发送数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .BuildAsync();

            while (true)
            {
                await theClient.Send(Guid.NewGuid().ToString());

                await Task.Delay(1000);
            }
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            var theServer = await SocketBuilderFactory.GetTcpSocketServerBuilder(6001)
                            .SetLengthFieldEncoder(2)
                            .SetLengthFieldDecoder(ushort.MaxValue, 0, 2, 0, 2)
                            .OnConnectionClose((server, connection) =>
            {
                Console.WriteLine($"连接关闭,连接名[{connection.ConnectionName}],当前连接数:{server.GetConnectionCount()}");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"服务端异常:{ex.Message}");
            })
                            .OnNewConnection((server, connection) =>
            {
                connection.ConnectionName = $"名字{connection.ConnectionId}";
                Console.WriteLine($"新的连接[{connection.ClientAddress.Address.MapToIPv4().ToString()}]:{connection.ConnectionName},当前连接数:{server.GetConnectionCount()}");
            })
                            .OnRecieve((server, connection, bytes) =>
            {
                Console.WriteLine($"服务端:数据{Encoding.UTF8.GetString(bytes)}");
                connection.Send(bytes);
            })
                            .OnSend((server, connection, bytes) =>
            {
                Console.WriteLine($"向连接名[{connection.ConnectionName}]发送数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnServerStarted(server =>
            {
                Console.WriteLine($"服务启动");
            }).BuildAsync();

            Console.ReadLine();
        }
Esempio n. 4
0
        static async Task Main(string[] args)
        {
            var theClient = await SocketBuilderFactory.GetWebSocketClientBuilder("127.0.0.1", 6002)
                            .OnClientStarted(client =>
            {
                Console.WriteLine($"客户端启动");
            })
                            .OnClientClose(client =>
            {
                Console.WriteLine($"客户端关闭");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"异常:{ex.Message}");
            })
                            .OnRecieve((client, msg) =>
            {
                Console.WriteLine($"客户端:收到数据:{msg}");
            })
                            .OnSend((client, msg) =>
            {
                Console.WriteLine($"客户端:发送数据:{msg}");
            })
                            .BuildAsync();

            while (true)
            {
                await theClient.Send(Guid.NewGuid().ToString());

                await Task.Delay(1000);
            }
        }
Esempio n. 5
0
        static async Task Main(string[] args)
        {
            var theClient = await SocketBuilderFactory.GetUdpSocketBuilder()
                            .OnClose(server =>
            {
                Console.WriteLine($"客户端关闭");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"客户端异常:{ex.Message}");
            })
                            .OnRecieve((server, point, bytes) =>
            {
                Console.WriteLine($"客户端:收到来自[{point.ToString()}]数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnSend((server, point, bytes) =>
            {
                Console.WriteLine($"客户端发送数据:目标[{point.ToString()}]数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnStarted(server =>
            {
                Console.WriteLine($"客户端启动");
            }).BuildAsync();

            while (true)
            {
                await theClient.Send(Guid.NewGuid().ToString(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6003));

                await Task.Delay(1000);
            }
        }
Esempio n. 6
0
        static async Task Main(string[] args)
        {
            var theServer = await SocketBuilderFactory.GetWebSocketServerBuilder(6002)
                            .OnConnectionClose((server, connection) =>
            {
                Console.WriteLine($"连接关闭,连接名[{connection.ConnectionName}],当前连接数:{server.GetConnectionCount()}");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"服务端异常:{ex.Message}");
            })
                            .OnNewConnection((server, connection) =>
            {
                connection.ConnectionName = $"名字{connection.ConnectionId}";
                Console.WriteLine($"新的连接:{connection.ConnectionName},当前连接数:{server.GetConnectionCount()}");
            })
                            .OnRecieve((server, connection, msg) =>
            {
                Console.WriteLine($"服务端:数据{msg}");
                connection.Send(msg);
            })
                            .OnSend((server, connection, msg) =>
            {
                Console.WriteLine($"向连接名[{connection.ConnectionName}]发送数据:{msg}");
            })
                            .OnServerStarted(server =>
            {
                Console.WriteLine($"服务启动");
            }).BuildAsync();

            Console.ReadLine();
        }
Esempio n. 7
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            theClient = await SocketBuilderFactory.GetTcpSocketClientBuilder(Host, Port)
                        .SetLengthFieldEncoder(4)
                        .SetLengthFieldDecoder(int.MaxValue, 0, 4, 0, 4)
                        .OnClientClose(server =>
            {
                Console.WriteLine($"客户端关闭");
            })
                        .OnException(ex =>
            {
                Console.WriteLine($"客户端异常:{ex.Message}");
            })
                        .OnRecieve((server, bytes) =>
            {
                var res = new LineResponse(bytes);
                if (res.FuncName == "Health")
                {
                    Console.WriteLine($"{DateTime.Now} 心跳包返回");
                }
                else if (res.FuncName == "Login")
                {
                    Console.WriteLine($"{DateTime.Now} 登陆包返回 \r\n----------------------------\r\n");
                }
                else if (res.FuncName == "GetInitStationInfo")
                {
                    Console.WriteLine($"{DateTime.Now} 站点初始化返回 \r\n----------------------------\r\n");
                }
                else
                {
                    Console.WriteLine($"{DateTime.Now} 服务器返回:{JsonConvert.SerializeObject(res, Formatting.Indented)}");
                }
            })
                        .OnClientStarted(server =>
            {
                Console.WriteLine($"客户端启动");
            }).BuildAsync();

            await theClient.Send(GetLogin());

            Task.Run(async() => {
                await RunHealth();
            });

            Console.ReadLine();
            await CallALL();

            await Task.Delay(-1);
        }
Esempio n. 8
0
        static async Task Main(string[] args)
        {
            var theClient = await SocketBuilderFactory.GetTcpSocketClientBuilder("127.0.0.1", 6001)
                            .SetLengthFieldEncoder(2)
                            .SetLengthFieldDecoder(ushort.MaxValue, 0, 2, 0, 2)
                            .OnClientStarted(client =>
            {
                Console.WriteLine($"客户端启动");
            })
                            .OnClientClose(client =>
            {
                Console.WriteLine($"客户端关闭");
            })
                            .OnException(ex =>
            {
                Console.WriteLine($"异常:{ex.Message}");
            })
                            .OnRecieve((client, bytes) =>
            {
                Console.WriteLine($"客户端:收到数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .OnSend((client, bytes) =>
            {
                Console.WriteLine($"客户端:发送数据:{Encoding.UTF8.GetString(bytes)}");
            })
                            .BuildAsync();

            bool flag = true;

            while (flag)
            {
                string str = Console.ReadLine();
                if (str == "ok")
                {
                    //await theClient.Send(Guid.NewGuid().ToString());

                    byte[] bytes = BitConverter.GetBytes(2);

                    await theClient.Send(bytes);

                    await Task.Delay(1000);
                }
                else if (str == "exit")
                {
                    flag = false;
                }
                str = Console.ReadLine();
            }
        }
Esempio n. 9
0
        /// <summary>
        /// 启动服务
        /// </summary>
        /// <returns></returns>
        public async Task <IWebSocketServer> StartAsync()
        {
            var port = _configuration.GetValue <int>("WebSocketPort");

            _socket = await SocketBuilderFactory.GetWebSocketServerBuilder(port)
                      .OnConnectionClose((server, connection) =>
            {
                _logger.LogInformation($"连接关闭,连接名[{connection.ConnectionName}],当前连接数:{server.GetConnectionCount()}");
                RemoveConnection(connection.ConnectionId);
            })
                      .OnException(ex =>
            {
                _logger.LogError(ex, $"服务端异常:{ex.Message}");
            })
                      .OnNewConnection((server, connection) =>
            {
                connection.ConnectionName = $"{connection.ClientAddress.ToString()}";
                _logger.LogInformation($"新的连接:{connection.ConnectionName},当前连接数:{server.GetConnectionCount()}");
            })
                      .OnRecieve((server, connection, msg) =>
            {
                if (msg.HasVal() && msg.StartsWith("InformIdentity_"))
                {
                    var userId = msg.Replace("InformIdentity_", string.Empty);
                    if (userId.HasVal())
                    {
                        AddConnection(userId, connection.ConnectionId);
                        connection.Send("InformIdentity_ok");
                    }
                }
            })
                      .OnSend((server, connection, msg) =>
            {
                _logger.LogInformation($"向连接名[{connection.ConnectionName}]发送数据:{msg}");
            })
                      .OnServerStarted(server =>
            {
                _logger.LogInformation($"服务启动");
            }).BuildAsync();

            return(_socket);
        }