1. 程式人生 > >卜若的程式碼筆記-photon系列-第二章:客戶端與日誌

卜若的程式碼筆記-photon系列-第二章:客戶端與日誌

1.建立Plugins資料夾

                                                                                        

2.將

Photon3Unity3D.dll 
ExitGamesLibs.dll

丟到這個檔案家裡面

                                                                                    

3.建立指令碼ClientManger


using ExitGames.Client.Photon;
using UnityEngine;

public class ClientManager : MonoBehaviour, IPhotonPeerListener
{

    private static ClientManager Instance;
    public PhotonPeer peer;
    public void connectServer()
    {

        peer = new PhotonPeer(this, ConnectionProtocol.Udp);
        peer.Connect("127.0.0.1:5055", "SavageCollision");

    }
    void Awake()
    {

        Instance = this;
    }
    void Start()
    {
        connectServer();
    }

    void Update()
    {
        peer.Service();
    }
    public void DebugReturn(DebugLevel level, string message)
    {
        
    }
    void OnApplicationQuit()
    {

        if (peer != null & peer.PeerState == PeerStateValue.Connected)
        {

            peer.Disconnect();
        }
    }


    public void OnEvent(EventData eventData)
    {
        
    }

    public void OnMessage(object messages)
    {
       
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
        
    }

    public void OnStatusChanged(StatusCode statusCode)
    {
       
    }

 

}

4.日誌:

4-1:在伺服器端新增引用

log4net.dll
ExitGames.Logging.Log4Net.dll

4.2:寫日誌初始化函式

        private void InitLogging()
        {
            ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
            GlobalContext.Properties["LogFileName"] = "SavageCollision"; //日誌名字
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
        }

4.3:寫log函式

  private static readonly ILogger logger = 
  ExitGames.Logging.LogManager.GetCurrentClassLogger();

        public static void log(string str)
        {
            logger.Info(str);
        }

4.4:複製log4net.config

 複製到

並選擇始終複製

4.5-在setup裡面初始化日誌

4-6:生成解決辦法,重啟伺服器測試日誌

4.7.連結測試

txt可以匯入到擦看日誌的那個窗口裡面就像這樣

 5.連結測試:

執行客戶端(Unity) 

 

Success 

6.伺服器端程式碼(有小修改,僅供參考)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using ExitGames.Logging.Log4Net;
using System.IO;
using log4net;
using log4net.Config;
using ExitGames.Logging;

namespace SavageCollision
{
    public class Main : ApplicationBase
    {
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {

            Log.log("有一個連結");

            return new Client(initRequest);
        }

        private void InitLogging()
        {
            ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
            GlobalContext.Properties["LogFileName"] = "SavageCollision"; //日誌名字
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
        }

        public static readonly ILogger logger = ExitGames.Logging.LogManager.GetCurrentClassLogger();

        public static void log(string str)
        {
            logger.Info(str);
        }

        protected override void Setup()
        {
            InitLogging();
            Log.log("伺服器開啟");

        }

        protected override void TearDown()
        {

            
            
        }
    }
}