一、准备工作
1、安装PhotonServer服务器:https://www.photonengine.com
2、安装完成后开启测试,开启MmoDemo节点进行测试
注意!注意!注意!:关闭酷狗音乐播放器!!!(酷狗会占用Photon的端口…被坑了好久…日志信息也不好使…爱音乐有错??(笑哭))
3、测试没问题即可。
二、创建自己的Photon应用并部署到服务器
1、在VS2015中新建VS类库项目,项目名为服务器名字(我的为:PongGameServer)
2、引入Photon的三个类库文件:在解压目录下的lib文件夹下
注意引入后文件的复制本地属性要设置为True,如果为False,则设置嵌入互操作类型为False即可更改为True
3、更改项目应用程序目标框架为.Net Framework4.5,框架版本低会报错
4、在Photon目录deploy文件夹下创建对应的服务器文件夹,文件夹内再创建bin文件夹,存放生成的类库文件
5、更改项目的输出路径为上一步创建的bin文件夹目录
6、生成,测试以上操作是否正确(即bin目录下是否正确生成编译后的类库文件)
生成以下文件即可
7、PongGameServer.cs文件
using Photon.SocketServer;
namespace PongGameServer
{
public class PongGameApplication : ApplicationBase //继承接口,重写继承方法,注释不必要的异常抛出代码
{
//当有客户端链接时调用该方法
protected override PeerBase CreatePeer(InitRequest initRequest)
{
PongGamePeer pongGamePeer = new PongGamePeer(initRequest); //创建自己的Peer类对象并返回
return pongGamePeer;
}
//当服务器初始化时调用该方法
protected override void Setup()
{
//throw new NotImplementedException();
}
//关闭服务器时调用该方法
protected override void TearDown()
{
//throw new NotImplementedException();
}
}
}
8、新建PongGamePeer类,PongGamePeer.cs文件
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
namespace PongGameServer
{
//构建自己的Peer类,继承ClientPeer类即可,注释不必要的异常抛出代码
class PongGamePeer:ClientPeer
{
//构造函数
public PongGamePeer(InitRequest initRequest) : base(initRequest)
{
}
//接受客户端的请求和事件
protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
//throw new NotImplementedException();
}
//处理客户端断开连接的后续工作
protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
//throw new NotImplementedException();
}
}
}
9、打开PhotonServer.config文件,添加服务器的相关配置信息(配置文件在解压文件的对应操作平台的文件夹下,我的在bin_Win64下)
<项目名_Instance
MaxMessageSize=“512000”
MaxQueuedDataPerPeer=“512000”
PerPeerMaxReliableDataInTransit=“51200”
PerPeerTransmitRateLimitKBSec=“256”
PerPeerTransmitRatePeriodMilliseconds=“200”
MinimumTimeout=“5000”
MaximumTimeout=“30000”
DisplayName=“项目名”
>
<!-- 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="项目名">
</UDPListener>
</UDPListeners>
<!-- 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="项目名">
<Application
Name="项目名"
BaseDirectory="文件夹名"
Assembly="命名空间"
Type="命名空间.主类名"
ForceAutoRestart="true"
WatchFiles="dll;config"
ExcludeFiles="log4net.config">
</Application>
</Applications>
</项目名_Instance>
10、重新开启Photon服务器软件,然后选择检测日志信息检查配置信息是否正确
11、日志文件出现以下信息则说明服务器启动成功
12、新建Unity项目,创建在Assets文件夹下创建Plugins文件夹,拷贝Photon–>lib–>Photon3Unity3D.dll库文件到Plugins文件夹下,unity会自动引用该文件夹下的库文件
13、创建任意游戏物体,在游戏物体上挂载GameServer.cs脚本,我们将在这个文件内处理连接服务器逻辑
14、GameServer.cs文件内容
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using ExitGames.Client.Photon; //引入Photon库
5
6
7 public class GameServer : MonoBehaviour, IPhotonPeerListener //继承IPhotonPeerListener接口并实现接口
8 { 9
10 public PhotonPeer peer;
11 private string Message="";
12
13 // Use this for initialization
14 void Start()
15 {
16 peer = new PhotonPeer(this,ConnectionProtocol.Udp); //创建Peer对象
17 peer.Connect(“localhost:5055”,“PongGameServer”); //连接服务器
18 }
19
20 // Update is called once per frame
21 void Update()
22 {
23 peer.Service(); //开启监听服务
24 }
25
26 public void DebugReturn(DebugLevel level, string message)
27 {
28 // throw new System.NotImplementedException();
29 Debug.Log(“DebugReturn Message.”);
30 Debug.Log(message);
31 }
32 //服务器端向客户端发起数据的时候
33 public void OnEvent(EventData eventData)
34 {
35 // throw new System.NotImplementedException();
36 Debug.Log(“OnEvent Message.”);37
38 }
39 //客户端向服务器发起一个请求以后服务器处理完以后 就会给客户端一个响应
40 public void OnOperationResponse(OperationResponse operationResponse)
41 {
42 // throw new System.NotImplementedException();
43 Debug.Log(“OnOperationResponse Message.”);44
45 }
46 //状态改变的时候调用该方法
47 public void OnStatusChanged(StatusCode statusCode)
48 {
49 //根据服务器返回的连接状态码判断连接状态
50 switch (statusCode)
51 {
52 case StatusCode.Connect: { Debug.Log(“连接服务器成功.”); } break;
53 case StatusCode.Disconnect: { Debug.Log(“连接服务器失败.”); } break;
54 default: break;
55 }
56 }
57
58 }
15、运行unity并查看后台log信息
16、服务器端向客户端发送请求的两种方法:
(1)、OperationResponse opResponse = new OperationResponse(1);
SendOperationResponse(opResponse, sendParameters);//给客户端一个响应
客户端执行的方法为:OnOperationResponse();
(2)、EventData ed = new EventData(1);ed.Parameters=data2;
SendEvent(ed, new SendParameters());
客户端执行的方法为:OnEvent();
17、客户端向服务器端发送请求:
GamePhotonEngine.Peer.OpCustom(状态码,字典,是否为安全稳定传输模式);