Unity游戏开发Photon Server之客户端架构
发表于2017-08-31
客户端的服务器部分通过不同的控制器对外提供调用,完成和服务器的通信。
类间关系如图:
图中的类里,PhotonEngine是继承了MonoBehavior类和IPhotonPeerListener接口的单例类,会在Awake函数中连接服务器,在Update函数中调用PhotonPeer的Service函数来向服务器发送Request,并接受服务器的Response.值得注意的是在场景加载时要确保不会被销毁以防止连接断开。
ControllerBase是抽象类,继承自MonoBehavior.在Start函数执行时将在PhotonEngine中注册。当PhotonEngine得到Response之后,会根据OperationCode调用已注册的相应的Controller.在Destroy函数中,会取消在PhotonEngine的注册记录以防止出现找不到引用的bug.各个不同的Controller就会对不同的操作进行处理。
PhotonEngine类的实例代码如下:
public class PhotonEngine : MonoBehaviour, IPhotonPeerListener { IPHostEntry hostEntry; public ConnectionProtocol protocol = ConnectionProtocol.Tcp; public string IPaddress = string.Empty; //public string IPAddress = IP+":4530"; public string applicationName = "ARApplication"; public Dictionary<byte, ControllerBase> controllerDic = new Dictionary<byte, ControllerBase>(); public delegate void OnConnectedToServerEvent(); public event OnConnectedToServerEvent OnConnectedToServer; public delegate void OnDisConnectedEvent(); public event OnDisConnectedEvent OnDisConnect; public static PhotonEngine photonInstance { get { return _instance; } } private static PhotonEngine _instance; PhotonPeer peer; bool isConnected = false; void Awake() { _instance = this; hostEntry = Dns.GetHostEntry(Dns.GetHostName()); foreach(IPAddress ip in hostEntry.AddressList) { if(ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork) { IPaddress += ":4530"; } } peer = new PhotonPeer(this, protocol); peer.Connect(IPaddress, applicationName); } void Update() { peer.Service(); } public void RegisterController(OperationCode opCode, ControllerBase controller) //在ControllerBase中调用此方法 { controllerDic.Add((byte)opCode, controller); } public void UnRegisterController(OperationCode opCode) { controllerDic.Remove((byte)opCode); } public void SendRequest(OperationCode opCode, Dictionary<byte, object> parameter) { peer.OpCustom((byte)opCode, parameter, true); } public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnOperationResponse(OperationResponse operationResponse) { Debug.Log("Response Sucessed"); ControllerBase controllerObject; controllerDic.TryGetValue(operationResponse.OperationCode, out controllerObject); //根据operationResponse.Operationcode得到字典中已经注册好的对应的Controller controllerObject.OnOperationResponse(operationResponse);//调用Controller中的方法; } public void OnStatusChanged(StatusCode statusCode) { switch (statusCode) { case StatusCode.Connect: { isConnected = true; OnConnectedToServer(); } break; case StatusCode.Disconnect: { Debug.Log("Disconnected"); } break; } } }
接着是抽象类ControllerBase
public abstract class ControllerBase : MonoBehaviour { public abstract OperationCode opCode { get; } public virtual void Start() { PhotonEngine.photonInstance.RegisterController(opCode, this); } public virtual void OnDestroy() { PhotonEngine.photonInstance.UnRegisterController(opCode); } public abstract void OnOperationResponse(OperationResponse response); }