PhotonServer(一)——服务器基础搭建
2 --> PhontonServer下载: https://www.photonengine.com/en-US/sdks#server-sdkserverserver 官方配置资料: https://doc.photonengine.com/en-us/server/current/getting-started/photon-server-in-5min 解除20人同时连接限制到100人同时连接: 下载后我们可以根据文档来进行Server的配置。刚下载并且解压下来后可以看到限制最大连接数为20人,那么这里我们可以升级到100人连接数的,首先注册并登录PhontonServer,然后再下面网址下载一个解除100人数限制的文件。https://dashboard.photonengine.com/en-US/selfhosted 下载后的文件,根据电脑系统,32位就放在PhotonServer解压下..\deploy\bin_Win32,如果是64位就放在PhotonServer解压下..\deploy\bin_Win64,我电脑是64,如下 然后我们启动PhotonControl.exe,这时候我们就可以看到最大连接数到了100人了。 创建自己的Server 我这里用的是VS 2017 完成后,要开始创建生成库,在PhotonServer解压下..\deploy中创建自己项目名称文件夹,在此文件夹中创建bin文件夹。 之后,开始开发服务器端,通过浏览,添加下载的PhotonServer中一下五个.dll文件 服务器代码: using Photon.SocketServer; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AR_RemoteServer { //所有的Server端 主类都要继承自applicationbase public class RemoteServer : ApplicationBase { //当一个客户端请求连接的时候,服务器端就会调用这个方法 //我们使用peerbase,表示和一个客户端的链接,然后photon就会把这些链接管理起来 protected override PeerBase CreatePeer(InitRequest initRequest) { return new ClientPeer(initRequest); } protected override void Setup() { } protected override void TearDown() { } } } using Photon.SocketServer; using PhotonHostRuntimeInterfaces; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AR_RemoteServer { //管理跟客户端的链接的 class ClientPeer : Photon.SocketServer.ClientPeer { public ClientPeer(InitRequest initRequest) : base(initRequest) { } //处理客户端断开连接的后续工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { } //处理客户端的请求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { } } } 至于服务端具体代码,后续我们在整合。 下面开始生成服务:配置好这些信息后,右击项目—>生成,生成后在之前设置的文件夹下就会出现.dll文件 开始配置刚才生成的Server: 首先,打开PhotonServer.config配置 开始配置我们的TCP,UDP配置。用VS打开它。可以看到LoadBalancing配置,可以根据这个写入字节的配置节点,这里我们直接复制一份出来,还是复制到PhotonServer.config下,修改一下节点就OK。 <ArRemote MaxMessageSize="512000" MaxQueuedDataPerPeer="512000" PerPeerMaxReliableDataInTransit="51200" PerPeerTransmitRateLimitKBSec="256" PerPeerTransmitRatePeriodMilliseconds="200" MinimumTimeout="5000" MaximumTimeout="30000" DirectDispatchToCLR="true" DisplayName="ArRemote"> <!-- 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="AR_RemoteServer"> </UDPListener> </UDPListeners> <!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. --> <TCPListeners> <!-- TCP listener for Game clients on Master application --> <TCPListener IPAddress="0.0.0.0" Port="4530" OverrideApplication="AR_RemoteServer" InactivityTimeout="10000"> </TCPListener> </TCPListeners> <!-- WebSocket (and Flash-Fallback) compatible listener --> <WebSocketListeners> <WebSocketListener IPAddress="0.0.0.0" Port="9090" DisableNagle="true" InactivityTimeout="10000" OverrideApplication="AR_RemoteServer"> </WebSocketListener> </WebSocketListeners> <!-- Defines the Photon Runtime Assembly to use. --> <Runtime Assembly="PhotonHostRuntime, Culture=neutral" Type="PhotonHostRuntime.PhotonDomainManager" UnhandledExceptionPolicy="TerminateProcess"> </Runtime> <!-- 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_Win64 folders. The BaseDirectory must include a "bin" folder. --> <Applications Default="AR_RemoteServer"> <Application Name="AR_RemoteServer" BaseDirectory="AR_RemoteServer" Assembly="AR_RemoteServer" Type="AR_RemoteServer.RemoteServer"> </Application> </Applications> </ArRemote> 修改部分: Application标签中,添加 ForceAutoRestart="true"表示是“否自动重启”。 最后关闭重启下Photon,就可以看到ArRemote的Application 了,启动起来。 日志输出: 打开Open logs,会弹出 输出Server启动的一些信息,如果启动出错可以在这找错误信息。另一个CLR是提示License以及最大连接数,以及运行的Photon的应用的配置信息等,可以通过CLR看出哪些配置出错了,这个是Photon的两个日志输出. 出现上面的running就启动成功。 出现上面CServerice:OnException(),表示出现错误,看后面意思修改。 扩展自己Log输出: 上边已经引入log4net.dll文件,可以去log4net的官网找这个配置怎么写,也可以在我们下载的photon里面找这个配置文件。这里我在phonton文件下的目录里面找到了配置文件log4net.config,把配置文件log4net.config复制到我们项目里面的根目录下并且将它设置为始终复制,然后打开配置文件修改里面的配置。 下面我们开始创建一个Log对象,然后初始化。在RemoteServer这个类里面定义一个Log对象 //定义一个Log对象 public static readonly ILogger log = LogManager.GetCurrentClassLogger(); //调用 log.Info("连接"); 输出日志文件在下列路径可以找到,我定义的名字是AR.Server.log 最重要的一点:自定义的输出日志必须在Server启动时初始化它的位置等信息 protected override void Setup() { //日志的初始化(定义配置文件log4net位置) //Path.Combine 表示连接目录和文件名,可以屏蔽平台的差异 // Photon: ApplicationLogPath 就是配置文件里面路径定义的属性 //this.ApplicationPath 表示可以获取photon的根目录,就是Photon-OnPremise-Server-SDK_v5\deploy这个目录 //这一步是设置日志输出的文档文件的位置,这里我们把文档放在Photon-OnPremise-Server-SDK_v5\deploy\bin_Win64\log里面 log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_win64")), "log"); //this.BinaryPath表示可以获取的部署目录就是目录Photon-OnPremise-Server-SDK_v5\deploy\AR_RemoteServer\bin FileInfo configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")); //如果这个配置文件存在 if (configFileInfo.Exists) { LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);//设置photon使用哪个日志插件 //让log4net这个插件读取配置文件 XmlConfigurator.ConfigureAndWatch(configFileInfo); } //初始化 log.Info("Server启动!");//最后利用log对象就可以输出了 } 打开自定义日志,如下 这样整个服务就配置好了,至于服务编码后续再总结