Photon Server环境搭建
1.创建类库并且程序集输出路径改为@PhotonServer\@项目文件夹名\bin
2.引入ExitGamesLibs.dll、Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll动态链接库
3.引入using Photon.SocketServer;主类继承ApplicationBase
using Photon.SocketServer;
namespace MyGameServer
{
public class MyGameServer : ApplicationBase
{
// 客户端请求连接时调用,PeerBase表示和一个客户端连接
protected override PeerBase CreatePeer(InitRequest initRequest)
{
return new ClientPeer(initRequest);
}
// 初始化
protected override void Setup()
{
}
// Server关闭时
protected override void TearDown()
{
}
}
}
- 4.创建客户端连接
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace MyGameServer
{
public class ClientPeer : Photon.SocketServer.ClientPeer
{
public ClientPeer(InitRequest init): base(init) { }
// 处理断开连接的工作
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
// 处理客户端请求
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
}
}
}
- 5.修改配置文件并部署:
<!-- 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>
- 6.利用log4net配置日志:引入log4net.dll,ExitGames.Logging.Log4Net.dll动态链接库,复制src-server中的log4net.config,将log4net.config配置文件放至项目根目录,始终复制到输出目录。之后获取log日志对象,初始化日志
<?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}\\项目log文件名.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>
using Photon.SocketServer;
using ExitGames.Logging;
using System.IO;
using ExitGames.Logging.Log4Net;
using log4net.Config;
namespace MyGameServer
{
public class MyGameServer : ApplicationBase
{
// 获取log对象
private static readonly ILogger log = LogManager.GetCurrentClassLogger();
// 客户端请求连接时调用,PeerBase表示一个客户端连接
protected override PeerBase CreatePeer(InitRequest 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)
{
// 设置log4net日志插件
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
// log4net读取配置文件
XmlConfigurator.ConfigureAndWatch(configFileInfo);
}
log.Info("Set up!");
}
// Server端关闭的时候
protected override void TearDown(){}
}
}