Unity advanced study notes: photonServer test

photonServer is a network framework released by photon, which encapsulates UDP and TCP communication mechanisms so that users can directly call APIs to realize online game communication

1 photonServer download and install
insert image description here

Go to the SDK option on the Photon official website and choose to download Server. The current Server version has been updated to v5. Here I downloaded the old version v4 in order to be consistent with the tutorial. After downloading, follow the installation instructions to install it.

The free version of PhotonServer initially only supports 20 people online. After creating a photon account, you can get the key to expand the server to 100 people for free. If you want more capacity, you need to use the paid version

2 Visual Studio installation
I have been using VS Code before, and there are some differences from Visual Studio. Here we download the Visual Studio Community version.
insert image description here
When downloading, if the download from the official website is too slow, you can change the download link to a domestic source (there are many online tutorials)

When installing Visual Studio, since we are doing unity game development, add .NET desktop development and Unity game development to the installation options.

2 photonserver workspace

All executable server programs are located in the Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy folder. If we want to create a new server program, create a new folder under this folder, and create a folder bin under the new folder. All server dll files should be placed under bin

Here we create a new folder PSTest, and create a new folder bin below

Before writing the server program, we need to import the framework of photonserver. In the Solution Explorer, we right-click the dependency to add a project reference, and select the following dll files under the Photon-OnPremise-Server-SDK_v4-0-29-11263\lib folder
insert image description here

We enter Visual Studio and create a new project type class library. Create class PSTest

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 is the server entry class, which is used to start the server and inherits from ApplicationBase. This class must implement the methods CreatePeer, Setup, and TearDown, which are automatically called when the client connects to the server, the server starts, and the server shuts down. Among them, the CreatePeer method returns a PeerBase object, which we will create separately later.

The Peer in photonServer is the encapsulation of the socket.

PSPeer class

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 inherits from the ClientPeer class and needs to implement the constructor, OnDisconnect, OnOperationRequest. We are here only for testing, do not write anything in the method class.

After the program is written, we right-click the project -> properties to enter the output option, set the base path of the output to Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\PSTest\bin, and then generate the bin file after the project is
insert image description here
generated
insert image description here

3 Modify the configuration file

After generating the project, PSTest still cannot be started normally in photonServer, we also need to modify the configuration file. Go to the Photon-OnPremise-Server-SDK_v4-0-29-11263\deploy\bin_Win64 folder and open the file PhotonServer.config

Here we directly copy the example node MMoDemo and modify the file based on it

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

The modification method is as follows:

1 Rename all places named MMoDemo to PSTest. Note that the Type attribute under the Application node is namespace + file name, so enter PSTest.PSTest (here the namespace name is also 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>

This paragraph is temporarily unused, you can delete it directly

Finally, start the photonserver server to run PSTest. If the server automatically shuts down soon after startup, there may be a problem with the program, which can be checked according to the log information. Here, I shut down after a few seconds after starting, and finally found that the configuration file had a typo

Guess you like

Origin blog.csdn.net/Raine_Yang/article/details/130766929