unity进阶学习笔记:photonServer测试

photonServer是由photon发布的一个网络框架,其封装了UDP和TCP通信机制让用户可以直接调用API实现网络游戏通信

1 photonServer下载安装
在这里插入图片描述

进入Photon官网的SDK选项,选择下载Server。目前Server版本已经更新到v5,这里我为了和教程保持一致下载的是老版本v4.下载完后按照安装指引安装即可

PhotonServer免费版初始只支持20人联机,创建photon账号后可以免费获取将服务器扩容到100人的key。如果要更大容量就需要使用付费版

2 Visual Studio安装
我之前一直使用VS Code,和Visual Studio还有一定区别。这里我们下载Visual Studio Community版本
在这里插入图片描述
下载时,如从官网下载过慢,可以将下载链接换为国内源(网上教程很多)

安装Visual Studio时,由于我们做的是unity游戏开发,在安装选项中添加.NET桌面开发和Unity游戏开发。

2 photonserver工作空间

所有可执行的服务器程序都位于Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy文件夹下。如果我们要创建一个新的服务器程序,在该文件夹下面创建新文件夹,并在新建文件夹下面创建文件夹bin。所有的服务器dll文件要放在bin下面

这里我们新建文件夹PSTest,并在下面新建文件夹bin

在编写服务器程序之前,我们要导入photonserver的框架。在解决方法资源管理器中,我们右键依赖性添加项目引用,选择Photon-OnPremise-Server-SDK_v4-0-29-11263\lib 文件夹下面的以下dll文件
在这里插入图片描述

我们进入Visual Studio,新建项目类型类库。创建类PSTest

扫描二维码关注公众号,回复: 15716941 查看本文章
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;

namespace PSTest
{
    
    
    public class PSTest : ApplicationBase
    {
    
    

        // when client connects
        protected override PeerBase CreatePeer(InitRequest initRequest)
        {
    
    
            PSPeer peer = new PSPeer(initRequest);
            return peer;
        }

        protected override void Setup()
        {
    
    
            
        }

        protected override void TearDown()
        {
    
    
            
        }
    }
}

PSTest为服务器入口类,用于启动服务器,继承自ApplicationBase。该类必须实现方法CreatePeer,Setup,TearDown,分别在客户端连接上服务器,服务器启动,服务器关闭时自动进行调用。其中CreatePeer方法要返回一个PeerBase对象,我们后面要单独创建。

在photonServer中的Peer即为对socket的封装。

PSPeer类

using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSTest
{
    
    
    public class PSPeer : ClientPeer
    {
    
    
        public PSPeer(InitRequest initRequest) : base(initRequest)
        {
    
    
        }

        protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
        {
    
    
        }

        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
    
    
        }
    }
}

PSPeer继承自ClientPeer类,需要实现构造方法,OnDisconnect,OnOperationRequest。我们这里仅为测试,先不在方法类写任何内容。

程序编写完成后,我们右键项目->属性 进入输出选项,将输出的基路径设为Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\PSTest\bin,然后生成项目
在这里插入图片描述
生成后的bin文件
在这里插入图片描述

3 修改配置文件

生成项目后在photonServer中依然无法正常启动PSTest,我们还需要对配置文件进行修改。进入Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64文件夹下面,打开文件PhotonServer.config

这里我们直接复制示例的节点MMoDemo并在其基础上修改文件

	<PSTest
		MaxMessageSize="512000"
		MaxQueuedDataPerPeer="512000"
		PerPeerMaxReliableDataInTransit="51200"
		PerPeerTransmitRateLimitKBSec="256"
		PerPeerTransmitRatePeriodMilliseconds="200"
		MinimumTimeout="5000"
		MaximumTimeout="30000"
		DisplayName="PSTest"
		>

		<!-- 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="PSTest">
			</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="PSTest"
				>
			</TCPListener>
		</TCPListeners>

		<!-- 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="PSTest">

			<!-- MMO Demo Application -->
			<Application
				Name="PSTest"
				BaseDirectory="PSTest"
				Assembly="PSTest"
				Type="PSTest.PSTest"
				ForceAutoRestart="true"
				WatchFiles="dll;config"
				ExcludeFiles="log4net.config">
			</Application>

		</Applications>
	</PSTest>
	

修改方式如下:

1 对于所有命名为MMoDemo的地方,全部改名PSTest。注意在Application节点下Type属性为 命名空间+文件名 因此要输入PSTest.PSTest (这里命名空间名字也为PSTest)

2

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

这一段暂时用不上,可以直接删掉

最后,启动photonserver服务器即可运行PSTest。如果服务器启动后很快自动关闭,多半是程序哪里有问题,可以根据日志信息排查。这里我一开始一直启动后几秒钟就关闭,最后发现是配置文件有拼写错误

猜你喜欢

转载自blog.csdn.net/Raine_Yang/article/details/130766929