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);
相关推荐
想要入门的程序猿20 分钟前
摄影测量学习()
数码相机·学习
worilb23 分钟前
Spring Cloud 学习与实践(13):使用 Seata 解决分布式事务问题
分布式·学习·spring cloud
渣渣灰飞36 分钟前
MySQL 系统学习 第四阶段:MySQL 高级 第一节:索引(Index)
数据库·学习·mysql
网络小白不怕黑1 小时前
监控httpd状态
服务器
星子yu1 小时前
【学习】怎么学好数据结构
数据结构·学习
~kiss~2 小时前
MoE的痛点:离散路由
学习
Amazing_Cacao2 小时前
CFCA精品可可工艺师初级校准:暴力破解机器黑盒,实现物理参数与最终风味的精准对齐
笔记·学习
日取其半万世不竭2 小时前
云服务器迁移怎么少停机?DNS 切换前后的完整清单
运维·服务器
浮江雾3 小时前
Flutter第三节----Dart中的数据类型
android·开发语言·学习·flutter·入门·函数
风和先行3 小时前
Android 数据库相关学习总结
android·数据库·学习