• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

53 - 配置 - Photon2 Server 简单应用 application

作者:

贺及楼

成为作者

更新日期:2024-05-11 11:56:25

作用:简单的开始,以MyGameServer为例,最终目的生成类库.dll给server用

找一个地方新建一个保存所有代码的文件夹

VS新建项目

新建项目—Visual C#—类库—写个名字MyGameServer—创建
这是dll后缀文件,不可以自己启动的

VS生成.dll方法

解决方案资源管理器—MyGameServer项目—右键—生成
可以在MyGameServer-MyGameServer-bin-Debug里生成dll文件

VS检查项目

项目MyGameServer—属性
看是否.NET4.5

创建2个文件夹,给server

Photon路径:
C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy
1、
deploy文件夹里新建文件夹MyGameServer

2、
MyGameServer文件夹里新建文件夹bin
bin不可改名字

vs修改生成路径

项目MyGameServer—属性—生成—输出—输出路径
为上面的bin
ctrl + S 保存一下

添加引用

解决方案资源管理器—MyGameServer项目—引用—右键—添加引用—浏览—浏览—目标C/D盘—Photon-OnPremise-Server-SDK_v4-0-29-11263—lib
引用5个:
ExitGamesLibs.dll—————————————Photon
Photon.StocketServer.dll——————————Photon
PhotonHostRuntimeInterfaces.dll——————-Photon
ExitGames.Logging.Log4Net.dll——————-日志的
log4net.dll————————————————-日志的

骚操作:解决方案资源管理器—MyGameServer项目—管理NuGet程序包里搜

配置log4net.config

找C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\src-server\Mmo\Photon.MmoDemo.Server
里的log4net.config—复制ctrl+c
vs—解决方案资源管理器—点击一下MyGameServer项目—黏贴ctrl+v
让log4net.config与类在一个目录

点击一下log4net.config—属性—高级—复制到输出目录—始终复制
让信息复制到C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\MyGameServer\bin
修改文件第5行
这个文档的百分号是错误的,不要复制

  1. value="\\\\\\\\\\\\\\\\\\\\\\\\\\\%property{Photon:ApplicationLogPath}\\MyGame.Server.log" /> #######修改名字mmo-MyGame

添加主类

可以删除Class1.cs
解决方案资源管理器—MyGameServer项目—Class1.cs

解决方案资源管理器—MyGameServer项目—右键—添加—新建项—类
名字一般与项目名称相同MyGameServer.cs—添加
以至于namespace与class都是MyGameServer
class MyGameServer前加上public,外界可以访问

开始部署(自己可以测试)

  • MyGameServer.cs
  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 ExitGames.Logging;
  8. using ExitGames.Logging.Log4Net;
  9. using System.IO;
  10. using log4net.Config;
  11. namespace MyGameServer
  12. {
  13. //所有的server端都要继承ApplicationBase
  14. public class MyGameServer : ApplicationBase
  15. {
  16. public static readonly ILogger log = LogManager.GetCurrentClassLogger();
  17. //当一个peer客户端请求链接的时候创建peerbase
  18. protected override PeerBase CreatePeer(InitRequest initRequest)
  19. {
  20. return new ClientPeer(initRequest);
  21. }
  22. //初始化
  23. protected override void Setup()
  24. {
  25. //日志的初始化
  26. log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
  27. FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath,"log4net.config"));
  28. if (configFileInfo.Exists)
  29. {
  30. LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//让Photon知道
  31. XmlConfigurator.ConfigureAndWatch(configFileInfo);//让log4net这个插件读取配置文件
  32. }
  33. log.Info("初始化完成");
  34. }
  35. //server端关闭的时候
  36. protected override void TearDown()
  37. {
  38. }
  39. }
  40. }

设置了日志
MyGame.Server.log————————程序自己配置的信息输出
C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\log\MyGame.Server.log

直接跳过测试

  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 ExitGames.Logging;
  8. using ExitGames.Logging.Log4Net;
  9. using System.IO;
  10. using log4net.Config;
  11. using MyGameServer.Manager;
  12. using Common;
  13. using MyGameServer.Handler;
  14. using MyGameServer.Threads;
  15. namespace MyGameServer
  16. {
  17. //所有的server端 主类都要集成自applicationbase
  18. public class MyGameServer:ApplicationBase
  19. {
  20. //日志C:\Users\chu\Desktop\Mygame\MyGameServer\MyGameServer.cs
  21. public static readonly ILogger log = LogManager.GetCurrentClassLogger();
  22. public static MyGameServer Instance//单例模式检测进来的code是什么
  23. {
  24. get;//外面只可以获取到
  25. private set;里面可以修改
  26. }
  27. public Dictionary<OperationCode, BaseHandler> HandlerDict = new Dictionary<OperationCode, BaseHandler>();
  28. public List<ClientPeer> peerList = new List<ClientPeer>();//通过这个集合可以访问到所有客户端的peer,从而向任何一个客户端发送数据
  29. private SyncPositionThread syncPositionThread = new SyncPositionThread();//实例化线程
  30. //当一个客户端请求链接的
  31. //我们使用peerbase,表示和一个客户端的链接
  32. protected override PeerBase CreatePeer(InitRequest initRequest)
  33. {
  34. log.Info("一个客户端连接过来了。。。。");
  35. ClientPeer peer = new ClientPeer(initRequest);
  36. peerList.Add(peer);//保存在服务器
  37. return peer;
  38. }
  39. //server端初始化的时候
  40. protected override void Setup()
  41. {
  42. Instance = this;
  43. // 日志的初始化
  44. log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
  45. FileInfo configFileInfo = new FileInfo( Path.Combine( this.BinaryPath ,"log4net.config"));
  46. if (configFileInfo.Exists)
  47. {
  48. LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//让photon知道
  49. XmlConfigurator.ConfigureAndWatch(configFileInfo);//让log4net这个插件读取配置文件
  50. }
  51. log.Info("服务器开启了!");
  52. InitHandler();//初始化-收信息
  53. syncPositionThread.Run();//线程开启
  54. }
  55. public void InitHandler()
  56. {
  57. //登录
  58. LoginHandler loginHandler = new LoginHandler();
  59. HandlerDict.Add(loginHandler.OpCode, loginHandler);
  60. //错误找不到时用一个默认的来处理
  61. DefaultHandler defaultHandler = new DefaultHandler();
  62. HandlerDict.Add(defaultHandler.OpCode, defaultHandler);
  63. //注册
  64. RegisterHandler registerHandler = new RegisterHandler();
  65. HandlerDict.Add(registerHandler.OpCode, registerHandler);
  66. //位置更新
  67. SyncPositionHandler syncPositionHandler = new SyncPositionHandler();
  68. HandlerDict.Add(syncPositionHandler.OpCode, syncPositionHandler);
  69. //其他玩家的信息
  70. SyncPlayerHandler syncPlayerHandler = new SyncPlayerHandler();
  71. HandlerDict.Add(syncPlayerHandler.OpCode, syncPlayerHandler);
  72. }
  73. //server端关闭的时候
  74. protected override void TearDown()
  75. {
  76. syncPositionThread.Stop();//线程结束
  77. log.Info("服务器应用关闭了");
  78. }
  79. }
  80. }

ClientPeer.cs(测试版)

  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. namespace MyGameServer
  9. {
  10. public class ClientPeer:Photon.SocketServer.ClientPeer
  11. {
  12. public ClientPeer(InitRequest initRequest) : base(initRequest)
  13. {
  14. }
  15. //客户端断开连接
  16. protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
  17. {
  18. }
  19. //客户端连接
  20. protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
  21. {
  22. switch (operationRequest.OperationCode)//通过OperationCode区分请求
  23. {
  24. case 1:
  25. MyGameServer.log.Info("收到客户端发送的一个请求");
  26. //得到参数
  27. Dictionary<byte, object> data = operationRequest.Parameters;
  28. object intValue;
  29. data.TryGetValue(1, out intValue);
  30. object stringValue;
  31. data.TryGetValue(2, out stringValue);
  32. MyGameServer.log.Info("参数:"+ intValue.ToString()+ stringValue.ToString());
  33. //加上参数回去
  34. OperationResponse opResponse = new OperationResponse(1);
  35. Dictionary<byte, object> data2 = new Dictionary<byte, object>();
  36. data2.Add(1, 100);//加数据
  37. data2.Add(2, "aaBB中文");//加数据
  38. opResponse.SetParameters(data2);
  39. SendOperationResponse(opResponse, sendParameters);//给客户端一个响应
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. }
  46. }

ClientPeer.cs(跳过测试版)

  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 Common.Tools;
  8. using Common;
  9. using MyGameServer.Handler;
  10. //客户端
  11. namespace MyGameServer
  12. {
  13. public class ClientPeer:Photon.SocketServer.ClientPeer
  14. {
  15. public float x, y, z;//位置信息
  16. public string username;
  17. public ClientPeer(InitRequest initRequest):base(initRequest)
  18. {
  19. }
  20. //处理客户端断开链接的后续工作
  21. protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)
  22. {
  23. MyGameServer.Instance.peerList.Remove(this);
  24. }
  25. //处理客户端的请求
  26. protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
  27. {
  28. //实例化(获得请求码)
  29. BaseHandler handler = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict, (OperationCode)operationRequest.OperationCode);//MyGameServer的字典和事情码组成字典
  30. if (handler != null)
  31. {
  32. handler.OnOperationRequest(operationRequest, sendParameters, this);
  33. }
  34. else
  35. {
  36. BaseHandler defaultHandler = DictTool.GetValue<OperationCode, BaseHandler>(MyGameServer.Instance.HandlerDict, OperationCode.Default);
  37. defaultHandler.OnOperationRequest(operationRequest, sendParameters, this);
  38. }
  39. }
  40. }
  41. }

生成dll

重启一下Photon server

C:\Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64\PhotonControl

托盘的小蓝star开启自己的程序

托盘的小蓝open log可以看到有没有启动成功

1、有没有”初始化完成”
2、Server is running