Photon Server游戏服务器教程(一)服务器配置

发表于2018-05-02
评论0 6.2k浏览
Photon Server 是一款实时的Socket服务器和开发框架,开发非常快速,使用非常简单。服务端架构在windows系统平台上,采用C#语言编写。Photon Server客户端SDK提供了多种平台的开发API,包括DotNet、Unity3D、C/C++以及ObjC等。这个Photon Server游戏服务器教程系列主要时帮助大家去理解和使用,下面就来看看Photon Server的配置

一、PhotonServer的下载与解压:

1.PhotonServe的官方网站https://www.photonengine.com/zh-CN/Photon ,进入到官网后点击SDKs,选择Server 工程,点击SeverSDK ON-PREMISES进行下载,需要注册一个账号。

2.百度云盘,链接::https://pan.baidu.com/s/1nvyyC21 密码:brn6

3.把下载的文件解压到指定的盘符即可(文件路径最好不要含有中文),无需安装,我这里解压在D:\Program Files (x86)文件目录下。
解压后得到5个文件夹
build:编译配置有关的文件。
deploy :主要存放photon的服务器控制程序和服务端Demo。
doc:存放PhotonServer开发的相关API文档。
lib:存放PhotonServer开发的相关动态链接库。
src-server:服务端Demo源代码

根据自己的电脑系统选择deploy文件夹下的bin_Win32或者bin_Win64文件下的PhotonControl.exe,点击运行Photon。

运行后会出现在右下角的托盘里

此时的Licenses 最大连接数量为20,如若想扩展到100,需在官网下载授权文件,放在deploy文件夹下的bin_Win32或者bin_Win64目录里面,下次运行时会自动识别授权文件。

选择 Your Server 选项

点击Download下载100 CCU文件即可。

二、搭建自己的服务器

1.下面我们开始搭建自己的第一个Photon Sever 服务器端程序,在Visual Studio 2013中新建一个MyGamePhotonServer类库工程。

2.在工程中添加一下三个引用,文件在PhotonServer解压目录的lib文件夹中,三个dll分别是:ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll

3.在此工程目录中添加入口类MyGameServer,继承ApplicationBase类,并实现其接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
namespace MyGamePhotonServer
{
    /// <summary>
    /// 所有的server端 主类都要继承自ApplicationBase
    /// </summary>
    public class MyGameServer:ApplicationBase
    {
        /// <summary>
        /// 刚一个客户端请求连接的 
        /// </summary>
        /// <param name="initRequest"></param>
        /// <returns></returns>
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
           return new MyClientPeer(initRequest);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        protected override void Setup()
        {
        }
        /// <summary>
        /// server端关闭的时候
        /// </summary>
        protected override void TearDown()
        {
        }
    }
}

4.添加与客户端通信的类MyClientPeer, 需要继承ClientPeer类,并实现接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
namespace MyGamePhotonServer
{
   public class MyClientPeer:ClientPeer
    {
       public MyClientPeer(InitRequest initRequest):base(initRequest)
       { }
       /// <summary>
       /// 处理客户端断开连接后的操作
       /// </summary>
       /// <param name="reasonCode"></param>
       /// <param name="reasonDetail"></param>
        protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)
        {
        }
       /// <summary>
       /// 处理客户端的请求
       /// </summary>
       /// <param name="operationRequest"></param>
       /// <param name="sendParameters"></param>
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
        }
    }
}

5.在deploy文件夹下新建一个MyGameServer\bin文件夹,在Visual Studio 2013中把我们刚创建的服务器程序MyPhotonServer部署在PhotonServer 中。

6.配置服务器:打开bin_Win64(如果你是32的就打开bin_Win32)文件夹下的PhotonServer.config,在Application标签下添加自己服务器配置文件。
<Application
    Name="MyGame"
    BaseDirectory="MyGameServer"
    Assembly="MyGamePhotonServer"
    Type="MyGamePhotonServer.MyGameServer"
    ForceAutoRestart="true"
    WatchFiles="dll;config"
    ExcludeFiles="log4net.config">
</Application>

Name:这个就是程序名称
BaseDirectory就是我们发布后在deploy文件加下的路径:
Assembly:就是我们的类库工程生成的那个dll文件
Type:我们入口类的名称(要带上命名空间)
以下是完整的配置文件
<?xml version="1.0" encoding="Windows-1252"?>
<!--
    (c) 2015 by Exit Games GmbH, http://www.exitgames.com
    Photon server configuration file.
    For details see the photon-config.pdf.
    This file contains two configurations:
        "LoadBalancing"
                Loadbalanced setup for local development: A Master-server and a game-server.
                Starts the apps: Game, Master, CounterPublisher
                Listens: udp-port 5055, tcp-port: 4530, 843 and 943     
-->
<Configuration>
    <!-- Multiple instances are supported. Each instance has its own node in the config file. -->
    <LoadBalancing
        MaxMessageSize="512000"
        MaxQueuedDataPerPeer="512000"
        PerPeerMaxReliableDataInTransit="51200"
        PerPeerTransmitRateLimitKBSec="256"
        PerPeerTransmitRatePeriodMilliseconds="200"
        MinimumTimeout="5000"
        MaximumTimeout="30000"
        DisplayName="LoadBalancing (MyCloud)">
        <!-- 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="Master">
            </UDPListener>
            <UDPListener
                IPAddress="0.0.0.0"
                Port="5056"
                OverrideApplication="Game">
            </UDPListener>
        </UDPListeners>
        <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->       
        <TCPListeners>
            <!-- TCP listener for Game clients on Master application -->
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4530"
                OverrideApplication="Master"
                PolicyFile="Policy\assets\socket-policy.xml"
                InactivityTimeout="10000"
                >
            </TCPListener>
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4531"
                OverrideApplication="Game"
                PolicyFile="Policy\assets\socket-policy.xml"
                InactivityTimeout="10000">
            </TCPListener>
            <!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->
            <TCPListener
                IPAddress="0.0.0.0"
                Port="4520">
            </TCPListener>
        </TCPListeners>
        <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
        <PolicyFileListeners>
          <!-- multiple Listeners allowed for different ports -->
          <PolicyFileListener
            IPAddress="0.0.0.0"
            Port="843"
            PolicyFile="Policy\assets\socket-policy.xml">
          </PolicyFileListener>
          <PolicyFileListener
            IPAddress="0.0.0.0"
            Port="943"
            PolicyFile="Policy\assets\socket-policy-silverlight.xml">
          </PolicyFileListener>
        </PolicyFileListeners>
        <!-- WebSocket (and Flash-Fallback) compatible listener -->
        <WebSocketListeners>
            <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9090"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="Master">
            </WebSocketListener>
            <WebSocketListener
                IPAddress="0.0.0.0"
                Port="9091"
                DisableNagle="true"
                InactivityTimeout="10000"
                OverrideApplication="Game">
            </WebSocketListener>
        </WebSocketListeners>
        <!-- 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="Master">     
            <Application
                Name="Master"
                BaseDirectory="LoadBalancing\Master"
                Assembly="Photon.LoadBalancing"
                Type="Photon.LoadBalancing.MasterServer.MasterApplication"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config"
                >
            </Application>
            <Application
                Name="Game"
                BaseDirectory="LoadBalancing\GameServer"
                Assembly="Photon.LoadBalancing"
                Type="Photon.LoadBalancing.GameServer.GameApplication"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config">
            </Application>
            <!-- CounterPublisher Application -->
            <Application
                Name="CounterPublisher"
                BaseDirectory="CounterPublisher"
                Assembly="CounterPublisher"
                Type="Photon.CounterPublisher.Application"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config">
            </Application>  
        </Applications>
    </LoadBalancing>
  <!-- Instance settings -->
  <MyGameInstance
        MaxMessageSize="512000"
        MaxQueuedDataPerPeer="512000"
        PerPeerMaxReliableDataInTransit="51200"
        PerPeerTransmitRateLimitKBSec="256"
        PerPeerTransmitRatePeriodMilliseconds="200"
        MinimumTimeout="5000"
        MaximumTimeout="30000"
        DisplayName="MyGameDemo"
        >
    <!-- 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="MyGame">
      </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="MyGame"
                >
      </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="MyGame">
      <!-- MyGame Application -->
      <Application
                Name="MyGame"
                BaseDirectory="MyGameServer"
                Assembly="MyGamePhotonServer"
                Type="MyGamePhotonServer.MyGameServer"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config">
      </Application>
    </Applications>
  </MyGameInstance>
</Configuration>

7.启动服务器程序 MyGameDemo
到此为止我们的第一个Photon Server 服务器就搭建好了。
来自:https://blog.csdn.net/u010812661/article/details/78710548

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引