unity学习(19)——客户端与服务器合力完成注册功能(1)入门准备

逆向服务器用了三天的时间,但此时觉得一切都值,又可以继续学习了。

服务器中登录请求和注册请求由command变量进行区分,上一层的type变量都是login。

public void process(Session session, SocketModel model)
{
  switch (model.Command)
  {
    case 0:
      this.login(session, model);
      break;
    case 2:
      this.reg(session, model);
      break;
  }
}

从注册入手!视频对应的应该是第七讲。

点击注册按钮,输入账号密码,实测可以在服务器收到编码后的字符串,但此时有个问题就是只有点击登录按钮后,客户端才会连接服务器,上来直接点击“注册”再“确定”是收不到任何东西的。

现在这些都简单了,新发现是vs可以自动分别打开两个不同的项目,登录按钮绑定的函数如下。

unity学习(19)——客户端与服务器合力完成注册功能(1)入门准备_第1张图片

然后发现,LoginClick()和RegistClick()中基本大同小异,说明建立连接的不是LoginClick()。而是因为“登录”按钮绑了Canvas,Canvas中是有网络初始化脚本的。所以最简单的办法就是给登录界面右边的“注册”按钮也绑上Canvas。实测此时确实可以直接注册

unity学习(19)——客户端与服务器合力完成注册功能(1)入门准备_第2张图片

然后最自然的想法就是看看账号密码存在哪里了!加个位数限制,16位之内,防溢出,至于字符限制,暂时不用考虑。

public void reg(Session session, SocketModel model)
{
  Console.WriteLine("用户申请注册666");
        //MyLog.form.textAdd("用户申请注册");
  Console.WriteLine(model.Message);
  LoginDTO loginDto = Coding.decode(model.Message);
  Console.WriteLine("与众不同"+loginDto.userName.Length+"    "+ loginDto.passWord.Length);
  if(loginDto.userName.Length<=17&& loginDto.passWord.Length<=17)
  {
            Console.WriteLine("BizUtil.account.create");
            bool v = BizUtil.account.create(loginDto.userName, loginDto.passWord);
            session.write(0, 0, 3, (object)new BoolDTO(v));
  }
}

然后去看BizUtil.account.create()函数的代码,vs会自动定位的。

有如下两类数据需要存储

namespace GameServer.src_biz
{
  internal class BizUtil
  {
    public static AccountBiz account = new AccountBiz();
    public static UserBizImpl user = new UserBizImpl();
  }
}

 去看AccountBiz类(account是类中实例化的一个对象)中的create()函数的内容,只要知道满足条件就去进TryAdd函数就可以了:

public bool create(string userName, string password)
{
  if (this.accounts.ContainsKey(userName))
    return false;
  AccountModel accountModel = new AccountModel(Guid.NewGuid().ToString(), userName, password);
  return this.accounts.TryAdd(userName, accountModel);
}

实际测试发现,create函数的返回值是True和false,可以做到1.判断账号是否重复。但2只要服务器重启,之前的数据就全部作废了!简而言之没有数据库功能!unity学习(19)——客户端与服务器合力完成注册功能(1)入门准备_第3张图片

你可能感兴趣的:(学习,服务器,运维)