PhotonServer 基础 服务器架设

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_28474981/article/details/90262551

1.下载

1.进入网站

https://www.photonengine.com/zh-CN/sdks#realtime

2.选中下载标签为Server
在这里插入图片描述
3.点击Icon上的More,然后点击下载
在这里插入图片描述

2.安装

双击、跟着安装就可以了

3.HelloWorld

3.1 建立一个项目,并添加所需引用

路径:Photon-OnPremise-Server-SDK_v4-0-29-11263\lib
在这里插入图片描述

3.2 配置log输出配置文件

将Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\Loadbalancing\GameServer\bin下的文件log4net.config加入项目,并配置始终复制:
在这里插入图片描述

3.3 具体代码

MyServer :

using ExitGames.Logging;
using ExitGames.Logging.Log4Net;
using log4net;
using log4net.Config;
using Photon.SocketServer;
using System.IO;


public class MyServer : ApplicationBase
{
    private static ILogger log = ExitGames.Logging.LogManager.GetCurrentClassLogger();
    //日志输出
    public static void Log(string str)
    {
        log.Info(str.ToString());
    }

    protected virtual void InitLogging()
    {
        ExitGames.Logging.LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
        GlobalContext.Properties["Photon:ApplicationLogPath"] =  Path.Combine(this.ApplicationRootPath, "log");
        GlobalContext.Properties["LogFileName"] = "My" + this.ApplicationName;
        XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(this.BinaryPath, "log4net.config")));
    }


    //创建连接
    protected override PeerBase CreatePeer(InitRequest initRequest)
    {
        Log("CreatePeer.+++++++++++++++++++++++++++++++++++++");
        return new MyClientLink(initRequest);
    }

    //服务器启动时调用
    protected override void Setup()
    {
        InitLogging();
        Log("Setup ok.+++++++++++++++++++++++++++++++++++++");
    }


    //服务器停止时调用
    protected override void TearDown()
    {
        Log("TearDown ok.");
    }
}

MyClientLink :

using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;

public class MyClientLink : ClientPeer
{

    public MyClientLink(InitRequest initRequest) : base(initRequest)
    {
        MyServer.Log("客户端上线");

    }
    //客户端断开连接
    protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
    {

        MyServer.Log("客户端下线");
    }
    //客户端发起请求
    protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
    {
        MyServer.Log("客户端发送请求");
    }
}

3.4 配置生成路径

(1)在路径Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy下创建目标文件夹
在这里插入图片描述
(2)然后在里面创建文件夹bin
在这里插入图片描述
(3)设置解决方案生成路径
在这里插入图片描述
(4)生成解决方案就可以了

3.5 配置服务器属性

在路径Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64下找到文件PhotonServer.config并配置
(1)首先先复制原来有的,例如MMoDemo
(2)开始修改

	<TestServer
		MaxMessageSize="512000"
		MaxQueuedDataPerPeer="512000"
		PerPeerMaxReliableDataInTransit="51200"
		PerPeerTransmitRateLimitKBSec="256"
		PerPeerTransmitRatePeriodMilliseconds="200"
		MinimumTimeout="5000"
		MaximumTimeout="30000"
		DisplayName="TestServer"<!-- PhotonControl展示出来的名称-->
		>
		<UDPListeners>
			<UDPListener
				IPAddress="0.0.0.0"
				Port="5055"<!-- 使用UDP链接时的端口-->
				OverrideApplication="TestServer"><!-- 使用UDP链接时的连接名-->
			</UDPListener>
		</UDPListeners>
    
		<TCPListeners>
			<TCPListener
				IPAddress="0.0.0.0"
				Port="4530"<!-- 使用TCP链接时的端口-->
				PolicyFile="Policy\assets\socket-policy.xml"
				InactivityTimeout="10000"
				OverrideApplication="TestServer"<!-- 使用UDP链接时的连接名-->				
				>
			</TCPListener>
		</TCPListeners>
		
		<WebSocketListeners>
			<WebSocketListener
				IPAddress="0.0.0.0"
				Port="9090"<!-- 使用WebSocket链接时的端口-->
				DisableNagle="true"
				InactivityTimeout="10000"
				OverrideApplication="TestServer"><!-- 使用WebSocket链接时的连接名-->	
			</WebSocketListener>
		</WebSocketListeners>
		<Applications Default="TestServer">
		
			<!-- TestServer Application -->
			<Application
				Name="Test3Server"<!-- 项目名称-->
				BaseDirectory="TestServer"<!-- dll放的文件夹,即deploy下的文件名 -->
				Assembly="TestServer"<!-- 程序集名称-->
				Type="TestServer.MainClass"<!-- 程序入口-->
				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>

配置完重启就能看见,Start as application 启动
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_28474981/article/details/90262551