Ejemplo n.º 1
0
        static async void Test5()
        {
            Console.WriteLine("服务端1,客户端2:");
            if (Console.ReadKey().KeyChar == '1')
            {
                var svr = new NetServer(777);
#if DEBUG
                svr.Log = XTrace.Log; svr.LogSend = true; svr.LogReceive = true;
#endif
                //svr.Add<DefaultCodec>();
                svr.Add(new LengthFieldCodec {
                    Size = 4
                });
                //svr.Add<BinaryCodec<UserY>>();
                svr.Add <JsonCodec <UserY> >();
                svr.Add <EchoHandler>();
                svr.Start();
            }
            else
            {
                var client = new NetUri("tcp://127.0.0.1:777").CreateRemote();
#if DEBUG
                client.Log = XTrace.Log; client.LogSend = true; client.LogReceive = true;
#endif
                //client.Add<DefaultCodec>();
                client.Add(new LengthFieldCodec {
                    Size = 4
                });
                //client.Add<BinaryCodec<UserY>>();
                client.Add <JsonCodec <UserY> >();
                client.Open();

                //client.Send("Stone");
                var user = new UserY {
                    ID = 0x1234, Name = "Stone", DisplayName = "大石头"
                };
                for (var i = 0; i < 3; i++)
                {
                    var rs = await client.SendAsync(user) as UserY;

                    XTrace.WriteLine("{0} {1}", rs.Name, rs.DisplayName);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>创建客户端之后,打开连接之前</summary>
        /// <param name="svr"></param>
        protected virtual ISocketClient OnCreate(String svr)
        {
            var client = new NetUri(svr).CreateRemote();

            // 网络层采用消息层超时
            client.Timeout     = Timeout;
            client.StatSend    = StatSend;
            client.StatReceive = StatReceive;

            client.Add(GetMessageCodec());

            client.Opened   += (s, e) => OnNewSession(this, s);
            client.Received += Client_Received;

            return(client);
        }
Ejemplo n.º 3
0
        /// <summary>创建客户端之后,打开连接之前</summary>
        /// <param name="svr"></param>
        protected virtual ISocketClient OnCreate(String svr)
        {
            var client = new NetUri(svr).CreateRemote();

            client.Timeout = Timeout;
            //if (Log != null) client.Log = Log;
            client.StatSend    = StatSend;
            client.StatReceive = StatReceive;

            client.Add(new StandardCodec {
                Timeout = Timeout, UserPacket = false
            });

            client.Opened += (s, e) => OnNewSession(this, s);

            return(client);
        }