photonserver版本:4.0.28.2962 VS2019
一 . 启动PhotonServer 安装目录下找到PhotonConTrol.exe打开 桌面右下角出现photon图标
二 . 创建服务器端项目,并设置目录部署 1.打开vs新建解决方案 MyGameServer 2.修改目标框架为 .NET Framework 4.6.1 太高怕photon支持不了 3.在photon安装目录中找到deploy文件夹,新建MyGameServer/bin目录,设置VS输出路径到此目录
4.设置引用 在libs文件夹中找到这5个dll, 5.新建MyGameServerMain类继承ApplicationBase,实现接口,需要引入Photon.SocketServer; 新建ClientPeer,继承Photon.SocketServer.ClientPeer,实现接口 .点击生成dll 6. 找到PhotonServer.config.用vs打开 添加配置 7.在configuration根下添加如下
<!-- Instance settings -->
<MyGameInstance <!-- 自定义Instance -->
MaxMessageSize="512000"
MaxQueuedDataPerPeer="512000"
PerPeerMaxReliableDataInTransit="51200"
PerPeerTransmitRateLimitKBSec="256"
PerPeerTransmitRatePeriodMilliseconds="200"
MinimumTimeout="5000"
MaximumTimeout="30000"
DisplayName="My Game" <!-- 1.Photon中显示的名字 -->
>
<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
<!-- Port 5055 is Photon's default for UDP connections. -->
<UDPListeners>
<UDPListener
IPAddress="0.0.0.0"
Port="5055"
OverrideApplication="MyGame1"> <!-- 2.应用程序名 ,自定义 -->
</UDPListener>
</UDPListeners>
<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
<!-- Port 4530 is Photon's default for TCP connecttions. -->
<!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) -->
<TCPListeners>
<TCPListener
IPAddress="0.0.0.0"
Port="4530"
PolicyFile="Policy\assets\socket-policy.xml"
InactivityTimeout="10000"
OverrideApplication="MyGame1" <!-- 3.应用程序名 ,自定义 -->
>
</TCPListener>
</TCPListeners>
<!-- Defines the Photon Runtime Assembly to use. -->
<Runtime
Assembly="PhotonHostRuntime, Culture=neutral"
Type="PhotonHostRuntime.PhotonDomainManager"
UnhandledExceptionPolicy="Ignore">
</Runtime>
<!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->
<!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->
<Applications Default="MyGame1"> <!--应用程序名 ,自定义 -->
<!-- MMO Demo Application -->
<Application
Name="MyGame1" <!-- 应用程序名 ,自定义 -->
BaseDirectory="MyGameServer" <!-- 在deploy文件夹下应用程序所在的文件夹名称 -->
Assembly="MyGameServer"<!-- 程序集名字 -->
Type="MyGameServer.MyGameServerMain"<!-- 配置的主要的脚本 -->
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</MyGameInstance><!-- 自定义Instance -->
8.新建`MyGameServerMain作为主要脚本 ,具体内容
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
using Photon.SocketServer;
using System.IO;
namespace MyGameServer
{
public class MyGameServerMain : ApplicationBase
{
public static readonly ILogger log = LogManager.GetCurrentClassLogger();
protected override PeerBase CreatePeer(InitRequest initRequest)
{
log.Info("一个客户端连接过来了");
return new ClientPeer(initRequest);
}
protected override void Setup()
{
log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =Path .Combine ( Path.Combine(this.ApplicationRootPath , "bin_Win64"),"log");
FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
if (configFileInfo.Exists )
{
LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
XmlConfigurator.ConfigureAndWatch(configFileInfo);
}
log.Info(" Set Log4 ok !");
}
protected override void TearDown()
{
log.Info("服务器应用关闭了");
}
}
}
9.新建clientpeer具体如下
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using System.Collections.Generic;
namespace MyGameServer
{
public class ClientPeer : Photon.SocketServer.ClientPeer
{
public ClientPeer (InitRequest initRequest ):base (initRequest) { }
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
}
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
switch (operationRequest .OperationCode)
{
case 1:
MyGameServerMain.log.Info("收到了一个客户端的请求 ");
OperationResponse operation = new OperationResponse(1);
Dictionary<byte, object> data = operationRequest.Parameters;
object x;
data.TryGetValue( 1,out x);
object y;
data.TryGetValue(2, out y);
MyGameServerMain.log.Info("得到的参数数据是 : " + x .ToString ()+ " + " + y.ToString ());
Dictionary<byte, object> data2 = new Dictionary<byte, object>();
data2.Add(1, 2020);
data2.Add(2, "回复你消息");
operation.SetParameters(data2);
SendOperationResponse(operation, sendParameters);
EventData eventData = new EventData(1);
eventData.Parameters = data2;
SendEvent(eventData, new SendParameters());
break;
case 2:
break;
default:
break;
}
}
}
}
10.配置log4gnet.config 找到log4gnet.config.xml 复制到MyGameServer下 修改输出的日志文本文档名 为MyGameLog4
二.新建Unity项目 1.新建Plugins 文件夹 在libs文件夹中找到Photon3Unity3D.dll导入 2.新建unity脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon;
public class PhotonEngine : MonoBehaviour,IPhotonPeerListener
{
public static PhotonEngine Instance = null;
private static PhotonPeer peer;
public static PhotonPeer Peer
{
get { return peer; }
}
public void DebugReturn(DebugLevel level, string message)
{
throw new System.NotImplementedException();
}
public void OnEvent(EventData eventData)
{
switch (eventData.Code )
{
case 1:
Debug.Log("收到客户端发来的一个事件");
Dictionary<byte, object> data = eventData.Parameters;
object x;
data.TryGetValue(1, out x);
object y;
data.TryGetValue(2, out y);
Debug.Log(x.ToString() + y.ToString());
break;
default:
break;
}
}
public void OnOperationResponse(OperationResponse operationResponse)
{
switch (operationResponse .OperationCode )
{
case 1:
Debug.Log("收到客户端的回复");
Dictionary<byte, object> data = operationResponse.Parameters;
object x;
data.TryGetValue(1, out x);
object y;
data.TryGetValue(2, out y);
Debug.Log(x.ToString() + y.ToString());
break;
}
}
public void OnStatusChanged(StatusCode statusCode)
{
Debug.Log(statusCode);
}
private void Awake()
{
Instance = this;
}
private void Start()
{
peer = new PhotonPeer(this, ConnectionProtocol.Udp);
peer.Connect("127.0.0.1:5055","MyGame1");
}
private void Update()
{
peer.Service();
}
private void OnDestroy()
{
if (peer!=null &&peer .PeerState ==PeerStateValue.Connected )
{
peer.Disconnect();
}
}
}
新建test脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
void Update()
{
if (Input .GetKeyDown (KeyCode.Space))
{
SendRequset();
}
}
private void SendRequset()
{
Debug.Log("向服务器端发送了一条请求");
Dictionary<byte, object> data = new Dictionary<byte, object>();
data.Add(1, 100);
data.Add(2, "传送数据");
PhotonEngine.Peer.OpCustom(1 ,data ,true );
}
}
三.测试 服务器启动 打开log日志 启动服务器 启动unity 测试成功
|