Ejemplo n.º 1
0
        private void Init()
        {
            client             = SocketFactory.CreateClient <AsyncTcpClient>(host, 8800);
            client.DataReceive = (o, args) =>
            {
                string line = args.Stream.ToPipeStream().ReadLine();
                line = ChatModel.Util.StringUtil.GetGBString(line);

                Console.WriteLine(line);
                CmdInfo info = JsonSerializer.Deserialize <CmdInfo>(line);
                switch (info.Type)
                {
                case CmdType.Error:
                    Console.WriteLine(info.GetDataRowText());
                    break;

                case CmdType.Login:
                    user = info.As <User>();
                    Console.WriteLine($"欢迎[{user.Name}]登录系统");
                    break;

                case CmdType.SendMsg:
                    ReceiveMsgInfo receiveMsgInfo = info.As <ReceiveMsgInfo>();
                    if (receiveMsgInfo != null)
                    {
                        Console.WriteLine($"[{receiveMsgInfo.From.Name}]:[{receiveMsgInfo.Msg}]");
                    }
                    break;
                }
            };
            client.Connected += (c) =>
            {
                Console.WriteLine("已经连接到服务器");
            };
            client.Connect();
        }