新手学习Photon之创建Photon服务器

来源:互联网 发布:搜款网服饰网络批发 编辑:程序博客网 时间:2024/05/30 04:12

新手学习Photon之创建Photon服务器

这是我第一次写博客,纯属为了总结一下自己的所学所感。如有什么不好之处,请各位大神高抬贵手。作为一个unity爱好者,一直在玩单机比较枯燥,所以尝试写一个联网的东西。Photon服务器非常适合我们这些萌新爱好者,它为什么创建好了服务框架,我们只需要编写一些库文件就能创造属于自己的服务器。

言归正传,Photon服务器怎么安装我就不多说。我们来讲讲怎么创建服务器吧。
首先我们需要创建一个项目,选择Visual C#——类库。
这里写图片描述
创建完成后,我们导入Photon库文件。找到Photon安装目录,进入库目录(…/Photon/lib),导入库文件:
-ExitGames.Logging.Log4Net.dll
-ExitGamesLibs.dll
-log4net.dll
-Photon.SocketServer.dll
-PhotonHostRuntimeInterfaces.dll
完成后,创建两个类MyServer(服务端),MyClient(客户端)。
MyServer继承ApplicationBase,MyClient继承ClientPeer。

using Photon.SocketServer;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using PhotonHostRuntimeInterfaces;namespace PhotonServer{    public class MyServer : ApplicationBase    {        /// <summary>        ///客户端创建连接        /// </summary>        /// <param name="initRequest"></param>        /// <returns></returns>        protected override PeerBase CreatePeer(InitRequest initRequest)        {            return new MyClient(initRequest);        }        /// <summary>        /// 服务器开启        /// </summary>        protected override void Setup()        {        }        /// <summary>        /// 服务器关闭        /// </summary>        protected override void TearDown()        {        }    }}
using Photon.SocketServer;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using PhotonHostRuntimeInterfaces;namespace PhotonServer{    public class MyClient : ClientPeer    {        /// <summary>        /// 初始化        /// </summary>        /// <param name="initRequest"></param>        public MyClient(InitRequest initRequest) : base(initRequest)        {        }        /// <summary>        /// 客户端断开        /// </summary>        /// <param name="reasonCode"></param>        /// <param name="reasonDetail"></param>        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)        {        }        /// <summary>        /// 客户端请求        /// </summary>        /// <param name="operationRequest"></param>        /// <param name="sendParameters"></param>        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)        {        }    }}

为了以后编程调试方便,我们可以在MyServer.cs中加入Log模块,并且导入log4net.config文件。改文件可以在Photon的其他案例中找到。
※※※导入log4net.config后,必须把该文件属性中改为始终复制。(非常关键,否则日志不输出)

        private static ILogger log = ExitGames.Logging.LogManager.GetCurrentClassLogger();        //创建一个静态方法来输出日志        public static void Log(string str){log.Info(str.ToString());}        //初始化日志        protected virtual void InitLogging()        {            ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");            GlobalContext.Properties["LogFileName"] = this.ApplicationName;            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));        }

这样一个简单的服务端完成了,后面可以根据需要添加方法。
接下里我们需要在Photon安装目录下,deploy文件夹下创建文件夹PhotonServer/bin父子文件夹。
然后将项目PhotonServer的生成目录改成bin文件夹所在的目录,并生成。
这里写图片描述
完成后找到deploy/bin_Win64文件夹下PhotonServer.config文件。添加如下代码

<PhotonServer        MaxMessageSize="512000"        MaxQueuedDataPerPeer="512000"        PerPeerMaxReliableDataInTransit="51200"        PerPeerTransmitRateLimitKBSec="256"        PerPeerTransmitRatePeriodMilliseconds="200"        MinimumTimeout="5000"        MaximumTimeout="30000"        DisplayName="PhotonServer"        >        <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->        <!-- Port 5055 is Photon's default for UDP connections. -->        <UDPListeners>            <UDPListener                IPAddress="0.0.0.0"                Port="5255"                OverrideApplication="PhotonServer">            </UDPListener>        </UDPListeners>        <!-- Defines the Photon Runtime Assembly to use. -->        <Runtime            Assembly="PhotonHostRuntime, Culture=neutral"            Type="PhotonHostRuntime.PhotonDomainManager"            UnhandledExceptionPolicy="Ignore">        </Runtime>        <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->        <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->        <Applications Default="PhotonServer">            <!-- MMO Demo Application -->            <Application                Name="PhotonServer"                BaseDirectory="PhotonServer"                Assembly="PhotonServer"                Type="PhotonServer.MyServer"                ForceAutoRestart="true"                WatchFiles="dll;config"                ExcludeFiles="log4net.config">            </Application>        </Applications>    </PhotonServer>

这样运行该文件夹下PhotonControl.exe,选择PhotonServer/Start as application就可以开启服务器了。
这里写图片描述

0 0
原创粉丝点击