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);
相关推荐
艾莉丝努力练剑12 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
人生游戏牛马NPC1号2 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
saynaihe5 小时前
ubuntu 22.04 anaconda comfyui安装
linux·运维·服务器·ubuntu
小蜜蜂爱编程5 小时前
ubuntu透网方案
运维·服务器·ubuntu
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
头发那是一根不剩了6 小时前
nginx:SSL_CTX_use_PrivateKey failed
运维·服务器
死也不注释7 小时前
【第零章编辑器开发与拓展】
unity·编辑器
林林要一直努力7 小时前
AOSP Settings模块问题初窥
android·学习·bug·android studio
死也不注释8 小时前
【第一章编辑器开发基础第二节编辑器布局_3GUI元素和布局大小(3/4)】
unity·编辑器
景彡先生8 小时前
C++编译期计算:常量表达式(constexpr)全解析
服务器·c++