作用:服务器处理请求
增加MyGameServer.InitHandler()方法上增加
//登录
LoginHandler loginHandler = new LoginHandler();
HandlerDict.Add(loginHandler.OpCode, loginHandler);
创建XXHandler.cs
修改MyGameServer
修改LoginHandler
修改OperationCode
//不改数据库
UserManager manager = new UserManager();//实例化经理
//不改回复客户端
OperationResponse response = new OperationResponse(operationRequest.OperationCode);//实例化新的回复客户端
Script—Request—新建LoginHandler.cs
LoginHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using Photon.SocketServer;
using Common.Tools;
using MyGameServer.Manager;
namespace MyGameServer.Handler
{
class LoginHandler:BaseHandler
{
//找到是那个请求
public LoginHandler()
{
OpCode = OperationCode.Login;//Login改变
}
public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer)
{
//获得数据
string username = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.Username) as string;//获得username
string password = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte)ParameterCode.Password) as string;//获得password
//数据库
UserManager manager = new UserManager();//实例化经理
bool isSuccess = manager.VerifyUser(username, password);//用经理验证一下,带上username,password。返回一个布尔值
//准备回复客户端的数据
OperationResponse response = new OperationResponse(operationRequest.OperationCode);//实例化新的回复客户端,是/否
if (isSuccess)
{
response.ReturnCode = (short)Common.ReturnCode.Success;//是
peer.username = username;//登录名
}
else
{
response.ReturnCode = (short)Common.ReturnCode.Failed;//否
}
//回复客户端
peer.SendOperationResponse(response, sendParameters);//回复客户端
}
}
}