Unity PhotonServer服务器开发(三)

客户端

Unity中

创建文件夹Plugins,导入前面解压文件夹中的插件Photon3Unity3D.dll。

创建空物体及同名脚本PhotonManager

完善代码并测试

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

public class PhotonManager : MonoBehaviour,IPhotonPeerListener
{
    public static PhotonManager Instance;
    public PhotonPeer peer;

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

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

    void Update()
    {
        peer.Service();

        //测试:消息发送
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发送消息
            Dictionary<byte, object> dic = new Dictionary<byte, object>();
            dic.Add(1,"aaa");
            peer.OpCustom(1,dic,true);
        }
    }

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

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

    public void OnEvent(EventData eventData)
    {
    }

    public void OnOperationResponse(OperationResponse operationResponse)
    {
    }

    //状态改变
    public void OnStatusChanged(StatusCode statusCode)
    {
        if (statusCode == StatusCode.Connect)
        {
            Debug.Log("连接服务器成功");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Photon.SocketServer;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;

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

        //有客户端连接时调用
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            PeerClass peer = new PeerClass(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()
        {

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

namespace TestSever
{
    public class PeerClass : ClientPeer
    {
        public PeerClass(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;
                TestSeverClass.log.Info(dic[1]);
            }
        }
    }
}

服务器脚本重新生成

重新启动PhotonControl,

打开自己添加的服务器。

打开日志。

运行Unity

按下空格

服务器响应 

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

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

        //有客户端连接时调用
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            PeerClass peer = new PeerClass(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()
        {

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

namespace TestSever
{
    public class PeerClass : ClientPeer
    {
        public PeerClass(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;
                //TestSeverClass.log.Info(dic[1]);

                //创建一个响应
                OperationResponse res = new OperationResponse(1,dic);
                SendOperationResponse(res, sendParameters);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;

public class PhotonManager : MonoBehaviour,IPhotonPeerListener
{
    public static PhotonManager Instance;
    public PhotonPeer peer;

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

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

    void Update()
    {
        peer.Service();

        //测试:消息发送
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发送消息
            Dictionary<byte, object> dic = new Dictionary<byte, object>();
            dic.Add(1,"aaa");
            peer.OpCustom(1,dic,true);
        }
    }

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

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

    //接收到服务器发来的事件
    public void OnEvent(EventData eventData)
    {
    }

    //接收到服务器发来的响应
    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("连接服务器成功");
        }
    }
}

服务器发送事件

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

namespace TestSever
{
    public class PeerClass : ClientPeer
    {
        public PeerClass(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;
                //TestSeverClass.log.Info(dic[1]);

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

                //创建一个事件
                EventData eventData = new EventData(1,dic);
                SendEvent(eventData,sendParameters);
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;

public class PhotonManager : MonoBehaviour,IPhotonPeerListener
{
    public static PhotonManager Instance;
    public PhotonPeer peer;

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

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

    void Update()
    {
        peer.Service();

        //测试:消息发送
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //给服务器发送消息
            Dictionary<byte, object> dic = new Dictionary<byte, object>();
            dic.Add(1,"aaa");
            peer.OpCustom(1,dic,true);
        }
    }

    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("连接服务器成功");
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值