1.安装PhotonServer
(1)首先下载PhotonServer,或者点击PhotonServer直接下载
(2)双击运行PhotonServer安装包,进行解压。
(3)解压出来后,如果是64位电脑,则运行 E:PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64文件夹下的PhotonControl.exe;32位电脑则运行…\deploy\bin_Win32文件夹下的PhotonControl.exe.此时,运行如下:
(4)下载认证链接(要先去注册PhotonServer才能下载)。然后,下载的认证链接放在(3)的应用程序同级目录。此时先将Photoserver退出,再重新打开,此时,最大连接数被更新为100.
2.PhotonServer所需配置要求
再来看一下,PhotonServer开发所需要的硬件配置.具体如下:
开发版本:
For using Photon SDKs to develop your own server, the requirements for the development environment are the following:
最低配置
- Windows 8.1
- Microsoft .NET Framework 4.5
推荐配置
- Windows 10 x64
- Microsoft .NET Framework 4.7
- Microsoft Visual Studio 2017
3.PhotonServer示例项目
1.创建服务端程序
在创建服务端程序的时候,需要选择创建类库项目MyGameServer. 且该项目使用的 .net framework版本也需要与后期使用的程序框架保持一致。比如:后面使用了NHibernate插件,而NHibernate支持4.5版本的,那么创建该项目时,最好保持 .net framework版本一样,在这里避免出现不兼容的情形,我们将MyGameServer项目使用的 .net framework版本选为4.5版本。
2.配置服务端程序环境
(1)修改服务端程序输出路径
在 E:PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy文件夹下创建与上述项目名一致的文件夹MyGameServer,再在该目录下创建一个bin文件夹。
(2)修改服务端程序的属性
将输出路径修改为上面创建的文件夹路径。具体操作如下:
第一步,在创建的服务端程序上右键,点击“属性”;
第二步,再点击“生成,修改输出路径参数,将其改为上面创建的文件夹路径。
(3)引入PhotonServer所需的dll文件
在服务端程序,选择添加如下引用。这些dll文件位于PhotonServer的安装目录下面的lib文件下。具体为:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\lib\
3.服务端程序
首先,看一下服务端程序涉及PhotonServer的类.
我们创建了两个类:MyGameServer.cs和ClientPeer.cs。MyGameServer.cs为服务端程序的主类,ClientPeer.cs用于处理每个客户端的连接请求。示例代码如下:
MyGameServer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
namespace MyGameServer
{
//所有Server端 主类
//函数执行顺序:Setup()->CreatePeer()->TearDown()
public class MyGameServer : ApplicationBase
{
//当一个客户端请求连接时
//peerbase表示和一个客户端的连接 由PhotonServer完成调用
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new ClientPeer(initRequest);
}
//服务端应用启动的初始化操作 由PhotonServer完成调用
protected override void Setup()
{
}
//server端关闭 由PhotonServer完成调用
protected override void TearDown()
{
}
}
}
ClientPeer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace MyGameServer
{
//管理每个客户端的连接(类似客服)
public class ClientPeer : Photon.SocketServer.ClientPeer
{
//构造方法
public ClientPeer(InitRequest initRequest) : base(initRequest)
{
}
//断开连接
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
//处理客户端的请求
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
}
}
}
然后,右键项目名称”生成”。
4.在Photon Server上部署服务端程序
首先,打开配置文件 E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\PhotonServer.config,然后在该文件中添加一个Instance settings:
<!-- Instance settings -->
<MyGameInstance
MaxMessageSize="512000"
MaxQueuedDataPerPeer="512000"
PerPeerMaxReliableDataInTransit="51200"
PerPeerTransmitRateLimitKBSec="256"
PerPeerTransmitRatePeriodMilliseconds="200"
MinimumTimeout="5000"
MaximumTimeout="30000"
DisplayName="MyGame"
>
<!-- 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="5055"
OverrideApplication="MyGame1">
</UDPListener>
</UDPListeners>
<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
<!-- Port 4530 is Photon's default for TCP connecttions. -->
<!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) -->
<TCPListeners>
<TCPListener
IPAddress="0.0.0.0"
Port="4530"
PolicyFile="Policy\assets\socket-policy.xml"
InactivityTimeout="10000"
OverrideApplication="MyGame1"
>
</TCPListener>
</TCPListeners>
<!-- 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="MyGame1">
<!-- MyGame1 Application -->
<Application
Name="MyGame1"
BaseDirectory="MyGameServer"
Assembly="MyGameServer"
Type="MyGameServer.MyGameServer"
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</MyGameInstance>
经过上述正确配置之后,Photon Server有了如下信息,点击”Start as application”.
开启之后,再点击“Open Logs”查看输出日志。如果开启失败,则在停止结束的地方,即”Service shutting down”,从这里往上查找出错异常的地方,再根据不同的异常进行调试。下图,是正确开启成功的日志文件。
5.使用log4net调试服务端程序
(1)添加log4net.dll和ExitGames.Logging.Log4Net.dll引用
方法同上,在这里不再赘述。该dll文件也是在PhotonServer的lib文件夹下,具体路径为:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\lib
log4net.dll为插件,ExitGames.Logging.Log4Net.dll为服务端程序与log4net的中间件。
(2)引入log4net的配置文件
我们从PhotonServer里面提供的示例程序中,将log4net.config文件拷贝到我们自己创建的服务端程序中.示例程序该文件的路径为:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\src-server\Mmo\Photon.MmoDemo.Server\log4net.config.然后修改该文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false" update="Overwrite">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" /><!--只有这里被修改,其余保持默认即可-->
<appendToFile value="true" />
<maximumFileSize value="5000KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
<!-- logger -->
<root>
<level value="INFO" />
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="RollingFileAppender" />
</root>
<logger name="OperationData">
<level value="INFO" />
</logger>
</log4net>
修改完成后,将该文件属性中的”复制到输出目录” 修改为 “始终复制”。
另外,我们也可以从log4net官网上下载,根据自己需求进行配置。
(3)添加调试逻辑
MyGameServer.cs代码如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using Photon.SocketServer;
namespace MyGameServer
{
//所有Server端 主类
//函数执行顺序:Setup()->CreatePeer()->TearDown()
public class MyGameServer : ApplicationBase
{
//定义一个ILogger对象,用于日志输出
private static readonly ILogger log = LogManager.GetCurrentClassLogger();//只能初始化一次
//当一个客户端请求连接时
//peerbase表示和一个客户端的连接 由PhotonServer完成调用
protected override PeerBase CreatePeer(InitRequest initRequest)
{
log.Info("Client Request!");
return new ClientPeer(initRequest);
}
//服务端应用启动的初始化操作 由PhotonServer完成调用
protected override void Setup()
{
//日志初始化
//ApplicationRootPath为部署服务端程序目录 在这里指的是:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy
log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath,"log");//配置日志输出目录 同下
//log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = @"E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\log";//配置日志输出目录
//BinaryPath为当前程序集的目录 在这里指的是:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\MyGameServer\bin
FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
if (configFileInfo.Exists)
{
//告知Photon使用的是log4net日志插件
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
//让log4net插件读取配置文件
XmlConfigurator.ConfigureAndWatch(configFileInfo);
}
log.Info("SetUp Completed!");
}
//server端关闭 由PhotonServer完成调用
protected override void TearDown()
{
log.Info("Server is shutdown.");
}
}
}
完成之后,重启应用,即可在E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\log 目录下,看见一个新创建的文件,如图所示:
(4)日志文件说明
一个应用有三个日志输出文件:1,2为Photon生成的日志;3为自定义日志
1.PhotonCLR.log:为应用和listen的日志输出
2.Photon-ApplicationName-Time.log:为项目启动的顺序日志输出;
3.CustomName.Server.log:自己在程序当中日志输出
Photon生成的日志路径:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\log\
自定义日志输出路径:任何地方。上面存储在:E:\PhotonServerSDK\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\log\
注意的是:在程序当中拼接路径的时候,最好使用Path.Combine进行拼接,这样有利于兼容性。
参考链接:
下载Photon网址:https://www.photonengine.com/en-us/sdks#onpremiseserver
下载链接:https://www.photonengine.com/en-US/Download/Photon-Server-SDK_v4-0-29-11263.exe
第一个服务端教程:https://doc.photonengine.com/en-us/onpremise/current/app-framework/an-app-from-scratch