1. 配置日志

将【Photon-OnPremise-Server-SDK_v4-0-29-11263\src-server\Mmo\Photon.MmoDemo.Server】下的【log4net.config】文件引入项目中




unity手机游戏日志_学习


将它的属性由【不复制】改为【始终复制】


unity手机游戏日志_Powered by 金山文档_02


打开该文件,修改配置属性中的文件名为PSTest


unity手机游戏日志_unity手机游戏日志_03


  1. 使用日志

在PSTest.cs中的【PSTest】类中创建log对象,并在Setup方法配置日志的初始化方法

public static ILogger log = LogManager.GetCurrentClassLogger();

protected override void Setup()
{
//日志的初始化
log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(ApplicationRootPath, "bin_Win64/log");
            
//对photonserver日志设置为log4net
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);

//让log4net插件读取配置文件
FileInfo file = new FileInfo(Path.Combine(BinaryPath, "log4net.config"));
XmlConfigurator.ConfigureAndWatch(file);

//记入日志
log.Info("初始化完成");

}

在PSPeer.cs中可以接收客户端发来的消息并保存到日志中

//接收到客户端发来的请求
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
   if (operationRequest.OperationCode == 1)
        {
            Dictionary<byte, object> dic = operationRequest.Parameters;
            PSTest.log.Info(dic[1]);
          }
 }
  1. 接收消息

在unity中创建一个对象,挂载以下脚本

需要添加引用

using ExitGames.Client.Photon;

继承类IPhotonPeerListener,并生成4个重载方法


unity手机游戏日志_unity手机游戏日志_04


创建本类的单例和PhotonPeer对象

public static PhotoManager Instance;
public PhotonPeer peer;

在Awake中实例化并连接服务器,端口号在PhotonServer.config文件中查看。

void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);

        //连接
        peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
        peer.Connect("127.0.0.1:4530","PSTest");
    }

在Update中一直保持连接,并设置发送消息

void Update()
    {
        peer.Service();

        //测试发消息
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发消息
            Dictionary < byte,object> dic = new Dictionary<byte,object>(); 
            dic.Add(1,"你好,我是王小虎");
            peer.OpCustom(1, dic, true);

        }
    }

完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;


public class PhotoManager : MonoBehaviour,IPhotonPeerListener
{

    public static PhotoManager Instance;
    public PhotonPeer peer;

    void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);

        //连接
        peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
        peer.Connect("127.0.0.1:4530","PSTest");
    }

    void Update()
    {
        peer.Service();

        //测试发消息
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发消息
            Dictionary < byte,object> dic = new Dictionary<byte,object>(); 
            dic.Add(1,"你好,我是王小虎");
            peer.OpCustom(1, dic, true);

        }
    }

    private void OnDestroy()
    {
        peer.Disconnect();
    }

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

    //接收到服务器发来的事件
    public void OnEvent(EventData eventData)
    {
        if (eventData.Code == 1)
        {
            Debug.Log(eventData
                .Parameters[1]);
        }
    }

    //接收到服务器发来的响应
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        if (operationResponse.OperationCode == 1)
        {
            Debug.Log(operationResponse.Parameters[1]);
        }
    }

    //状态改变
    public void OnStatusChanged(StatusCode statusCode)
    {
        if (statusCode == StatusCode.Connect)
        {
            Debug.Log("链接服务器成功!");
        }
       
    }

    

  
    
}

在PSPeer.cs中的OnOperationRequest接收消息

//接收到客户端发来的请求
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            if (operationRequest.OperationCode == 1)
            {
                Dictionary<byte, object> dic = operationRequest.Parameters;
                PSTest.log.Info(dic[1]);
            }
        }
  1. 发送消息

在PSPeer.cs中的OnOperationRequest接收消息后重新发送回去

protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            if (operationRequest.OperationCode == 1)
            {
                Dictionary<byte, object> dic = operationRequest.Parameters;
                //PSTest.log.Info(dic[1]);

                //创建一个响应
                OperationResponse res = new OperationResponse(1, dic);
                SendOperationResponse(res, sendParameters);

                //创建一个事件
                EventData data = new EventData(1, dic); 
                SendEvent(data, sendParameters);


            }
        }

在unity中的脚本中接收消息

//接收到服务器发来的事件
    public void OnEvent(EventData eventData)
    {
        if (eventData.Code == 1)
        {
            Debug.Log(eventData
                .Parameters[1]);
        }
    }

    //接收到服务器发来的响应
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        if (operationResponse.OperationCode == 1)
        {
            Debug.Log(operationResponse.Parameters[1]);
        }
    }

    //状态改变
    public void OnStatusChanged(StatusCode statusCode)
    {
        if (statusCode == StatusCode.Connect)
        {
            Debug.Log("链接服务器成功!");
        }
       
    }

完整代码

unity中PhotoManager的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;


public class PhotoManager : MonoBehaviour,IPhotonPeerListener
{

    public static PhotoManager Instance;
    public PhotonPeer peer;

    void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);

        //连接
        peer = new PhotonPeer(this,ConnectionProtocol.Tcp);
        peer.Connect("127.0.0.1:4530","PSTest");
    }

    void Update()
    {
        peer.Service();

        //测试发消息
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发消息
            Dictionary < byte,object> dic = new Dictionary<byte,object>(); 
            dic.Add(1,"你好,我是王小虎");
            peer.OpCustom(1, dic, true);

        }
    }

    private void OnDestroy()
    {
        peer.Disconnect();
    }

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

    //接收到服务器发来的事件
    public void OnEvent(EventData eventData)
    {
        if (eventData.Code == 1)
        {
            Debug.Log(eventData
                .Parameters[1]);
        }
    }

    //接收到服务器发来的响应
    public void OnOperationResponse(OperationResponse operationResponse)
    {
        if (operationResponse.OperationCode == 1)
        {
            Debug.Log(operationResponse.Parameters[1]);
        }
    }

    //状态改变
    public void OnStatusChanged(StatusCode statusCode)
    {
        if (statusCode == StatusCode.Connect)
        {
            Debug.Log("链接服务器成功!");
        }
       
    }

    

  
    
}

PSTest.cs

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

namespace PSTest
{
    public class PSTest : ApplicationBase
    {
        public static ILogger log = LogManager.GetCurrentClassLogger();

        //有客户端链接会调用
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            PSPeer peer = new PSPeer(initRequest);
            return peer;
        }

        protected override void Setup()
        {
            //日志的初始化
            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(ApplicationRootPath, "bin_Win64/log");
            
            //对photonserver日志设置为log4net
            LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
            //让log4net插件读取配置文件
            FileInfo file = new FileInfo(Path.Combine(BinaryPath, "log4net.config"));
            XmlConfigurator.ConfigureAndWatch(file);

            log.Info("初始化完成");

        }

        protected override void TearDown()
        {
            
        }
    }
}

PSPeer.cs

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

namespace PSTest
{

    public class PSPeer : ClientPeer
    {
        public PSPeer(InitRequest initRequest) : base(initRequest)
        {
        }

        //断开后调用
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
           
        }

        //接收到客户端发来的请求
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            if (operationRequest.OperationCode == 1)
            {
                Dictionary<byte, object> dic = operationRequest.Parameters;
                //PSTest.log.Info(dic[1]);

                //创建一个响应
                //  OperationResponse res = new OperationResponse(1, dic);
                //  SendOperationResponse(res, sendParameters);

                //创建一个事件
                EventData data = new EventData(1, dic); 
                SendEvent(data, sendParameters);


            }
        }
    }
}