经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Photon Server 服务端编程
来源:cnblogs  作者:无名之士  时间:2019/9/10 10:30:39  对本文有异议

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

 

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

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

 

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

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

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

  1. 1 <?xml version="1.0" encoding="utf-8" ?>
  2. 2 <log4net debug="false" update="Overwrite">
  3. 3
  4. 4 <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  5. 5 <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\MyGame.Server.log" />
  6. 6 <!--MyGame.Server修改为自己想要的日志文件名称-->
  7. 7 <appendToFile value="true" />
  8. 8 <maximumFileSize value="5000KB" />
  9. 9 <maxSizeRollBackups value="2" />
  10. 10 <layout type="log4net.Layout.PatternLayout">
  11. 11 <conversionPattern value="%d [%t] %-5p %c - %m%n" />
  12. 12 </layout>
  13. 13 </appender>
  14. 14
  15. 15 <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
  16. 16 <layout type="log4net.Layout.PatternLayout">
  17. 17 <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  18. 18 </layout>
  19. 19 <filter type="log4net.Filter.LevelRangeFilter">
  20. 20 <levelMin value="DEBUG" />
  21. 21 <levelMax value="FATAL" />
  22. 22 </filter>
  23. 23 </appender>
  24. 24
  25. 25 <!-- logger -->
  26. 26 <root>
  27. 27 <level value="INFO" />
  28. 28 <!--<appender-ref ref="ConsoleAppender" />-->
  29. 29 <appender-ref ref="RollingFileAppender" />
  30. 30 </root>
  31. 31
  32. 32 <logger name="OperationData">
  33. 33 <level value="INFO" />
  34. 34 </logger>
  35. 35
  36. 36 </log4net>

 

原文链接:http://www.cnblogs.com/unknown6248/p/11462702.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号