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);
相关推荐
大哥您好1 小时前
QEMU 11.1.2 多架构编译指南(x86_64 / aarch64 / riscv64)
linux·服务器
我是伪码农2 小时前
百蝶杯学习
学习
keyipatience2 小时前
JavaScript学习(1)书写位置,变量、数据类型、运算符
开发语言·前端·javascript·css·学习
Gauss松鼠会3 小时前
高斯数据库(openGauss)的物理存储结构
运维·服务器·数据库·sql·opengauss·经验总结
Android系统攻城狮3 小时前
Linux Gstreamer深度解析之gst_audio_ring_buffer_prepare_read调用流程与实战(七十四)
linux·运维·服务器·gstreamer音视频·音视频进阶
杨云龙UP3 小时前
PostgreSQL 18.4 数据库整库迁移实战:pg_dump 备份与 pg_restore 恢复完整操作手册
运维·服务器·数据库·postgresql·pg_dump·pg_restore
xian_wwq4 小时前
【学习笔记】Milvus 如何完成一次向量相似度检索,Milvus 索引到底应该怎么选?-15
笔记·学习·milvus
Hhuhkt4 小时前
3D 云养宠小游戏
学习
阿松爱学习4 小时前
【Unity开发】监听Scene场景事件
unity·c#
土星云SaturnCloud5 小时前
土星云智慧服务区方案:AI视觉+边缘计算+视频存管一体化的全场景智能化落地解析
服务器·人工智能·ai·边缘计算