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


Photon基礎開發框架

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

 

一、Photon服務端示例

1、服務端開發

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

Photon.SocketServer.dll
PhotonHostRuntimeInterfaces.dll

為什么新建類庫項目呢?所有的Photon的服務端程序都是先編譯成dll,再由PhotonControl.exe通過配置文件調用運行的。

 

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服務端:

1、Application為服務端程序入口,所有開發者自己的程序都要有一個繼承ApplicationBase的類的程序入口

2、Peer為服務端與客戶端的對等通信點,服務端和客戶端都通過各自的Peer進行通信.

3、V4版本的Photon對.Net的開發API做了調整,在原來的PeerBase基礎上又更加細化出不同分工的Peer,這里調用ClientPeer,可以看到官方源碼里並ClientPeer並沒有什么東西,細心的朋友可以思考為什么這么做

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

 

2、服務端部署

1、PhotonControl.exe:首先所有的Photon的服務端程序都是先編譯成dll,再通過配置由PhotonControl.exe調用運行的。

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

3、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就可以看到自定義的服務端已經運行

 

二、客戶端

客戶端暫時用簡單的控制台,解決方案下添加一個控制台項目, 添加引用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

 

 

1、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):連接狀態函數,當游戲的異步操作完成活發生錯誤時候,狀態發生改變回調這個函數

 

2、PhotonPeer類

PhotonPeer主要功能是客戶端和Photon Server 通信。可以理解為對等通信點或者勉強理解為信使。PhotonPeer通過listener和通信協議和服務端通信。。每個Application都可以有多個PhotonPeer,但是每一個不同的PhotonPeer都應該有自己listener用來監聽事件、操作、回調函數。這里的listener就是繼承IPhotonPeerListener接口的類的實例。

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

后文再詳解

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM