photon 服务器操作系统,PhotonServer游戏服务器部署及日志输出之美

本系列教程介绍游戏服务器端框架----PhotonServer

开发工具:Unity3d2017、VS2017、PhotonServer SDK

首先到官网下载其SDK,下载地址

服务器部署

将下载下来的SDK解压

目录结构如下

031ba9feb21aa2647d76608fe6018431.png

deploy:部署程序

doc:文档

lib:存放类库,在接下来的客户端(Unity3d)和服务器端开发需要引用到

src-server:Demo源码

接下来开始部署自己的游戏服务器

打开VS,新建类库

eecc095128877124fe72463218a0662b.png

在项目中引用lib目录下的如下图五个类库

4fe03b86088b693fb097eaa9874c9848.png

在项目中新建入口类,这里命名为MyGameServer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Photon.SocketServer;

using System.IO;

using ExitGames.Logging;

using ExitGames.Logging.Log4Net;

using log4net.Config;

namespace MyGameServer

{

//服务器框架主类 框架入口

class MyGameServer : Photon.SocketServer.ApplicationBase

{

//单例模式

public static ILogger LOG = LogManager.GetCurrentClassLogger();

//当有客户端接入时候调用

protected override PeerBase CreatePeer(InitRequest initRequest)

{

return new ClientPeer(initRequest);

}

//当框架启动时候调用

protected override void Setup()

{

}

//当框架停止时候调用

protected override void TearDown()

{

}

}

}

新建客户端类,这里命名为ClientPeer

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Photon.SocketServer;

using PhotonHostRuntimeInterfaces;

namespace MyGameServer

{

class ClientPeer : Photon.SocketServer.ClientPeer

{

public ClientPeer(InitRequest ir) : base(ir) { }

//该客户端断开连接

protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)

{

}

//该客户端出操作请求

protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)

{

}

}

}

这里一个简单的服务端程序写好了,在PhotonServer的depoly目录下新建一个文件夹,这里命名为MyGame,再创建一个子目录,命名为bin

在右键项目->属性,将项目生成路径配置到刚刚创建的bin目录下

1239e999f32a98b8a02d9abc38f23e94.png

右键项目->点击生成

9f1105466505fb16aceddb1e86305a51.png

在MyGame->bin目录下生成执行程序

dbfbdffa170eea58665660666f501cda.png

配置文件:在bin_Win64打开PhontonServer.config文本文件(XML)

105e6f4d23b3eaa3341efb50a5f2373b.png

打开配置文件,发现里面有两个示列程序,复制粘贴一个做为自己的

b7f7161180c003edb40128d5abea3a33.png

MaxMessageSize="512000"

MaxQueuedDataPerPeer="512000"

PerPeerMaxReliableDataInTransit="51200"

PerPeerTransmitRateLimitKBSec="256"

PerPeerTransmitRatePeriodMilliseconds="200"

MinimumTimeout="5000"

MaximumTimeout="30000"

DisplayName="LoadBalancing (MyCloud)">

IPAddress="0.0.0.0"

Port="5055"

OverrideApplication="Master">

IPAddress="0.0.0.0"

Port="5056"

OverrideApplication="Game">

IPAddress="0.0.0.0"

Port="4530"

OverrideApplication="Master"

PolicyFile="Policy\assets\socket-policy.xml"

InactivityTimeout="10000"

>

IPAddress="0.0.0.0"

Port="4531"

OverrideApplication="Game"

PolicyFile="Policy\assets\socket-policy.xml"

InactivityTimeout="10000">

IPAddress="0.0.0.0"

Port="4520">

IPAddress="0.0.0.0"

Port="843"

PolicyFile="Policy\assets\socket-policy.xml">

IPAddress="0.0.0.0"

Port="943"

PolicyFile="Policy\assets\socket-policy-silverlight.xml">

IPAddress="0.0.0.0"

Port="9090"

DisableNagle="true"

InactivityTimeout="10000"

OverrideApplication="Master">

IPAddress="0.0.0.0"

Port="9091"

DisableNagle="true"

InactivityTimeout="10000"

OverrideApplication="Game">

Assembly="PhotonHostRuntime, Culture=neutral"

Type="PhotonHostRuntime.PhotonDomainManager"

UnhandledExceptionPolicy="Ignore">

Name="Master"

BaseDirectory="LoadBalancing\Master"

Assembly="Photon.LoadBalancing"

Type="Photon.LoadBalancing.MasterServer.MasterApplication"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config"

>

Name="Game"

BaseDirectory="LoadBalancing\GameServer"

Assembly="Photon.LoadBalancing"

Type="Photon.LoadBalancing.GameServer.GameApplication"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config">

Name="CounterPublisher"

BaseDirectory="CounterPublisher"

Assembly="CounterPublisher"

Type="Photon.CounterPublisher.Application"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config">

MaxMessageSize="512000"

MaxQueuedDataPerPeer="512000"

PerPeerMaxReliableDataInTransit="51200"

PerPeerTransmitRateLimitKBSec="256"

PerPeerTransmitRatePeriodMilliseconds="200"

MinimumTimeout="5000"

MaximumTimeout="30000"

DisplayName="MMO Demo"

>

IPAddress="0.0.0.0"

Port="5055"

OverrideApplication="MMoDemo">

IPAddress="0.0.0.0"

Port="4530"

PolicyFile="Policy\assets\socket-policy.xml"

InactivityTimeout="10000"

OverrideApplication="MMoDemo"

>

IPAddress="0.0.0.0"

Port="843"

PolicyFile="Policy\assets\socket-policy.xml"

InactivityTimeout="10000">

IPAddress="0.0.0.0"

Port="943"

PolicyFile="Policy\assets\socket-policy-silverlight.xml"

InactivityTimeout="10000">

IPAddress="0.0.0.0"

Port="9090"

DisableNagle="true"

InactivityTimeout="10000"

OverrideApplication="MMoDemo">

Assembly="PhotonHostRuntime, Culture=neutral"

Type="PhotonHostRuntime.PhotonDomainManager"

UnhandledExceptionPolicy="Ignore">

Name="MMoDemo"

BaseDirectory="MmoDemo"

Assembly="Photon.MmoDemo.Server"

Type="Photon.MmoDemo.Server.PhotonApplication"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config">

Name="CounterPublisher"

BaseDirectory="CounterPublisher"

Assembly="CounterPublisher"

Type="Photon.CounterPublisher.Application"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config">

MaxMessageSize="512000"

MaxQueuedDataPerPeer="512000"

PerPeerMaxReliableDataInTransit="51200"

PerPeerTransmitRateLimitKBSec="256"

PerPeerTransmitRatePeriodMilliseconds="200"

MinimumTimeout="5000"

MaximumTimeout="30000"

DisplayName="MyGameServer (LJL)">

IPAddress="0.0.0.0"

Port="5055"

OverrideApplication="MYGameServer">

IPAddress="0.0.0.0"

Port="4530"

OverrideApplication="MYGameServer"

PolicyFile="Policy\assets\socket-policy.xml"

InactivityTimeout="10000"

>

IPAddress="0.0.0.0"

Port="4520">

IPAddress="0.0.0.0"

Port="843"

PolicyFile="Policy\assets\socket-policy.xml">

IPAddress="0.0.0.0"

Port="943"

PolicyFile="Policy\assets\socket-policy-silverlight.xml">

IPAddress="0.0.0.0"

Port="9090"

DisableNagle="true"

InactivityTimeout="10000"

OverrideApplication="MYGameServer">

Assembly="PhotonHostRuntime, Culture=neutral"

Type="PhotonHostRuntime.PhotonDomainManager"

UnhandledExceptionPolicy="Ignore">

Name="MYGameServer"

BaseDirectory="MyGame"

Name="CounterPublisher"

BaseDirectory="CounterPublisher"

Assembly="CounterPublisher"

Type="Photon.CounterPublisher.Application"

ForceAutoRestart="true"

WatchFiles="dll;config"

ExcludeFiles="log4net.config">

日志输出

在MyGameServer的Setup函数里添如下代码

//当框架启动时候调用

protected override void Setup()

{

//设置配置文件属性

log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");//设置日志文件存储目录

//日志配置文件

FileInfo logConfigFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));

if (logConfigFileInfo.Exists)//配置文件存在

{

//设置Photon日志插件为Log4Next

LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);

//Log4Next这个插件读取配置文件

XmlConfigurator.ConfigureAndWatch(logConfigFileInfo);

}

LOG.Info("服务器初始化完成");

}

测试

运行PhotonServer,运行MyGameServer

701fbe99a5c87345075346ca1d112443.png

发现在bin_Win64->log文件夹下成功生成了MyGameServer.log文件

acb3cc76f5334bb7d416c70a85b55fb9.png

打开日志文件,有打印,表明配置成功

c0f96797a9ebf9de86740ff74920876b.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值