2019-08-16

基于PhotonServer SDK的unity多人游戏开发总体结构

总体架构.jpg

基本思路.jpg
客户端.jpg
服务端.jpg

SyncPlayerRequest为例实现方法如下

在建立连接以后首先Request代码如

public abstract class Request:MonoBehaviour
{
    public OperationCode OpCode;

 abstract public void OnOperationResponse(OperationResponse operationResponse);
    abstract public void DeFaultRequest();
    public virtual void Start()
    {
        PhotonEngine.Instance.AddRequest(this);//场景启动字典添加各Request,用于发送时和接收Response时对应具体的Request
    }
    public virtual void OnDestroy()
    {
        PhotonEngine.Instance.RemoveRequest(this);//关闭场景是字典中删除
    }
}

我们的SyncPlayerRequest方法继承于Request代码如下

public class SyncPlayerRequest : Request {
    private Player player;

    public override void Start()
    {
        base.Start();
        player = GetComponent();
    }
    public override void DeFaultRequest()
    {
        PhotonEngine.Peer.OpCustom((byte)OpCode, null, true); //向服务器发送指令方法,OpCode为相应的标识,null位置为具体的消息。
    }

 public override void OnOperationResponse(OperationResponse operationResponse)//处理服务器相应的Response
    {
        string usernameListString = (string)DictTool.GetValue(operationResponse.Parameters, (byte)ParameterCode.UsernameList);
        using (StringReader reader = new StringReader(usernameListString))//反序列化数据,获取用户登录名列表
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List));
            List usernameList = (List)serializer.Deserialize(reader);
            player.OnSyncPlayerResponse(usernameList);
        }
    }
}

客户端通过 DeFaultRequest()来发送请求,服务端通过建立的通道接收到消息,通过OpCode来调用特定的Handler,服务端BaseHandler代码如下

namespace ChatServer.Handler
{
    public abstract class BaseHandler
    {
        public OperationCode OpCode;
        abstract public void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters,ChatPeer peer);
    }
}

其中SyncPlayerHandler继承了BaseHandler代码如下

namespace ChatServer.Handler
{
    class SyncPlayerHandler : BaseHandler
    {
        public SyncPlayerHandler()
        {
            OpCode = OperationCode.SyncPlayer;
        }
        public override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters, ChatPeer peer)//处理来自客户端请求的具体部分
        {
            List usernameList = new List();
            foreach (ChatPeer tempPeer in ChatServer.Instance.peerList)
            {
                if (string.IsNullOrEmpty(tempPeer.username) == false && tempPeer != peer)
                {
                    usernameList.Add(tempPeer.username);
                }
            }
            StringWriter sw = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(List));
            serializer.Serialize(sw, usernameList);
            sw.Close();
            string usernameListString = sw.ToString();//序列化数据,防止传输时数据损坏
            Dictionary data = new Dictionary();
            data.Add((byte)ParameterCode.UsernameList, usernameListString);
            OperationResponse response = new OperationResponse(operationRequest.OperationCode);
            response.Parameters = data;
            peer.SendOperationResponse(response, sendParameters);
            //告诉其他客户端有新的客户端加入
            foreach (ChatPeer tempPeer in ChatServer.Instance.peerList)
            {
                if (string.IsNullOrEmpty(tempPeer.username) == false && tempPeer != peer)
                {
                    EventData ed = new EventData((byte)EventCode.NewPlayer);
                    Dictionary data2 = new Dictionary();
                    data2.Add((byte)ParameterCode.Username, peer.username);
                    ed.Parameters = data2;
                    tempPeer.SendEvent(ed, sendParameters);//向客户端发送事件,有特定的EventCode标识来请求客户端具体的处理事件代码
                    ChatServer.log.Info(peer.username);//打印日志信息,可查看,方便调试而已。
                }
            }
        }
    }
}

客户端对Response和Event的处理代码分别如下

//具体的请求标识在unity相应的组件中选择
public override void OnOperationResponse(OperationResponse operationResponse)
    {
        string usernameListString = (string)DictTool.GetValue(operationResponse.Parameters, (byte)ParameterCode.UsernameList);
        using (StringReader reader = new StringReader(usernameListString))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List));
            List usernameList = (List)serializer.Deserialize(reader);
            player.OnSyncPlayerResponse(usernameList);
        }
    }
//具体的事件标识在unity相应的组件中选择
public class NewPlayerEvent : BaseEvent {
    private Player player;
    public override void Start()
    {
        base.Start();
        player = GetComponent();  
    }
    public override void OnEvent(EventData eventData)
    {
        string username = (string)DictTool.GetValue(eventData.Parameters, (byte)EventCode);//工具类方法方便获取字典里的值
        player.OnNewPlayerEvent(username);
    }
}

在服务端和客户端之间发送信息时需要注意用相应的标识来定位具体的请求或者事件。
代码中用到的命名空间如下其中Common为自己写的工具类库,就是获得字典的类方法和标识枚举,读着可自行设计:

using ExitGames.Client.Photon;
using UnityEngine;
using System.Collections.Generic;
using Common.Tools;
using Common;
using System.IO;
using System.Xml.Serialization;
服务端
using Photon.SocketServer;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using System.IO;
using log4net.Config;还有自己需要引用的自己写的类的命名空间。

希望对你能带来帮助。

你可能感兴趣的:(2019-08-16)