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);
相关推荐
Thera7776 分钟前
Linux 核心绑定(CPU Affinity)详解:原理、方法与优缺点分析
linux·运维·服务器
幽络源小助理12 分钟前
SpringBoot+Vue智能学习平台系统源码 | 教育类JavaWeb项目免费下载 – 幽络源
vue.js·spring boot·学习
小鹏linux12 分钟前
【linux】进程与服务管理命令 - setup
linux·运维·服务器
倔强的石头10615 分钟前
【Linux指南】进程控制系列(二)进程终止 —— 退出场景、方法与退出码详解
linux·运维·服务器
爱吃生蚝的于勒28 分钟前
【Linux】零基础深入学习动静态库+深入学习地址
linux·运维·服务器·c语言·数据结构·c++·学习
不甘平凡的小鸟30 分钟前
libcurl+vs2017+openssl编译
linux·运维·服务器
Noushiki42 分钟前
RabbitMQ 基础 学习笔记1
笔记·学习·rabbitmq
知识分享小能手43 分钟前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04中的任务计划详解(16)
linux·学习·ubuntu
geniuscrh1 小时前
自建Tailscale的Derp服务器
运维·服务器
我命由我123451 小时前
Photoshop - Photoshop 工具栏(53)画板工具
笔记·学习·职场和发展·求职招聘·职场发展·学习方法·photoshop