Photon (v4)的基本框架。开发框架主要Photon和游戏逻辑(C#)两个部分,以下图最新的Photon v4支持的4种底层协议,游戏开发逻辑Photon目前主要划分为Load Balancing 和MMO(大型多人同时在线游戏)。服务器
新建解决方案TestPhotonServer并新建类库项目MyPhotonServer,类库添加Photon引用(可在photon安装目录的lib里找到)框架
Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll异步
为何新建类库项目呢?全部的Photon的服务端程序都是先编译成dll,再由PhotonControl.exe经过配置文件调用运行的。ide
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Photon.SocketServer; namespace MyPhotonServer { public class MyServerApplication:ApplicationBase { protected override PeerBase CreatePeer(InitRequest initRequest) { return new MyServerPeer(initRequest); } protected override void Setup() { //初始化 } protected override void TearDown() { //关闭 } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Photon.SocketServer; using PhotonHostRuntimeInterfaces; namespace MyPhotonServer { public class MyServerPeer:ClientPeer { public MyServerPeer(InitRequest initRequest) : base(initRequest) { } protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { //响应客户端的断开链接 } protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { //响应客户端的操做请求 } } }
看代码,这里是一个最简单的Photon服务端:函数
一、Application为服务端程序入口,全部开发者本身的程序都要有一个继承ApplicationBase的类的程序入口spa
二、Peer为服务端与客户端的对等通讯点,服务端和客户端都经过各自的Peer进行通讯.3d
三、V4版本的Photon对.Net的开发API作了调整,在原来的PeerBase基础上又更加细化出不一样分工的Peer,这里调用ClientPeer,能够看到官方源码里并ClientPeer并无什么东西,细心的朋友能够思考为何这么作日志
public abstract class ClientPeer : PeerBase { // Methods protected ClientPeer(InitRequest initRequest) : base(initRequest) { } }
一、PhotonControl.exe:首先全部的Photon的服务端程序都是先编译成dll,再经过配置由PhotonControl.exe调用运行的。code
二、PhotonServer.config文件:PhotonControl的运行目录中会找到这个文件,主要进行配置开发者程序来给PhotonControl调用。blog
三、log:PhotonControl.exe会在运行根目录生成日志,另外会在deploy下生成全部服务端的一个日志文件log。
部署:
在deploy目录下建一个文件夹TestPhotonServer, 右键咱们的服务端类库项目属性将Release的dll重定向下。注意要把dll设置在TestPhotonServer里新建的bin目录里
(因为示例很简单Release时候Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll、ExitGamesLibs.dll这几个dll没有发布到bin,这里手动复制到deploy下的TestPhotonServer/bin里面)
配置PhotonServer.config
添加Application节点到PhotonServer.config的Applications下,这里我放到loadBlancing下的Applications
<Application Name="TestPhotonServer" BaseDirectory="TestPhotonServer" Assembly="MyPhotonServer" Type="MyPhotonServer.MyServerApplication" ForceAutoRestart="true" WatchFiles="dll;config" ExcludeFiles="log4net.config"> </Application>
Name:服务器程序名称
BaseDirectory:设置的是deploy目录为基础设置,这里服务端程序文件夹在deploy里的TestPhotonServer
Assembly:Application入口程序所在的namespace
Type:入口类的完整限定性名称
ForceAutoRestart:顾名思义强制重启
WatchFiles:调用的文件后缀,dll和config
ExcludeFiles:通常是日志配置文件名称
运行PhotonControl.exe的loadBalancing就能够看到自定义的服务端已经运行
客户端暂时用简单的控制台,解决方案下添加一个控制台项目, 添加引用Photon3DotNet.dll
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ExitGames.Client.Photon; namespace MyPhotonClient { class MyPhotonClientPeerListener : IPhotonPeerListener { public bool IsConnect = false; public void DebugReturn(DebugLevel level, string message) { } public void OnEvent(EventData eventData) { } public void OnMessage(object messages) { } public void OnOperationResponse(OperationResponse operationResponse) { } public void OnStatusChanged(StatusCode statusCode) { //与服务器链接状态发生改变 Console.WriteLine("当前与服务端链接状态:"+statusCode); switch (statusCode) { case StatusCode.Connect: IsConnect = true; break; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ExitGames.Client.Photon; namespace MyPhotonClient { class Program { static void Main(string[] args) { MyPhotonClientPeerListener listener = new MyPhotonClientPeerListener(); PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Udp); if (peer.Connect("localhost:5055","MyServer")) { Console.WriteLine("客户端准备链接请求……"); while (!listener.IsConnect) { Console.WriteLine("链接中……"); peer.Service(); System.Threading.Thread.Sleep(500); } Console.WriteLine("已链接……"); //peer.Disconnect(); Console.ReadKey(); } else { Console.Write("未找到服务器"); } } } }
DebugReturn方法:主要提供各种错误与警告【供开发者】查看,在开发状态下协助开发者纠错。好比:讲上面客户端Program.cs里的地址localhost:5055,改为localhost:5050运行的时候仍是会不停的请求,可是没法成功链接,程序是不会报错的。这个时候咱们在DebugReturn方法里打印一下message帮助查找问题源
OnEvent(EventData eventData):处理Photon Server发送过来给客户端处理的事件。Event用于客户端和服务端沟通,操做(Operation)一般会触发Event,能够经过Event Code直到事件类型。时间的消息内容一般包含着它的Parameters里。这里暂做简单介绍
OnMessage(object messages):消息回调函数
OnOperationResponse(OperationResponse operationResponse):响应Operation的回调函数,好比加入游戏房间操做,服务器会分配给每一个客户端一个编号。这个Client的编号就能够经过响应回调函数获取
OnStatusChanged(StatusCode statusCode):链接状态函数,当游戏的异步操做完成活发生错误时候,状态发生改变回调这个函数
PhotonPeer主要功能是客户端和Photon Server 通讯。能够理解为对等通讯点或者勉强理解为信使。PhotonPeer经过listener和通讯协议和服务端通讯。。每一个Application均可以有多个PhotonPeer,可是每个不一样的PhotonPeer都应该有本身listener用来监听事件、操做、回调函数。这里的listener就是继承IPhotonPeerListener接口的类的实例。
peer.Connect调用的时候并不会直接去链接服务器,只有当peer.service()调用的时候才会向服务器发送请求。
后文再详解