记录自SIKI学院PhotonServer游戏服务端教程
目录
1.下载
解压后可以看到
lib下有一些库,比如u3d客户端使用的Photon3Unity3D.dll和日志 log4net.dll
deploy--->>bin_Win64--->>PhotonControl.exe,服务端的打开是在PhotonControl.exe操作
2.建立服务器应用类库
在deploy内建一个文件,如MyGameServer,然后放置自己的服务器应用
C#建一个项目为类库作为服务器应用
将项目属性中的生成指定到这个文件夹MyGameServer--->>bin下
3.配置PhotonServer.config
此时要配置PhotonServer,才能使得PhotonControl.exe识别到你的应用,从而能够操作
在\bin_Win64下找到PhotonServer.config 在VS打开,稍微折叠一下应该可以看到两个示例
一个是LoadBalancing,另一个是MMoDemo,随便原地复制一个进行修改
<!-- Instance settings -->
<MyGameInstance <!-- 自己定义一个名字 -->
MaxMessageSize="512000"
MaxQueuedDataPerPeer="512000"
PerPeerMaxReliableDataInTransit="51200"
PerPeerTransmitRateLimitKBSec="256"
PerPeerTransmitRatePeriodMilliseconds="200"
MinimumTimeout="5000"
MaximumTimeout="30000"
DisplayName="My Game" <!-- 此处就是在PhotonControl显示的名称 -->
>
<!-- 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>
PhotonControl.exe打开,在系统任务栏右下角,对它右键可以看到,MyGame就是我配置的
4.添加引用,建立服务端主类
然后开始建立主类,建立为MyGameServer,继承ApplicationBase,实现三个抽象方法
protected abstract void Setup();//服务器开启时,可用于初始化,如日志
protected abstract PeerBase CreatePeer(InitRequest initRequest);//客户端请求连接时,返回值为客户端类
protected abstract void TearDown();//服务器关闭时执行
5.服务端这边处理的客户端类
建立一个客户端类,如ClientPeer,继承Photon.SocketServer.ClientPeer
//构造函数
public ClientPeer(InitRequest initRequest):base(initRequest){}
//客户端断开连接时
protected void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
//客户端发来请求
protected void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
6.消息请求和回复
客户端发来请求
OperationRequest
Parameters, 即Dictionary<byte, object> 请求传递的字典,也就是数据
OperationCode 标识请求的类型,或者说消息头
服务端回复
OperationResponse
OperationCode byte 消息头
ReturnCode short 可用于仅表示成功或者失败之类的参数
Parameters Dictionary<byte, object>
ClientPeer的基类是PeerBase, 服务端回复客户端时,由服务器这边的客户端类调用方法
SendOperationResponse(OperationResponse operationResponse, SendParameters sendParameters);