1. 程式人生 > >PhotonServer遊戲伺服器部署及日誌輸出

PhotonServer遊戲伺服器部署及日誌輸出

本系列教程介紹遊戲伺服器端框架----PhotonServer

開發工具:Unity3d2017、VS2017、PhotonServer SDK

首先到官網下載其SDK,下載地址

伺服器部署

將下載下來的SDK解壓

目錄結構如下


deploy:部署程式

doc:文件

lib:存放類庫,在接下來的客戶端(Unity3d)和伺服器端開發需要引用到

src-server:Demo原始碼

接下來開始部署自己的遊戲伺服器

開啟VS,新建類庫


在專案中引用lib目錄下的如下圖五個類庫


 在專案中新建入口類,這裡命名為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目錄下


 右鍵專案->點選生成


在MyGame->bin目錄下生成執行程式


配置檔案:在bin_Win64開啟PhontonServer.config文字檔案(XML)


開啟配置檔案,發現裡面有兩個示列程式,複製貼上一個做為自己的


<?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


發現在bin_Win64->log資料夾下成功生成了MyGameServer.log檔案


開啟日誌檔案,有列印,表明配置成功


下一篇--------Unity3d遊戲客戶端與Photon伺服器資料連線

更多內容持續更新!!!!!

相關推薦

PhotonServer遊戲伺服器部署日誌輸出

本系列教程介紹遊戲伺服器端框架----PhotonServer開發工具:Unity3d2017、VS2017、PhotonServer SDK首先到官網下載其SDK,下載地址伺服器部署將下載下來的SDK解壓目錄結構如下deploy:部署程式doc:文件lib:存放類庫,在接下

Nginx+Uwsgi+Django+Python伺服器部署除錯(更新0.1)

Nginx+Uwsgi+Django+Python伺服器部署 環境:Ubuntu16.04 1.python開發環境 安裝pyenv,python,virtualenv,需要使用python虛擬環境env 2.安裝配置nginx 安裝 sudo apt-get ins

weblogic 之web伺服器部署相關問題整理

寫一些不同領域的博文,希望我的博文或者經驗對不同層次的你有所幫助!下面寫一篇關於java web 應用伺服器主題的博文!相信大家在剛接觸java web 這個方向的時候,做的第一個關於java web的專案的時候使用的第一個web 應用伺服器就是(湯姆貓)Tomcat,這個

jar包在伺服器部署執行的相關命令

1.用xftp、WinSCP(我使用的)等檔案傳輸工具將jar包上傳到伺服器; 示例:我將data-collection-jar-with-dependencies.jar的jar包上傳到伺服器的/home/hdfs/data-collection目錄下 2.用xshel

伺服器部署儲存過程問題周結

一次部署,耽誤了好長時間,記錄一下,長點記性! 問題:     本地程式打包,部署到遠端伺服器,訪問同一個介面返回的資料結構不一樣; 抓蟲子:     1、可能是打的包有問題,再打一次,再打一次,結果跟之前一樣;     2、在程式中的幾個關鍵點mapParam.p

Haproxy安裝配置日誌輸出問題

table block linux操作系統 獲得 寫入 pidfile 服務器集群 dmi 指定 簡介:軟件負載均衡一般通過兩種方式來實現:基於操作系統的軟負載實現和基於第三方應用的軟負載實現。LVS就是基於Linux操作系統實現的一種軟負載,HAProxy就是開源的並且基

laravel5日誌設定篇(3/3) – 精確到微秒日誌輸出位置記錄

想要實現的功能 記錄日誌輸出時的時間精度到微秒 記錄日誌輸出時程式執行位置(方法/行數) 記錄程序ID 例如: app.log [2017-10-22 10:13:57.833504] prod

Maven專案 Log4j 配置日誌輸出到相對路徑

1.Maven pom.xml 配置log4j依賴 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId>

kubernetes實戰篇之nexus oss伺服器部署基於nexus的docker映象倉庫搭建

系列目錄 Nexus oss倉庫管理平臺搭建 Nexus是一款倉庫管理工具,支援Npm,bower,maven,nuget,apt,yum甚至docker,helm等各種倉庫,說的通俗以下,就是私服映象倉庫.並且有免費版,OSS版即為免費版,下載的時候請注意版本的選擇. 下載地址 往下面滾動頁面,可以看到

JAVA中使用log4jslf4j進行日誌輸出的方法

cto 所在 inf 官方 pat etl 註意 actor 結果   JAVA中輸出日誌比較常用的是log4j,這裏講下log4j的配置和使用方法,以及slf4j的使用。 一、下載log4j的架包,並導入項目中,如下: 二、創建log4j.prope

日誌輸出到屏幕文件

bug ger round utf-8 col logger asc bin war #!/usr/bin/env python# -*- coding: utf-8 -*-import logging#創建日誌對像logger=logging.getLogger("TES

egg伺服器Linux部署日誌

如果Linux伺服器node,mysql環境還不會配置,請先看https://blog.csdn.net/bocongbo/article/details/83788950 1、將專案git clone到伺服器目錄,目錄自定 2、進入專案目錄,npm install安裝依賴包 3、n

ELKStack分散式日誌查詢分析伺服器安裝配置(ElasticSearch、Logstash、Kibana、Redis)

ELK對於沒有接觸的來說,並沒有一般的服務那麼容易安裝和使用,不過也沒有那麼難,elk一般作為日誌分析套裝工具使用。logs由logstash輸 入,logstash通過配置檔案對日誌做過濾、匹配,就是用來分析日誌的,輸出到elasticsearch,所以他的配置需要和日誌相匹配。 elas

伺服器端SVN部署基本使用

在多人協同開發中,有一個必不可少的工具就是SVN/git,它能幫助我們更好地管理專案程式碼。現在就來學習一下怎麼在服務端部署SVN。 系統:centos7.2 安裝SVN服務端 yum -y install subversion

jfinal部署伺服器用jetty啟動輸出中文亂碼

在部署jfinal專案是使用jetty啟動專案,但是在程式執行時報錯,輸出資訊是亂碼,調查發現是jetty編碼格式預設utf-8,但是專案中的方法並沒有使用,輸出資訊:input1":"瀛欏緩鍥?",然後找到你最開始的編碼方法,比如我的是每次都要呼叫一個簽名方法,就在這個簽名方法裡面 String

利用Syslog Watcher在windows下部署syslog日誌伺服器

1.概述 syslog協議是各種網路裝置、伺服器支援的網路日誌記錄標準。Syslog訊息提供有關網路事件和錯誤的資訊。系統管理員使用Syslog進行網路管理和安全稽核。 通過專用的syslog伺服器和syslog協議將來自整個網路的事件記錄整合到一箇中央儲存庫中,對於網路安全具有重大意義,syslog日誌伺服

阿里雲ECS伺服器部署Node.js專案詳細教程多網站部署

本文詳細介紹如何部署NodeJS專案到阿里雲ECS上,以及本人在部署過程中所遇到的問題、坑點和解決辦法,可以說是全網最全最詳細的教程了。同時講解了如何申請阿里雲免費SSL證書,以及一臺ECS伺服器配置多網站的Nginx配置方法等。 作者:Kovli 1. 雲伺

Centos-7遊戲伺服器環境部署(上)

建立新使用者(一般情況下最好不要使用root使用者登入) #useradd 使用者名稱 #password 密碼 獲取root許可權 修改 $ /etc/sudoers 檔案,找到下面一行,在root下面新增一行新的使用者 設定網路配置 $cd /etc/sys

家庭監控系統實現(三) -- 樹莓派nginx部署rtmp伺服器搭建

本篇主要記錄一下本地實時視訊流的推送。 關於這部分功能,最初的想法是在樹莓派端建立UDP伺服器,將由攝像頭採集到的視訊流廣播出去,但是Android端沒有找到播放UDP裸流的方式。VLC播放器可以設定UDP伺服器地址進行播放,但是效果如何沒有驗證,具體效果如何有時間可以

博通 Broadcom EDI 專案實施日誌(一)——SSH遠端登入CentOS伺服器部署EDI環境

前段時間開始了與博通公司(Broadcom Corporation) 的 EDI對接專案。幫助客戶與博通建立EDI連線。今天開始準備在這裡記錄下實施過程中的一些小心得。 開始之前我們先來科普一下EDI吧。 什麼是EDI呢? 即 Electronic Data Interchang