Photon開發實戰(2)——開發框架、第一個Photon程序

Photon基礎開發框架

Photon (v4)的基本框架。開發框架主要Photon和遊戲邏輯(C#)兩個部分,以下圖最新的Photon v4支持的4種底層協議,遊戲開發邏輯Photon目前主要劃分爲Load Balancing 和MMO(大型多人同時在線遊戲)。服務器

 

1、Photon服務端示例

一、服務端開發

新建解決方案TestPhotonServer並新建類庫項目MyPhotonServer,類庫添加Photon引用(可在photon安裝目錄的lib裏找到)框架

Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll異步

爲何新建類庫項目呢?全部的Photon的服務端程序都是先編譯成dll,再由PhotonControl.exe經過配置文件調用運行的。ide

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;

namespace MyPhotonServer
{
    public class MyServerApplication:ApplicationBase
    {
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new MyServerPeer(initRequest);
        }

        protected override void Setup()
        {
            //初始化
            
        }

        protected override void TearDown()
        {
            //關閉
        }
    }
}
MyServerApplication

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

namespace MyPhotonServer
{
    public class MyServerPeer:ClientPeer
    {
        public MyServerPeer(InitRequest initRequest)
            : base(initRequest)
        {
            
        }
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            //響應客戶端的斷開鏈接
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            //響應客戶端的操做請求

        }
    }
}
MyServerPeer

 

看代碼,這裏是一個最簡單的Photon服務端:函數

一、Application爲服務端程序入口,全部開發者本身的程序都要有一個繼承ApplicationBase的類的程序入口spa

二、Peer爲服務端與客戶端的對等通訊點,服務端和客戶端都經過各自的Peer進行通訊.3d

三、V4版本的Photon對.Net的開發API作了調整,在原來的PeerBase基礎上又更加細化出不一樣分工的Peer,這裏調用ClientPeer,能夠看到官方源碼裏並ClientPeer並無什麼東西,細心的朋友能夠思考爲何這麼作日誌

public abstract class ClientPeer : PeerBase
{
    // Methods
    protected ClientPeer(InitRequest initRequest) : base(initRequest)
    {
    }
}

 

二、服務端部署

一、PhotonControl.exe:首先全部的Photon的服務端程序都是先編譯成dll,再經過配置由PhotonControl.exe調用運行的。code

二、PhotonServer.config文件:PhotonControl的運行目錄中會找到這個文件,主要進行配置開發者程序來給PhotonControl調用。blog

三、log:PhotonControl.exe會在運行根目錄生成日誌,另外會在deploy下生成全部服務端的一個日誌文件log。

部署:

deploy目錄下建一個文件夾TestPhotonServer, 右鍵咱們的服務端類庫項目屬性將Release的dll重定向下。注意要把dll設置在TestPhotonServer裏新建的bin目錄裏

(因爲示例很簡單Release時候Photon.SocketServer.dll、PhotonHostRuntimeInterfaces.dll、ExitGamesLibs.dll這幾個dll沒有發佈到bin,這裏手動複製到deploy下的TestPhotonServer/bin裏面)

 

 

配置PhotonServer.config

 添加Application節點到PhotonServer.config的Applications下,這裏我放到loadBlancing下的Applications

      <Application
                Name="TestPhotonServer"
                BaseDirectory="TestPhotonServer"
                Assembly="MyPhotonServer"
                Type="MyPhotonServer.MyServerApplication"
                ForceAutoRestart="true"
                WatchFiles="dll;config"
                ExcludeFiles="log4net.config">
      </Application>        

 

Name:服務器程序名稱

BaseDirectory:設置的是deploy目錄爲基礎設置,這裏服務端程序文件夾在deploy裏的TestPhotonServer

Assembly:Application入口程序所在的namespace

Type:入口類的完整限定性名稱

ForceAutoRestart:顧名思義強制重啓

WatchFiles:調用的文件後綴,dll和config

ExcludeFiles:通常是日誌配置文件名稱

 運行PhotonControl.exe的loadBalancing就能夠看到自定義的服務端已經運行

 

2、客戶端

客戶端暫時用簡單的控制檯,解決方案下添加一個控制檯項目, 添加引用Photon3DotNet.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Client.Photon;

namespace MyPhotonClient
{
    class MyPhotonClientPeerListener : IPhotonPeerListener
    {
        public bool IsConnect = false;

        public void DebugReturn(DebugLevel level, string message)
        {
           
        }

        public void OnEvent(EventData eventData)
        {
            
        }

        public void OnMessage(object messages)
        {
            
        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            //與服務器鏈接狀態發生改變

            Console.WriteLine("當前與服務端鏈接狀態:"+statusCode);

            switch (statusCode)
            {
                case StatusCode.Connect:
                    IsConnect = true;
                    break;

            }
            
        }
    }
}
MyPhotonClientPeerListener
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExitGames.Client.Photon;

namespace MyPhotonClient
{
    class Program
    {
        static void Main(string[] args)
        {
            MyPhotonClientPeerListener listener = new MyPhotonClientPeerListener();

            PhotonPeer peer = new PhotonPeer(listener, ConnectionProtocol.Udp);

            if (peer.Connect("localhost:5055","MyServer"))
            {
                Console.WriteLine("客戶端準備鏈接請求……");

                while (!listener.IsConnect)
                {
                    Console.WriteLine("鏈接中……");
                    peer.Service();
                    System.Threading.Thread.Sleep(500);
                    
                }
                Console.WriteLine("已鏈接……");


                //peer.Disconnect();



                Console.ReadKey();

            }
            else
            {
                Console.Write("未找到服務器");
            }

        }
        
    }
}
Program

 

 

一、IPhotonPeerListener接口主要有5個方法

DebugReturn方法:主要提供各種錯誤與警告【供開發者】查看,在開發狀態下協助開發者糾錯。好比:講上面客戶端Program.cs裏的地址localhost:5055,改爲localhost:5050運行的時候仍是會不停的請求,可是沒法成功鏈接,程序是不會報錯的。這個時候咱們在DebugReturn方法裏打印一下message幫助查找問題源

OnEvent(EventData eventData):處理Photon Server發送過來給客戶端處理的事件。Event用於客戶端和服務端溝通,操做(Operation)一般會觸發Event,能夠經過Event Code直到事件類型。時間的消息內容一般包含着它的Parameters裏。這裏暫做簡單介紹

OnMessage(object messages):消息回調函數

OnOperationResponse(OperationResponse operationResponse):響應Operation的回調函數,好比加入遊戲房間操做,服務器會分配給每一個客戶端一個編號。這個Client的編號就能夠經過響應回調函數獲取

OnStatusChanged(StatusCode statusCode):鏈接狀態函數,當遊戲的異步操做完成活發生錯誤時候,狀態發生改變回調這個函數

 

二、PhotonPeer類

PhotonPeer主要功能是客戶端和Photon Server 通訊。能夠理解爲對等通訊點或者勉強理解爲信使。PhotonPeer經過listener和通訊協議和服務端通訊。。每一個Application均可以有多個PhotonPeer,可是每個不一樣的PhotonPeer都應該有本身listener用來監聽事件、操做、回調函數。這裏的listener就是繼承IPhotonPeerListener接口的類的實例。

peer.Connect調用的時候並不會直接去鏈接服務器,只有當peer.service()調用的時候纔會向服務器發送請求。 

後文再詳解

相關文章
相關標籤/搜索