unity学习(21)——客户端与服务器合力完成注册功能(3)用字典存储账号密码

cs 复制代码
public void reg(Session session, SocketModel model)
{
  LoginDTO loginDto = Coding<LoginDTO>.decode(model.Message);
  if(loginDto.userName.Length<=17&& loginDto.passWord.Length<=17)
  {
       bool v = BizUtil.account.create(loginDto.userName, loginDto.passWord);//bool代表注册成功或者失败
       session.write(0, 0, 3, (object)new BoolDTO(v));//这个session还需要单独拿时间来学
  }
}

0.最简单的一句 session.write(0, 0, 3, (object)new BoolDTO(v));

这句往session中的List队列中写入了一个消息,就是最早的SocketModel格式的,最后的message就一个bool值,代表最终的注册结果成功与否。

1.首先知道注册信息写到什么地方去了,先进create函数

cs 复制代码
public bool create(string userName, string password)
{
    if (this.accounts.ContainsKey(userName))
    {
        Console.WriteLine("注册失败");
        return false;
    }
  AccountModel accountModel = new AccountModel(Guid.NewGuid().ToString(), userName, password);
  return this.accounts.TryAdd(userName, accountModel);
}

2.然后,this.accounts.ContainsKey(userName)的结果代表用户名是否重复,所以其中自然有如何读取,进而判定用户名重复的内容。

先从accounts的定义入手,private ConcurrentDictionary<string, AccountModel> accounts = new ConcurrentDictionary<string, AccountModel>();

accounts是个字典,键是string代表用户名,值是AccountModel结构体。

ContainsKey函数是C#中的Dictionary方法,用于检查Dictionary中是否存在键。

如果键(用户名)已经存在,则注册失败,不存在,则通过以下代码新建一个值

cs 复制代码
AccountModel accountModel = new AccountModel(Guid.NewGuid().ToString(), userName, password);

再把用户名和这个值加入字典

cs 复制代码
 return this.accounts.TryAdd(userName, accountModel);
相关推荐
毕竟是shy哥8 小时前
vs code连接远程服务器docker环境及配置免密登录
服务器·docker·vs code
徐小黑ACG9 小时前
nginx配置文件
linux·服务器·nginx
sunshine22 girl9 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
新时代牛马9 小时前
Linux VFS 完整篇:从path_openat、dentry/inode 到page cache 与挂载排障
linux·运维·服务器
Kobebryant-Manba10 小时前
学习Bert微调
人工智能·学习·bert
xian_wwq10 小时前
【学习笔记】深度认知系列-第14讲 端侧AI崛起——为什么AI正在从云端走向本地
人工智能·笔记·学习
我爱cope11 小时前
【计算机网络 | 传输层3:TCP 协议概述:面向连接、可靠传输到底意味着什么?】
网络·网络协议·学习·tcp/ip·计算机网络·传输层
新时代牛马11 小时前
Linux 驱动中断与定时完整篇:从 request_threaded_irq 到hrtimer 选型与排障
linux·运维·服务器
代码的小搬运工12 小时前
KVO学习
学习·ios·cocoa
辣知13 小时前
辣知·化智47 黄帝陵与民族精神归途
学习