Photon Server 實現註冊與登陸(三) --- 前端UI設計和發起請求

 

1、打開以前的測試項目。先將服務端代碼編譯一下,在 bin/Debug/目錄下會發現有一個Common.dill。咱們相應導入到前端使用。直接拖拽到相應地方前端

 

UI相應佈局屬於前端操做,這裏就不作介紹了。詳細查看視頻:https://www.bilibili.com/video/av35109390/?p=70後端

 

2、代碼處理:跟後端同樣,前端發起請求的地方也會隨着項目變大而變多,因此也得單獨區分出每一個請求獨自管理。服務器

Request.cs 請求基類
LoginRequest.cs  登陸請求邏輯
RegiesterRequest.cs  註冊請求鏈接

LoginPanel.cs  登陸面掛載腳本
ReginsterPanel.cs 註冊腳本掛載腳

 

 

3、調整PhotonManager.cs 管理類ide

  (1)、添加字典,管理因此請求函數

    private Dictionary<OperationCode,Request> ResquesDict = new Dictionary<OperationCode, Request>();

       (2)、分配peer佈局

    public static PhotonPeer Peer
    {
        get { return peer; }
    }

       (3)、添加兩個函數,管理請求測試

    public void AddRequest(Request rest)
    {
        ResquesDict.Add(rest.OpCode,rest);
    }

    public void RemoveRequest(Request rest)
    {
        ResquesDict.Remove(rest.OpCode);
    }

      (4)、修改OnOperationResponse() 函數,實現接收服務端返回數據的分發this

 /// <summary>
    /// 客戶端請求後,服務端響應,返回數據
    /// </summary>
    /// <param name="operationResponse"></param>
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        //服務端返回的code
        OperationCode opCode = (OperationCode)operationResponse.OperationCode;
        Request request = null;
        
        //根據code查找相應請求
        bool tmp = ResquesDict.TryGetValue(opCode, out request);
        
        if (tmp)
        {
            //若是存在,分發給相應OnOperationResponse去處理本身的接收數據邏輯
            request.OnOperationResponse(operationResponse);
        }
        else
        {
            Debug.Log("沒找到響應處理對象");
        }
    }

 (5)、完整代碼spa

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection.Emit;
using Common;
using UnityEngine;
using ExitGames.Client.Photon;

public class PhotonManager : MonoBehaviour,IPhotonPeerListener
{
    //單例模式,保證只有一個連接服務器
    public static PhotonManager Instance;
    private static PhotonPeer peer;
    private bool connected;
    private Dictionary<OperationCode,Request> ResquesDict = new Dictionary<OperationCode, Request>();
    public static PhotonPeer Peer
    {
        get { return peer; }
    }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this.gameObject);
        }else if (Instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("111------開始鏈接----");
        
        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055","MyGame1");
        connected = false;
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("update------" + connected);
        
        peer.Service();
    }

    private void OnDestroy()
    {
        if (peer != null && peer.PeerState == PeerStateValue.Connected)
        {
            peer.Disconnect();
        }
    }

    public void DebugReturn(DebugLevel level, string message)
    {
    }

    /// <summary>
    /// 客戶端請求後,服務端響應,返回數據
    /// </summary>
    /// <param name="operationResponse"></param>
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        //服務端返回的code
        OperationCode opCode = (OperationCode)operationResponse.OperationCode;
        Request request = null;
        
        //根據code查找相應請求
        bool tmp = ResquesDict.TryGetValue(opCode, out request);
        
        if (tmp)
        {
            //若是存在,分發給相應OnOperationResponse去處理本身的接收數據邏輯
            request.OnOperationResponse(operationResponse);
        }
        else
        {
            Debug.Log("沒找到響應處理對象");
        }
    }

    /// <summary>
    /// 鏈接狀態發生改變時
    /// </summary>
    /// <param name="statusCode"></param>
    public void OnStatusChanged(StatusCode statusCode)
    {
        Debug.Log("serStatus-----" + statusCode.ToString());
        
        switch (statusCode)
        {
            case StatusCode.Connect:
                connected = true;
                break;
            default:
                connected = false;
                break;
        }
    }

    /// <summary>
    /// 服務端直接給客戶端數據時,不須要向服務器請求
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEvent(EventData eventData)
    {
        switch (eventData.Code)
        {
            case 1:
                break;
            case 2:

                break;
            default:
                break;
        }
    }

    public void AddRequest(Request rest)
    {
        ResquesDict.Add(rest.OpCode,rest);
    }

    public void RemoveRequest(Request rest)
    {
        ResquesDict.Remove(rest.OpCode);
    }
}
View Code

 

4、請求類rest

  (1)、Request.cs 基類

using System;
using Common;
using ExitGames.Client.Photon;
using UnityEngine;

public abstract class Request: MonoBehaviour
{
    //定義請求code
    public OperationCode OpCode;
    
    public abstract void DefaultRequest(); //發送請求
    
    public abstract void OnOperationResponse(OperationResponse operationResponse); //接收返回的數據

    public virtual void Start()
    {
        //從管理類中添加該請求
        PhotonManager.Instance.AddRequest(this);
    }

    public void OnDestroy()
    {
        //從管理類中移除該請求
        PhotonManager.Instance.RemoveRequest(this);
    }
}
View Code

  (2)、登錄類 LoginRequest.cs 

using System.Collections.Generic;
using Common;
using ExitGames.Client.Photon;
using UnityEngine;

public class LoginRequest : Request
{
    //經過代碼賦值,全部在UI界面上隱藏掉
    [HideInInspector]
    public string UserName;
    [HideInInspector]
    public string Password;

    //獲取UI面板
    private LoginPanel loginPanel;

    public override void Start()
    {
        //調用基類Start,將登錄請求添加到管理類中
        base.Start();
        loginPanel = GetComponent<LoginPanel>();
    }
    public override void DefaultRequest()
    {
        //登陸請求發起
        Dictionary<byte,object> data = new Dictionary<byte, object>();
        data.Add((byte) ParameterCode.UserName,UserName);
        data.Add((byte) ParameterCode.Password,Password);
        
        //經過Peer發送請求給服務端
        PhotonManager.Peer.OpCustom((byte)OpCode,data,true);
    }
    
    public override void OnOperationResponse(OperationResponse operationResponse)
    {
        //接收服務端的返回響應,將返回code給面板,由面板來處理接下來的邏輯(是提示錯誤仍是跳轉場景)
        ReturnCode returnCode = (ReturnCode) operationResponse.ReturnCode;
        loginPanel.OnLiginResponse(returnCode);
    }
}
View Code

       (3)、登錄面板腳本  LoginPannel.cs

using System;
using System.Collections;
using System.Collections.Generic;
using Common;
using UnityEngine;
using UnityEngine.UI;

public class LoginPanel : MonoBehaviour
{
    //如下public參數經過UI拖拽進行賦值
    #region Tags
    
    //註冊面板,用於點擊註冊時顯示該面板
    public GameObject registerPanel;
    //控件
    public InputField username;
    public InputField password;
    public Text hintmessage;

    //登錄請求類
    private LoginRequest loginRequest;
    
    #endregion
   
    private void Start()
    {
        //從面板中獲取請求類(面板腳本和請求類都掛載到請求面板UI中)
        loginRequest = GetComponent<LoginRequest>();
    }

    public void OnLoginButton()
    {
        //點擊登錄按鈕時,發送請求
        hintmessage.text = "";
        
        loginRequest.UserName = username.text;
        loginRequest.Password = password.text;
        
        //調用請求類中的發起請求
        loginRequest.DefaultRequest();
    }

    public void OnRegisterButton()
    {
        //點擊註冊時,隱藏登錄UI,顯示註冊UI
        gameObject.SetActive(false);
        registerPanel.SetActive(true);
    }

    public void OnLiginResponse(ReturnCode returnCode)
    {
        //服務端響應返回數據時,請求類會將返回信息傳遞到該函數
        Debug.Log("-----返回code" + returnCode);
        
        if (returnCode == ReturnCode.Success)
        {
            //登陸成功,調整下一個場景
            Debug.Log("--登錄成功----");
        }
        else
        {
            hintmessage.text = "用戶名或密碼錯誤";
        }
    }
}
View Code

      註冊類和註冊面板跟登錄相同邏輯

     (4)、註冊類 ReginesterRequest.cs

using System.Collections;
using System.Collections.Generic;
using Common;
using ExitGames.Client.Photon;
using UnityEngine;

public class RegiesterRequet : Request
{
    [HideInInspector]
    public string username; 
    [HideInInspector]
    public string password;

    private ReginsterPanel reginsterPanel;

    public override void Start()
    {
        base.Start();
        reginsterPanel = GetComponent<ReginsterPanel>();
    }

    public override void DefaultRequest()
    {
        //登陸請求發起
        Dictionary<byte,object> data = new Dictionary<byte, object>();
        data.Add((byte) ParameterCode.UserName,username);
        data.Add((byte) ParameterCode.Password,password);
        
        PhotonManager.Peer.OpCustom((byte)OpCode,data,true);
    }

    public override void OnOperationResponse(OperationResponse operationResponse)
    {
        ReturnCode returnCode = (ReturnCode) operationResponse.ReturnCode;
        reginsterPanel.OnReigsterResponse(returnCode);
    }
    
}
View Code

    (5)、註冊面板  ReginsterPanel.cs

using System;
using System.Collections;
using System.Collections.Generic;
using Common;
using UnityEngine;
using UnityEngine.UI;

public class ReginsterPanel : MonoBehaviour
{
    public GameObject loginPanel;

    public InputField username;
    public InputField password;
    public Text hintmessage;

    private RegiesterRequet regiesterRequet;

    private void Start()
    {
        regiesterRequet = GetComponent<RegiesterRequet>();
    }

    public void OnRegisterButton()
    {
        hintmessage.text = "";
            
        regiesterRequet.username = username.text;
        regiesterRequet.password = password.text;
        
        regiesterRequet.DefaultRequest();
    }
    
    public void OnBackBtn()
    {
        loginPanel.SetActive(true);
        gameObject.SetActive(false);
    }

    public void OnReigsterResponse(ReturnCode returnCode)
    {
        if (returnCode == ReturnCode.Success)
        {
            hintmessage.text = "註冊成功,請返回登陸";
        }
        else
        {
            hintmessage.text = "用戶名重複,請從新修改用戶名";
        }
    }
}
View Code

 

 

前端UI和請求、響應邏輯基本完成。接下來進行服務端響應。

 

查看視頻:https://www.bilibili.com/video/av35109390/?p=70

相關文章
相關標籤/搜索