卜若的代码笔记-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()
        {

            
            
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值