一、下载PhotonServer
(1)首先,打开网址:Photon Engine SDKs | Photon Engine
(2)点击下载
(3)鼠标右键解压该文件;
(4)打开C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\PhotonControl.exe,启动后可在底部任务栏找到。
二、将“最大容量20人”扩充成为“最大容量100人”
(1)下载Multiplayer Game Development Made Easy | Photon Engine
(2)将下载下来的文件放到同级目录:C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64下,然后关闭PhotonControl.exe后,重启之,即可破解为100人容量。
三、创建第一个服务器项目,并设置部署的目录
1.打开VS,创建一个类库(.NET Framework),命名为:MyGameServer; 目标框架的版本为:.NET Framework4.5(打开属性可查看修改);
2.在下载解压的Photon SDK文件夹中添加新的文件夹“MyGameServer”,在MyGameServer文件夹下创建文件夹"bin";
3.鼠标右键选择“属性”——“生成”——“输出路径”——“浏览”——选择“C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\MyGameServer\bin”;
4.鼠标右键“生成”,即可在Photon SDK指定路径下生成MyGameServer.dll;
四、开发ApplicationBase和ClientPee
1.鼠标右键“引用”——“添加引用”,浏览打开“C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\lib”,找到ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll;
2.删除“Class1.cs”,创建主类:MyGameServer.cs,创建类:ClientPeer.cs
using Photon.SocketServer;
namespace MyGameServer
{
//所有的server端,主类都要集成自applicationbase
public class MyGameServer : ApplicationBase
{
//当一个客户端请求连接的时候
//我们使用peerbase,表示和一个客户端的连接
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new ClientPeer(initRequest);
}
//初始化
protected override void Setup()
{
}
//server端关闭的时候
protected override void TearDown()
{
}
}
}
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)
{
}
}
}
五、部署并启动我们的第一个服务器端应用
1.打开“C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64”下的配置文件:PhotonServer.config;
2.复制MMoDemo的代码段,往代码下方粘贴,然后对该段代码进行修改,修改后如下:
<!-- Instance settings -->
<MyGameInstance
MaxMessageSize="512000"
MaxQueuedDataPerPeer="512000"
PerPeerMaxReliableDataInTransit="51200"
PerPeerTransmitRateLimitKBSec="256"
PerPeerTransmitRatePeriodMilliseconds="200"
MinimumTimeout="5000"
MaximumTimeout="30000"
DisplayName="My Game"
>
<!-- 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">
<!-- MMO Demo Application -->
<Application
Name="MyGame1"
BaseDirectory="MyGameServer"
Assembly="MyGameServer"
Type="MyGameServer.MyGameServer"
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</MyGameInstance>
六、配置PhotonServer中的日志Log
1.鼠标右键“引用”——“添加引用”——浏览(C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\lib)——找到log4net.dll、ExitGames.Logging.Log4Net.dll;
2.打开C:\Users\yst\Desktop\Photon-OnPremise-Server-SDK_v4-0-29-11263\src-server\Mmo\Photon.MmoDemo.Server文件夹,找到log4net.config配置文件,复制粘贴到VS中的项目MyGameServer下,将其中的一句“ <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MMO.Server.log" />”改为“ <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" />”;
3.注意,配置文件log4net.config的属性中,复制到输出目录应选择:始终复制;
MyGameServer.cs:
using Photon.SocketServer;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using System.IO;
namespace MyGameServer
{
//所有的server端,主类都要集成自applicationbase
public class MyGameServer : ApplicationBase
{
public static readonly ILogger log = LogManager.GetCurrentClassLogger();
//当一个客户端请求连接的时候
//我们使用peerbase,表示和一个客户端的连接
protected override PeerBase CreatePeer(InitRequest initRequest)
{
log.Info("一个客户端连接过来了!!!");
return new ClientPeer(initRequest);
}
//初始化
protected override void Setup()
{
//日志的初始化
log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");
FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
if(configFileInfo.Exists)
{
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//让photon知道
XmlConfigurator.ConfigureAndWatch(configFileInfo); //让log4net这个插件读取配置文件
}
log.Info("Setup Completed!");
}
//server端关闭的时候
protected override void TearDown()
{
log.Info("服务器应用关闭了!!!");
}
}
}