private void SendMsg(IServer server, ISession session, CmdInfo info) { MsgInfo input = info.As <MsgInfo>(); if (input != null) { User fromUser = GetUserInfo(server, input.From); if (fromUser == null) { logger.LogInformation($"您还没有登录系统 the session name is: {session.Name}"); SendError(session, "您还没有登录系统"); return; } if (input.MsgOfBytes != null) { logger.LogInformation($"the first byte is: {input.MsgOfBytes[0]}"); logger.LogInformation($"the last byte is: {input.MsgOfBytes[input.MsgOfBytes.Length - 1]}"); } switch (input.ToType) { case MsgToType.User: ISession toSession = GetSession(server, input.To); SendInfo(toSession, info, new ReceiveMsgInfo(fromUser, input)); break; case MsgToType.Group: break; case MsgToType.System: break; } } }
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(); }
private void Login(ISession session, CmdInfo info) { LoginInfo input = info.As <LoginInfo>(); if (input == null) { SendError(session, "参数错误-LoginInpu"); return; } var user = userRepository.Login(input); if (user == null) { SendError(session, "登录失败-请检查用户名或密码"); return; } user.Password = string.Empty; session[currentUserKey] = user; SendInfo(session, info.Clone(user)); }
private void AddUser(ISession session, CmdInfo info) { UserExtInfo userExt = info.As <UserExtInfo>(); if (userExt == null) { SendError(session, "参数错误-UserExtInfo"); } try { User user = new User(userExt) { Password = StringUtil.GetMd5String(User.PASSWORD) }; userRepository.InsertOrUpdate(user); SendInfo(session, info.Clone("添加成功")); } catch (Exception e) { SendError(session, e.Message); } }