前言
最近在学unity服务器的搭建,之前有做过后端ssm框架的应用开发,可以说对网站业务逻辑有一定的了解,但是在游戏服务器的开发还是一窍不通,所有选择了比较容易入门的photonServer进行学习。这个服务器有提供免费试用的版本,大家可以到photon官网下载。关于PhotonServer可以参考一下网站。
https://www.photonengine.com/zh-TW/server/pricing
https://vibrantlink.com/photonserver/
文末提供PhotonServer的百度网盘供大家下载
初窥PhotonServer
PhotonServer成功安装后,有以上的几个文件夹,主要关注目标是deploy和lib,lib是PhotonServer的类库,添加引用的时候会用到,deploy是重点关注对象,因为我们的游戏服务器应用需要部署到这个文件夹中,类似于Tomcat中的webapp文件夹。
在deploy文件夹中,有两个文件夹bin_Win32、bin_Win64,Photon服务器就在这里面,一个是64位的,一个是32位,这个相信大家都知道是怎么回事了。
还有一个配置文件,等下我们创建第一个Photon项目的时候在进行讲解。需要注意的是,PhotonControl.exe运行后不会在底部任务栏出现,只会在通知栏显示。
第一个Photon应用
博主使用的是vs2012,可以使用以上的版本。
新建项目
创建的是类库,因为我们需要将编译好的程序集打包到Photon的deploy文件中。
新建Photon项目路径
在deploy文件夹下,新建文件夹MyGameServer/bin
修改项目属性
右击项目-》属性
输出路径改为刚才新建的MyGameServer/bin,之后生成的程序会自动打包当Photon服务器中。
添加引用
添加以上几个引用。
创建服务器入口
新建两个类
Main.cs,继承自ApplicationBase类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
namespace MyGameServer {
public class Main:ApplicationBase {
private static readonly ILogger log = LogManager.GetCurrentClassLogger();
//当有客户端请求使调用,返回一个PeerBase对象
protected override PeerBase CreatePeer(InitRequest initRequest) {
return new ClientPeer(initRequest);
}
//服务器开启时调用
protected override void Setup() {
}
//服务器关闭时调用
protected override void TearDown() {
}
}
}
ClientPeer.cs,继承自Photon.SocketServer.ClientPeer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
namespace MyGameServer {
public class ClientPeer : Photon.SocketServer.ClientPeer{
public ClientPeer(InitRequest initRequest):base(initRequest) {
}
//当客户端断开连接时调用
protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail) {
}
//当客户端成功连接服务器时调用
protected override void OnOperationRequest(Photon.SocketServer.OperationRequest operationRequest, Photon.SocketServer.SendParameters sendParameters) {
}
}
}
生成程序集
右击项目——》生成,等待编译成功。
打开deploy/MyGameServer/bin,你会发现编译好的程序集已经打包到Photon服务器中
配置PhotonServer.config
在VS打开PhotonServer.config
在Configuration标签里面添加一下配置
<!-- 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">
<Application
Name="MyGame1"
BaseDirectory="MyGameService"
Assembly="MyGameServer"
Type="MyGameServer.Main"
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</MyGameInstance>
需要注意的是有几点:
DisplayName是Photon Instances:中的显示名称,单击Photon服务器的图标可以看到。
上图是服务器应用集的配置
- BaseDirectory是该服务器应用的根目录,既deploy目录下的MyGameServer文件夹名称
- Assembly是程序集名称,在项目属性中可以进行查看和修改
- Type是服务器入口程序的位置,命名空+类名,这里是Main类。
启动服务
在photon Control中启动My Game,Open Logs进行日志的查看
成功启动。
第一篇就结束了。下篇我们将学习如何为Photon服务器添加Log日志文件。
提取码:6wcc