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文件。

 

设置PhotonServer.config文件

 

 

 1 <MMoInstance  <!--这个Photon instances的名称-->
 2         MaxMessageSize="512000"
 3         MaxQueuedDataPerPeer="512000"
 4         PerPeerMaxReliableDataInTransit="51200"
 5         PerPeerTransmitRateLimitKBSec="256"
 6         PerPeerTransmitRatePeriodMilliseconds="200"
 7         MinimumTimeout="5000"
 8         MaximumTimeout="30000"
 9         DisplayName="MyGame"  <!--显示在Photon instances的名称-->
10         >
11         
12         <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
13         <!-- Port 5055 is Photon's default for UDP connections. -->
14         <UDPListeners>
15             <UDPListener
16                 IPAddress="0.0.0.0"
17                 Port="5055"
18                 OverrideApplication="MyGame1">"<!--指明这个端口号是给哪个Application使用的-->
19             </UDPListener>
20         </UDPListeners>
21     
22         <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
23         <!-- Port 4530 is Photon's default for TCP connecttions. -->
24         <!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> 
25         <TCPListeners>
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             </TCPListener>
34         </TCPListeners>
35 
36         <!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
37         <PolicyFileListeners>
38           <!-- multiple Listeners allowed for different ports -->
39           <PolicyFileListener
40             IPAddress="0.0.0.0"
41             Port="843"
42             PolicyFile="Policy\assets\socket-policy.xml"
43             InactivityTimeout="10000">
44           </PolicyFileListener>
45           <PolicyFileListener
46             IPAddress="0.0.0.0"
47             Port="943"
48             PolicyFile="Policy\assets\socket-policy-silverlight.xml"
49             InactivityTimeout="10000">
50           </PolicyFileListener>
51         </PolicyFileListeners>
52 
53         <!-- WebSocket (and Flash-Fallback) compatible listener -->
54         <WebSocketListeners>
55             <WebSocketListener
56                 IPAddress="0.0.0.0"
57                 Port="9090"
58                 DisableNagle="true"
59                 InactivityTimeout="10000"
60                 OverrideApplication="MyGame1">
61             </WebSocketListener>
62         </WebSocketListeners>
63 
64         <!-- Defines the Photon Runtime Assembly to use. -->
65         <Runtime
66             Assembly="PhotonHostRuntime, Culture=neutral"
67             Type="PhotonHostRuntime.PhotonDomainManager"
68             UnhandledExceptionPolicy="Ignore">
69         </Runtime>
70                 
71 
72         <!-- Defines which applications are loaded on start and which of them is used by default. Make sure the default application is defined. -->
73         <!-- Application-folders must be located in the same folder as the bin_win32 folders. The BaseDirectory must include a "bin" folder. -->
74         <Applications Default="MyGame1"><!--客户端连接服务器未指定Application时连接默认的Application-->
75         
76             <!-- MMO Demo Application -->
77             <Application
78                 Name="MyGame1"<!--应用名称-->
79                 BaseDirectory="MyGameServer"<!--\deploy下这个服务器应用的文件名称-->
80                 Assembly="MyGameServer"<!-—程序集名称-->
81                 Type="MyGameServer.MyGames"<!--主类名称-->
82                 ForceAutoRestart="true"<!--是否自动重启-->
83                 WatchFiles="dll;config"
84                 ExcludeFiles="log4net.config">
85             </Application>
86 
87         </Applications>
88     </MMoInstance>

 

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         /// <summary>
14         /// 获得日志对象   引用ExitGames.Logging命名空间
15         /// </summary>
16         public static readonly ILogger Log = LogManager.GetCurrentClassLogger();
17 
18         /// <summary>
19         /// 客户端连接请求时执行
20         /// </summary>
21         /// <param name="initRequest">客户端信息</param>
22         /// <returns></returns>
23         protected override PeerBase CreatePeer(InitRequest initRequest)
24         {
25             Log.Info("客户端连接成功!。。。。。");
26             return new ClientPeers(initRequest);
27         }
28 
29         /// <summary>
30         /// 初始化
31         /// </summary>
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         /// <summary>
47         /// 关闭时
48         /// </summary>
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<byte, object> 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 <?xml version="1.0" encoding="utf-8" ?>
 2 <log4net debug="false" update="Overwrite">
 3 
 4   <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
 5     <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" />
 6     <!--MyGame.Server修改为自己想要的日志文件名称-->
 7     <appendToFile value="true" />
 8     <maximumFileSize value="5000KB" />
 9     <maxSizeRollBackups value="2" />
10     <layout type="log4net.Layout.PatternLayout">
11       <conversionPattern value="%d [%t] %-5p %c - %m%n" />
12     </layout>
13   </appender>
14 
15   <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
16     <layout type="log4net.Layout.PatternLayout">
17       <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
18     </layout>
19     <filter type="log4net.Filter.LevelRangeFilter">
20       <levelMin value="DEBUG" />
21       <levelMax value="FATAL" />
22     </filter>
23   </appender>
24 
25   <!-- logger -->
26   <root>
27     <level value="INFO" />
28     <!--<appender-ref ref="ConsoleAppender" />-->
29     <appender-ref ref="RollingFileAppender" />
30   </root>
31 
32   <logger name="OperationData">
33     <level value="INFO" />
34   </logger>
35 
36 </log4net>

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

posted @ 2019-09-05 00:54  无名之士  阅读(1351)  评论(0编辑  收藏  举报