Photon Server implementation of the registration and login (four) --- register and login server response

As already collated server-side code, MyGameServer.cs ClientPeer.cs requests and responses and were split. Next, in response to the leading end of treatment

 

First, in response to login request

  Before finishing, mainly in response to the request class distal ClientPeer.cs the OnOperationRequest function. The response function taken from the group according to the management Handler code transmitted Handler distal respond.

// ClientPeer.cs request receiving function
 // response request distal 
        protected  the override  void OnOperationRequest (OperationRequest operationRequest, SendParameters sendParameters) 
        { 
            MyGameServer.log.Info ( " Client requested --- --- " + operationRequest.OperationCode); 

            // first came the acquisition of the client code (operationRequest.OperationCode)
             // then get Handler registered under the code to MyGameServer in.
            // Handler our registered to the main function HandlerDict in.
            // DictTool tools is our own definition, easy to pass key, you can Dict in value from here out is code corresponding Handler 
            
            BaseHandler HandlerDictTool.GetValue = <OperationCode, BaseHandler> (MyGameServer.Instance.HandlerDict, 
                (OperationCode) operationRequest.OperationCode); 

            IF (! Handler = null ) 
            { 
                // find the corresponding Handler, direct call processing logic corresponding OnOperationRequest 
                handler.OnOperationRequest ( operationRequest, sendParameters, the this ); 
            } 
            the else 
            { 
                // If not found, returns DefaultHandler our custom. 
                BaseHandler defHander = DictTool.GetValue <OperationCode, BaseHandler> (MyGameServer.Instance.HandlerDict, 
                    OperationCode.Default);
                
                defHander.OnOperationRequest(operationRequest,sendParameters,this);
            }
            
        }

  code login request is transmitted OperationCode.Login, LoginHander.cs main function acquired from the management group, then the call LoginHandler.cs OnOperationRequest () method and process received data.

  LoginHander.cs landed class

the using the System;
 the using the Common;
 the using Common.Toos;
 the using MyGameServer.Manager;
 the using Photon.SocketServer; 

namespace MyGameServer.Hander 
{ 
    public  class LoginHander: BaseHandler 
    { 
        public LoginHander () 
        { 
            OpCode = OperationCode.Login; 
        } 

        public  the override  void OnOperationRequest (OperationRequest operationRequest, sendParameters sendParameters, clientpeer use the peer) 
        { 
            // using parameters acquired from the client side tools uploaded 
            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 ; 
            
            / / database management 
            the userManager userManager = new new the userManager (); 
            
            // detect a user name and password are correct 
            BOOL ISOK = userManager.VerifyModel (username, password); 
           
            //Back to the client data 
            OperationResponse Response = new new OperationResponse (operationRequest.OperationCode);
             IF (ISOK) 
            { 
                response.ReturnCode = ( Short ) ReturnCode.Success; 
            } 
            the else 
            { 
                response.ReturnCode = ( Short ) ReturnCode.Failed; 
            } 

            // Tell client response 
            peer.SendOperationResponse (response, sendParameters); 
        } 
    } 
}
View Code

 

 

Second, the registration response class

  Receiver front-end data, and then check whether the database has the names, not to add, and returns a success

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 = newThe UserManager (); 
            
            // find out whether the user name already exists 
            the User the User = userManager.GetByName (username);
             IF (the User == null ) 
            { 
                // If not, then add the user information 
                the User = new new the User () {UserName username =, = password password}; 
                userManager.Add (User); 

                // return code, successful 
                response.ReturnCode = ( Short ) ReturnCode.Success; 
            } 
            the else 
            { 
                // return code, failure 
                response.ReturnCode = ( Short ) the ReturnCode. the Failed; 
            }

            // to the client in response 
            peer.SendOperationResponse (Response, sendParameters); 
        } 
    } 
}
View Code

 

Third, recompile, upload the modified file to the server, restart the service for testing.

  

 

 

 

   

 

 

View Video: https://www.bilibili.com/video/av35109390/?p=86

Guess you like

Origin www.cnblogs.com/cj8988/p/11691331.html