本系列教程介绍游戏服务器端框架----PhotonServer

原文地址:blog.liujunliang.com.cn

开发工具:Unity3d2017、VS2017、PhotonServer SDK

首先到官网下载其SDK,下载地址

服务器部署

将下载下来的SDK解压

目录结构如下

PhotonServer游戏服务器部署及日志输出之美_操作系统

deploy:部署程序

doc:文档

lib:存放类库,在接下来的客户端(Unity3d)和服务器端开发需要引用到

src-server:Demo源码


接下来开始部署自己的游戏服务器

打开VS,新建类库

PhotonServer游戏服务器部署及日志输出之美_游戏开发_02

在项目中引用lib目录下的如下图五个类库

PhotonServer游戏服务器部署及日志输出之美_游戏开发_03

 在项目中新建入口类,这里命名为MyGameServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using System.IO;
using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net.Config;
 
namespace MyGameServer
{
    //服务器框架主类 框架入口
    class MyGameServer : Photon.SocketServer.ApplicationBase
    {
 
        //单例模式
        public static ILogger LOG = LogManager.GetCurrentClassLogger();
 
        //当有客户端接入时候调用
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
            return new ClientPeer(initRequest);
        }
 
        //当框架启动时候调用
        protected override void Setup()
        {
            
        }
 
        //当框架停止时候调用
        protected override void TearDown()
        {
            
        }
    }
}

新建客户端类,这里命名为ClientPeer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
 
namespace MyGameServer
{
    class ClientPeer : Photon.SocketServer.ClientPeer
    {
        public ClientPeer(InitRequest ir) : base(ir) { }
 
        //该客户端断开连接
        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
            
        }
 
        //该客户端出操作请求
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            
        }
    }
}

这里一个简单的服务端程序写好了,在PhotonServer的depoly目录下新建一个文件夹,这里命名为MyGame,再创建一个子目录,命名为bin

在右键项目->属性,将项目生成路径配置到刚刚创建的bin目录下

PhotonServer游戏服务器部署及日志输出之美_游戏开发_04

 

 

 右键项目->点击生成

PhotonServer游戏服务器部署及日志输出之美_操作系统_05

在MyGame->bin目录下生成执行程序

PhotonServer游戏服务器部署及日志输出之美_操作系统_06

配置文件:在bin_Win64打开PhontonServer.config文本文件(XML)

PhotonServer游戏服务器部署及日志输出之美_操作系统_07

 

打开配置文件,发现里面有两个示列程序,复制粘贴一个做为自己的

PhotonServer游戏服务器部署及日志输出之美_操作系统_08

<?xml version="1.0" encoding="Windows-1252"?>
<!--
	(c) 2015 by Exit Games GmbH, http://www.exitgames.com
	Photon server configuration file.
	For details see the photon-config.pdf.
	This file contains two configurations:
		
		"LoadBalancing"
				Loadbalanced setup for local development: A Master-server and a game-server.
				Starts the apps: Game, Master, CounterPublisher
				Listens: udp-port 5055, tcp-port: 4530, 843 and 943		
	
-->
 
<Configuration>
	<!-- Multiple instances are supported. Each instance has its own node in the config file. -->
    
	<LoadBalancing
		MaxMessageSize="512000"
		MaxQueuedDataPerPeer="512000"
		PerPeerMaxReliableDataInTransit="51200"
		PerPeerTransmitRateLimitKBSec="256"
		PerPeerTransmitRatePeriodMilliseconds="200"
		MinimumTimeout="5000"
		MaximumTimeout="30000"
		DisplayName="LoadBalancing (MyCloud)">
 
		<!-- 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="Master">
			</UDPListener>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5056"
				OverrideApplication="Game">
			</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="Master"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000"
				>
			</TCPListener>
			
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4531"
				OverrideApplication="Game"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000">
			</TCPListener>
			
			<!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4520">
			</TCPListener>
		</TCPListeners>
		
		<!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
		<PolicyFileListeners>
		  <!-- multiple Listeners allowed for different ports -->
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="843"
			PolicyFile="Policy\assets\socket-policy.xml">
		  </PolicyFileListener>
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="943"
			PolicyFile="Policy\assets\socket-policy-silverlight.xml">
		  </PolicyFileListener>
		</PolicyFileListeners>
 
		
		<!-- WebSocket (and Flash-Fallback) compatible listener -->
		<WebSocketListeners>
			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9090"
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="Master">
			</WebSocketListener>
			
			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9091"
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="Game">
			</WebSocketListener>
			
		</WebSocketListeners>
 
		<!-- Defines the Photon Runtime Assembly to use. -->
		<Runtime
			Assembly="PhotonHostRuntime, Culture=neutral"
			Type="PhotonHostRuntime.PhotonDomainManager"
			UnhandledExceptionPolicy="Ignore">
		</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_win32 folders. The BaseDirectory must include a "bin" folder. -->
		<Applications Default="Master">		
			<Application
				Name="Master"
				BaseDirectory="LoadBalancing\Master"
				Assembly="Photon.LoadBalancing"
				Type="Photon.LoadBalancing.MasterServer.MasterApplication"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config"
				>
			</Application>
			<Application
				Name="Game"
				BaseDirectory="LoadBalancing\GameServer"
				Assembly="Photon.LoadBalancing"
				Type="Photon.LoadBalancing.GameServer.GameApplication"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>
			
			<!-- CounterPublisher Application -->
			<Application
				Name="CounterPublisher"
				BaseDirectory="CounterPublisher"
				Assembly="CounterPublisher"
				Type="Photon.CounterPublisher.Application"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>	
		</Applications>
	</LoadBalancing>	
	
	
	
	<!-- Instance settings -->
	<MMoDemo
		MaxMessageSize="512000"
		MaxQueuedDataPerPeer="512000"
		PerPeerMaxReliableDataInTransit="51200"
		PerPeerTransmitRateLimitKBSec="256"
		PerPeerTransmitRatePeriodMilliseconds="200"
		MinimumTimeout="5000"
		MaximumTimeout="30000"
		DisplayName="MMO Demo"
		>
		
		<!-- 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="MMoDemo">
			</UDPListener>
		</UDPListeners>
    
		<!-- 0.0.0.0 opens listeners on all available IPs. Machines with multiple IPs should define the correct one here. -->
		<!-- Port 4530 is Photon's default for TCP connecttions. -->
		<!-- A Policy application is defined in case that policy requests are sent to this listener (known bug of some some flash clients) --> 
		<TCPListeners>
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4530"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000"
				OverrideApplication="MMoDemo"				
				>
			</TCPListener>
		</TCPListeners>
 
		<!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
		<PolicyFileListeners>
		  <!-- multiple Listeners allowed for different ports -->
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="843"
			PolicyFile="Policy\assets\socket-policy.xml"
			InactivityTimeout="10000">
		  </PolicyFileListener>
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="943"
			PolicyFile="Policy\assets\socket-policy-silverlight.xml"
			InactivityTimeout="10000">
		  </PolicyFileListener>
		</PolicyFileListeners>
 
		<!-- WebSocket (and Flash-Fallback) compatible listener -->
		<WebSocketListeners>
			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9090"
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="MMoDemo">
			</WebSocketListener>
		</WebSocketListeners>
 
		<!-- Defines the Photon Runtime Assembly to use. -->
		<Runtime
			Assembly="PhotonHostRuntime, Culture=neutral"
			Type="PhotonHostRuntime.PhotonDomainManager"
			UnhandledExceptionPolicy="Ignore">
		</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_win32 folders. The BaseDirectory must include a "bin" folder. -->
		<Applications Default="MMoDemo">
		
			<!-- MMO Demo Application -->
			<Application
				Name="MMoDemo"
				BaseDirectory="MmoDemo"
				Assembly="Photon.MmoDemo.Server"
				Type="Photon.MmoDemo.Server.PhotonApplication"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>
 
			<!-- CounterPublisher Application -->
			<Application
				Name="CounterPublisher"
				BaseDirectory="CounterPublisher"
				Assembly="CounterPublisher"
				Type="Photon.CounterPublisher.Application"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>	
 
		</Applications>
	</MMoDemo>
	
 
	<MYGameServer
		MaxMessageSize="512000"
		MaxQueuedDataPerPeer="512000"
		PerPeerMaxReliableDataInTransit="51200"
		PerPeerTransmitRateLimitKBSec="256"
		PerPeerTransmitRatePeriodMilliseconds="200"
		MinimumTimeout="5000"
		MaximumTimeout="30000"
		DisplayName="MyGameServer (LJL)"><!-- 显示名称->
		<!-- 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="MYGameServer">
			</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="MYGameServer"
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000"
				>
			</TCPListener>
			
			<!-- DON'T EDIT THIS. TCP listener for GameServers on Master application -->
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4520">
			</TCPListener>
		</TCPListeners>
		
		<!-- Policy request listener for Unity and Flash (port 843) and Silverlight (port 943)  -->
		<PolicyFileListeners>
		  <!-- multiple Listeners allowed for different ports -->
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="843"
			PolicyFile="Policy\assets\socket-policy.xml">
		  </PolicyFileListener>
		  <PolicyFileListener
			IPAddress="0.0.0.0"
			Port="943"
			PolicyFile="Policy\assets\socket-policy-silverlight.xml">
		  </PolicyFileListener>
		</PolicyFileListeners>
 
		
		<!-- WebSocket (and Flash-Fallback) compatible listener -->
		<WebSocketListeners>
			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9090"
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="MYGameServer">
			</WebSocketListener>
			
		</WebSocketListeners>
 
		<!-- Defines the Photon Runtime Assembly to use. -->
		<Runtime
			Assembly="PhotonHostRuntime, Culture=neutral"
			Type="PhotonHostRuntime.PhotonDomainManager"
			UnhandledExceptionPolicy="Ignore">
		</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_win32 folders. The BaseDirectory must include a "bin" folder. -->
		<Applications Default="MYGameServer">		
			<Application
				Name="MYGameServer"
				BaseDirectory="MyGame"<!--项目路径->
				Assembly="MyGameServer"<!--程序集->
				Type="MyGameServer.MyGameServer"<!--命名空间下的入口类->
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config"
				>
			</Application>
			
			<!-- CounterPublisher Application -->
			<Application
				Name="CounterPublisher"
				BaseDirectory="CounterPublisher"
				Assembly="CounterPublisher"
				Type="Photon.CounterPublisher.Application"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>	
		</Applications>
	</MYGameServer>	
	
</Configuration>

日志输出

在MyGameServer的Setup函数里添如下代码

//当框架启动时候调用
        protected override void Setup()
        {
            //设置配置文件属性
            log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(Path.Combine(this.ApplicationRootPath, "bin_Win64"), "log");//设置日志文件存储目录
 
            //日志配置文件
            FileInfo logConfigFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
            if (logConfigFileInfo.Exists)//配置文件存在
            {
                //设置Photon日志插件为Log4Next
                LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
                //Log4Next这个插件读取配置文件
                XmlConfigurator.ConfigureAndWatch(logConfigFileInfo);
            }
 
            LOG.Info("服务器初始化完成");

         }

测试

运行PhotonServer,运行MyGameServer

PhotonServer游戏服务器部署及日志输出之美_游戏开发_09

发现在bin_Win64->log文件夹下成功生成了MyGameServer.log文件

PhotonServer游戏服务器部署及日志输出之美_游戏开发_10

打开日志文件,有打印,表明配置成功

PhotonServer游戏服务器部署及日志输出之美_游戏开发_11