1. 程式人生 > >Photon Server遊戲伺服器從零開始學習(一)部署第一個伺服器程式

Photon Server遊戲伺服器從零開始學習(一)部署第一個伺服器程式

概述
Photon引擎是一款實時的Socket伺服器和開發框架,快速、使用方便、容易擴充套件。

服務端架構在windows系統平臺上,採用C#語言編寫。

客戶端SDK提供了多種平臺的開發API,包括DotNet、Unity3D、C/C++以及ObjC等。

一、 PhotonServer的下載與解壓:

1.PhotonServe的官方網站https://www.photonengine.com/zh-CN/Photon ,進入到官網後點擊SDKs,選擇Server 工程,點選SeverSDK ON-PREMISES進行下載,需要註冊一個賬號。

這裡寫圖片描述

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 伺服器就搭建好了。

這裡寫圖片描述