前面已經整理過了服務端代碼,MyGameServer.cs 和 ClientPeer.cs 對請求和響應進行了拆分。接下來處理對前端的響應前端
1、響應登錄請求數據庫
以前整理中,響應前端請求主要在類ClientPeer.cs 中 OnOperationRequest 函數。該函數會根據前端傳遞的code從Handler管理組中取出響應的Handler進行響應。ide
//ClientPeer.cs 中接收請求的函數 //響應前端請求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { MyGameServer.log.Info("Client---請求了---" + operationRequest.OperationCode); //首先獲取客戶端傳來的code (operationRequest.OperationCode) //而後根據code 去 MyGameServer中獲取註冊的Handler。 //Handler咱們註冊到了主函數HandlerDict中。 //DictTool工具類是咱們本身定義的,方便傳入key,就能從Dict中取值,這裏取出的是code相對應的handler BaseHandler handler = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict, (OperationCode) operationRequest.OperationCode); if (handler != null) { //找到相應的Handler,直接調用 OnOperationRequest 進行相應邏輯處理 handler.OnOperationRequest(operationRequest,sendParameters,this); } else { //若是沒有找到,返回咱們自定義的 DefaultHandler. BaseHandler defHander = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict, OperationCode.Default); defHander.OnOperationRequest(operationRequest,sendParameters,this); } }
登錄請求發送的code是 OperationCode.Login,主函數會從管理組中獲取 LoginHander.cs ,而後調用LoginHandler.cs 的 OnOperationRequest()方法進行接收數據並處理。函數
LoginHander.cs 登錄類工具
using System; using Common; using Common.Toos; using MyGameServer.Manager; using Photon.SocketServer; namespace MyGameServer.Hander { public class LoginHander:BaseHandler { public LoginHander() { OpCode = OperationCode.Login; } public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer) { //利用工具類從獲取客戶端上傳的參數 string username = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte) ParameterCode.UserName) as string; string password = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte) ParameterCode.Password) as string; //數據庫管理類 UserManager userManager = new UserManager(); //檢測用戶名和密碼是否正確 bool isOk = userManager.VerifyModel(username, password); //返回給客戶端數據 OperationResponse response = new OperationResponse(operationRequest.OperationCode); if (isOk) { response.ReturnCode = (short)ReturnCode.Success; } else { response.ReturnCode = (short)ReturnCode.Failed; } //給客戶端響應 peer.SendOperationResponse(response, sendParameters); } } }
2、註冊響應類測試
接收前端數據,而後查詢數據庫是否有從名的,沒有就添加,並返回成功this
using Common; using Common.Toos; using MyGameServer.Manager; using MyGameServer.Model; using Photon.SocketServer; namespace MyGameServer.Hander { public class RegisterHandler:BaseHandler { public RegisterHandler() { OpCode = OperationCode.Register; } public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ClientPeer peer) { //獲取前端上傳的參數 string username = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte) ParameterCode.UserName) as string; string password = DictTool.GetValue<byte, object>(operationRequest.Parameters, (byte) ParameterCode.Password) as string; //返回給前端 OperationResponse response = new OperationResponse(operationRequest.OperationCode); UserManager userManager = new UserManager(); //查找是否已經存在該用戶名 User user = userManager.GetByName(username); if (user == null) { //若是不存在,則添加該用戶信息 user = new User(){UserName = username,Password = password}; userManager.Add(user); //返回碼,成功 response.ReturnCode = (short) ReturnCode.Success; } else { //返回碼,失敗 response.ReturnCode = (short) ReturnCode.Failed; } //給客戶端響應 peer.SendOperationResponse(response, sendParameters); } } }
3、從新編譯,上傳修改文件到服務端,從新啓動服務進行測試。spa