天天看點

Photon伺服器引擎入門

Photon是個好東西,但是網上的入門教程太少了,特别是中文版的。小弟就自己琢磨吧,下面一系列是對Photon的研究過程,如有哪個地方寫的有誤,望請前輩指教。

解壓出來是四個檔案

deploy:主要存放photon的伺服器控制程式和服務端Demo

doc:顧名思義,文檔

lib:Photon類庫,開發服務端需要引用的

src-server:服務端Demo源代碼

今天搞一個用戶端連接配接伺服器最簡單的程式,也算是hello world吧

用戶端以Unity3d 為基礎,hello world包括配置伺服器,用戶端,用戶端連接配接伺服器,用戶端狀态改變。

第一步:配置伺服器端

打開deploy檔案夾,看到包含了不同平台編譯出的Photon目錄,以“bin_”為字首命名目錄,選擇你的電腦對應的檔案夾打開,看到PhotonControl.exe,運作後,可以在windows右下角看到一個圖示,點選圖示可以看到photon伺服器控制菜單,這個以後再做詳細介紹.

打開visual stadio,建立項目,選擇c# 類庫,應用程式名字叫做MyServer.

完成後,把我們的Class1.cs,改名為MyApplication.cs,作為伺服器端主類.然後在目前項目添加引用,連結到剛才提到的lib檔案夾目錄下,添加以下引用:

ExitGamesLibs.dll,Photon.SocketServer.dll,PhotonHostRuntimeInterfaces.dll

然後建立一個類:MyPeer.cs,寫法如下:

using System;  

using System.Collections.Generic;  

using Photon.SocketServer;  

using PhotonHostRuntimeInterfaces;  

namespace MyServer  

{  

    public class MyPeer : PeerBase  

    {  

        public  MyPeer(IRpcProtocol protocol,IPhotonPeer photonPeer)  

            : base(protocol, photonPeer)  

        {   

        }  

        protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)  

        {  

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)  

    }  

}  

接上,MyApplication.cs類這樣寫:

using System.Linq;  

using System.Text;  

    public class MyApplication : ApplicationBase  

        protected override PeerBase CreatePeer(InitRequest initRequest)  

            return new MyPeer(initRequest.Protocol, initRequest.PhotonPeer);  

        protected override void Setup()  

        protected override void TearDown()  

完成後,在解決方案資料總管中選中目前項目,打開屬性,選擇生成頁籤,把輸出路徑改成bin\,然後就生成類庫吧

複制目前項目下MyServer檔案夾到deploy檔案夾下,删除除了bin檔案夾以外其他所有檔案和檔案夾,然後文本編輯器打開deploy\bin_Win64\PhotonServer.config配置檔案(我的是win7 64位機器,就選擇這個),添加以下配置:

<Application  

                Name="MyServer"  

                BaseDirectory="MyServer"  

                Assembly="MyServer"  

                Type="MyServer.MyApplication"  

                EnableAutoRestart="true"  

                WatchFiles="dll;config"  

                ExcludeFiles="log4net.config">  

Name:項目名字

BaseDirectory:根目錄,deploy檔案夾下為基礎目錄

Assembly :是在生成的類庫中的bin目錄下與我們項目名稱相同的.dll檔案的名字

Type:是主類的全稱,在這裡是:MyServer.MyApplication,一定要包括命名空間

EnableAutoRestart:是否是自動啟動,表示當我們替換伺服器檔案時候,不用停止伺服器,替換後photon會自動加載檔案

WatchFiles和ExcludeFiles

這段代碼放在<Default><Applications>放這裡</Applications></Default>節點下面

完成後儲存,運作托盤程式deploy\bin_Win64\PhotonControl.exe,

運作它,如果托盤圖示沒有變灰,說明伺服器運作成功。

打開Unity3d編輯器,首先把Photon-Unity3D_v3-0-1-14_SDK\libs\Release\Photon3Unity3D.dll導入到Unity中,建立腳本TestConnection.cs,腳本代碼如下:

using UnityEngine;  

using System.Collections;  

using ExitGames.Client.Photon;  

public class TestConnection : MonoBehaviour,IPhotonPeerListener {  

    public PhotonPeer peer;  

    // Use this for initialization  

    void Start () {  

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

    // Update is called once per frame  

    void Update () {  

        peer.Service();  

    void OnGUI(){  

        if(GUI.Button(new Rect(Screen.width/2,Screen.height/2,200,100),"Connect")){  

            peer.Connect("localhost:5055","MyServer");  

    #region IPhotonPeerListener implementation  

    public void DebugReturn (DebugLevel level, string message)  

    public void OnOperationResponse (OperationResponse operationResponse)  

    public void OnStatusChanged (StatusCode statusCode)  

        switch(statusCode){  

        case StatusCode.Connect:  

            Debug.Log("Connect Success!");  

            break;  

        case StatusCode.Disconnect:  

            Debug.Log("Disconnect!");  

    public void OnEvent (EventData eventData)  

    #endregion  

把腳本綁定到場景中物體上,運作後可以看到一個按鈕,點選連接配接,如果連接配接成功會列印"Connect Success!".

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366470,如需轉載請自行聯系原作者

繼續閱讀