Photon Server 服务端编程

Photon Server 和 Unity3D 数据交互:

Photon Server 服务端编程

Unity3D 客户端编程

VS2017 之 MYSQL实体数据模型

一:Photon Server的下载安装:

https://www.photonengine.com/zh-CN/sdks#server-sdkserverserver

点击下载 Download SDK(需注册登陆下载)

二:Photon Server的服务端编程:

 

1、新建项目MyGameServer,引用外部库(5个)并设置PhotonServer.config文件。

Photon Server 服务端编程_第1张图片

 

设置PhotonServer.config文件

Photon Server 服务端编程_第2张图片

 

 

 1 
 2         MaxMessageSize="512000"
 3         MaxQueuedDataPerPeer="512000"
 4         PerPeerMaxReliableDataInTransit="51200"
 5         PerPeerTransmitRateLimitKBSec="256"
 6         PerPeerTransmitRatePeriodMilliseconds="200"
 7         MinimumTimeout="5000"
 8         MaximumTimeout="30000"
 9         DisplayName="MyGame"  
10         >
11         
12         
13         
14         
15             <UDPListener
16                 IPAddress="0.0.0.0"
17                 Port="5055"
18                 OverrideApplication="MyGame1">"
19             
20         
21     
22         
23         
24          
25         
26             <TCPListener
27                 IPAddress="0.0.0.0"
28                 Port="4530"
29                 PolicyFile="Policy\assets\socket-policy.xml"
30                 InactivityTimeout="10000"
31                 OverrideApplication="MyGame1"                
32                 >
33             
34         
35 
36         
37         
38           
39           <PolicyFileListener
40             IPAddress="0.0.0.0"
41             Port="843"
42             PolicyFile="Policy\assets\socket-policy.xml"
43             InactivityTimeout="10000">
44           
45           <PolicyFileListener
46             IPAddress="0.0.0.0"
47             Port="943"
48             PolicyFile="Policy\assets\socket-policy-silverlight.xml"
49             InactivityTimeout="10000">
50           
51         
52 
53         
54         
55             <WebSocketListener
56                 IPAddress="0.0.0.0"
57                 Port="9090"
58                 DisableNagle="true"
59                 InactivityTimeout="10000"
60                 OverrideApplication="MyGame1">
61             
62         
63 
64         
65         <Runtime 66 Assembly="PhotonHostRuntime, Culture=neutral" 67 Type="PhotonHostRuntime.PhotonDomainManager" 68 UnhandledExceptionPolicy="Ignore"> 69  70 71 72  73  74  75 76  77 <Application 78 Name="MyGame1" 79 BaseDirectory="MyGameServer" 80 Assembly="MyGameServer" 81 Type="MyGameServer.MyGames" 82 ForceAutoRestart="true" 83 WatchFiles="dll;config" 84 ExcludeFiles="log4net.config"> 85  86 87  88 

 

2、新建MyGames类继承ApplicationBase作为服务器启动类,并实现其抽象方法。

 1 using System.Linq;
 2 using System.Text;
 3 using System.Threading.Tasks;
 4 using ExitGames.Logging;
 5 using Photon.SocketServer;
 6 using log4net.Config; 7 using ExitGames.Logging.Log4Net; 8 9 namespace MyGameServer 10 { 11 public class MyGames : ApplicationBase 12  { 13 ///  14 /// 获得日志对象 引用ExitGames.Logging命名空间 15 ///  16 public static readonly ILogger Log = LogManager.GetCurrentClassLogger(); 17 18 ///  19 /// 客户端连接请求时执行 20 ///  21 /// 客户端信息 22 ///  23 protected override PeerBase CreatePeer(InitRequest initRequest) 24  { 25 Log.Info("客户端连接成功!。。。。。"); 26 return new ClientPeers(initRequest); 27  } 28 29 ///  30 /// 初始化 31 ///  32 protected override void Setup() 33  { 34 log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"),"log"); 35 //引用System.IO命名空间 日志设置 36 FileInfo configInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")); 37 if (configInfo.Exists) 38  { 39 //引用ExitGames.Logging.Log4Net命名空间 40 LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance); //设置使用log4net插件 41 //引用log4net.Config命名空间 42 XmlConfigurator.ConfigureAndWatch(configInfo);//读取日志文件 43  } 44 Log.Info("初始化成功!。。。。。"); 45  } 46 ///  47 /// 关闭时 48 ///  49 protected override void TearDown() 50  { 51 Log.Info("服务器成功关闭!。。。。。"); 52  } 53  } 54 }

 

3、客户端连接类ClientPeers继承ClientPeer类并实现其抽象方法。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Photon.SocketServer; 7 using PhotonHostRuntimeInterfaces; 8 9 namespace MyGameServer 10 { 11 public class ClientPeers :ClientPeer 12  { 13 public ClientPeers(InitRequest initRequest):base(initRequest) 14  { 15  } 16 17 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) 18  { 19 MyGames.Log.Info("客户端断开连接!....."); 20  } 21 22 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) 23  { 24 //根据客户端请求类型分类 25 switch(operationRequest.OperationCode) 26  { 27 case 1: 28 //客户端数据获得 29 object i, j; 30 Dictionary date = operationRequest.Parameters; 31 date.TryGetValue(1,out i); 32 date.TryGetValue(2,out j); 33 //日志输出 34 MyGames.Log.Info(String.Format("收到一个请求!。。。。。{0},{1}",i,j)); 35 //返回客户端信息 36 OperationResponse op = new OperationResponse(1); 37 op.Parameters = date; 38 //SendOperationResponse只适用于双向交互时(即已由客户端发出请求,再有服务端返回),由服务端到客户端。 39  SendOperationResponse(op, sendParameters); 40 //单方面由服务端向客户端发送消息 41 EventData eventData = new EventData(1); 42 eventData.Parameters = date; 43  SendEvent(eventData, sendParameters); 44 break; 45 case 2: 46 break; 47 default: 48 break; 49  } 50  } 51  } 52 }

 4、引入日志配置文件log4net.config

 1 
 2 
 3 
 4   
 5     
 6     
 7     
 8     
 9     
10     
11       
12     
13   
14 
15   
16     
17       
18     
19     
20       
21       
22     
23   
24 
25   
26   
27     
28     
29     
30   
31 
32   
33     
34   
35 
36 

 下载地址:https://gitee.com/today6/unity

你可能感兴趣的:(Photon Server 服务端编程)