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