Real-time monitoring of the maximum number of online players (Photon)

Because the game is still in the development stage, the free version of Photon (100 players) is used, and there is a limit on the number of people. Then the question is, how can I monitor the online number of players? The following system shares the following Photon experience.

Insert picture description here

1. Monitoring method of the number of online players

Understanding the basic principles of Photon, you can know that Photon informs the connection status change through the IPhotonListener.OnStatusChanged() method. The parameter of this method is the enumerated type StatusCode.
Insert picture description here
This enumeration has the following types:
Insert picture description here

That's right, that's it!
Insert picture description here
When photon detects that the number of users reaches the limit of concurrent users, it will disconnect the server.
Therefore, we can use the following methods to achieve real-time monitoring of the upper limit of the number of online players.
Insert picture description here

 public void OnStatusChanged(StatusCode statusCode)
        {
    
    
            switch (statusCode)
            {
    
    
                case StatusCode.Disconnect:
                    DebugReturn(DebugLevel.INFO, "@@@@@@Disconnect");
                    PlayerController.instance.CancelInvoke();
                    PlayerController.instance.StopAllCoroutines();
                    photonPeer.Connect(ServerAddress, ServerAppName);
                    break;

                case StatusCode.TimeoutDisconnect:
                    DebugReturn(DebugLevel.INFO, "@@@@TimeoutDisconnect");

                    break;
                case StatusCode.DisconnectByServer:
                    DebugReturn(DebugLevel.INFO, "@@@@DisconnectByServer");

                    break;
                case StatusCode.Connect:
                    DebugReturn(DebugLevel.INFO, "@@@@Connected");
                    connected = true;
                    break;
                case StatusCode.Exception:
                    DebugReturn(DebugLevel.INFO, "@@@@Exception");
                    photonPeer.Connect(ServerAddress, ServerAppName);
                    break;
                case StatusCode.ExceptionOnConnect:
                    DebugReturn(DebugLevel.INFO, "@@@@ExceptionOnConnect");

                    break;
                case StatusCode.ExceptionOnReceive:
                    DebugReturn(DebugLevel.INFO, "@@@@ExceptionOnReceive");

                    break;
                case StatusCode.DisconnectByServerUserLimit:
                    Debug.Log("玩家人数超载!");
                    DebugReturn(DebugLevel.INFO, "@@@@DisconnectByServerUserLimit");
                    GameObject.Find("UserOverLoadPanel").SetActive(true);
                    break;

                default:
                    DebugReturn(DebugLevel.ERROR, statusCode.ToString());
                    break;
            }
        }

2. Photon server construction

1 Introduction

Photon is characterized by a long connection. It is a network engine that integrates products and services. It is also the fastest, most flexible and easy to use network engine. It can be used on multiple platforms such as PC, Mac, browser, mobile terminal, and console. Establish scalable MMOG, FPS, and any multiplayer online games and applications.

Photon address: https://www.photonengine.com/en/server

Photon engine charging method
Photon certificate is divided into personal version and enterprise version. The current price is as shown in the table below:
Personal version:
unlimited number of people, the price is 4800 yuan
Enterprise version:
500 people online at the same time 8900 yuan
1000 people online at the same time 12500 yuan
unlimited number of people RMB 19,600
Note: The certificate for personal edition and enterprise edition with 100 people online at the same time is free forever.

Communication mechanism between Photon server and Unity 3D client

  1. Send request to Photon server
  2. Process the response returned by the Photon server
  3. Complete the operation of the game room
  4. Implement custom events

For details, please refer to this blog: Unity 3D Photon Server

2. Configure Photon server

Photon address: https://www.photonengine.com/en/server
1. Install the Photon SDK and
go to the Photon official website to download the v3.1 version of the SDK. After the download is complete, unzip it to get a total of 4 file directories: deploy, doc, lib and src-server. Deploy is the executable file of the photon server under each platform. doc is a help file. Lib is a compiled dll file, used when deploying the server. The src-server contains the game demo source code.

2. Run the PhotonControl.exe file
Run PhotonControl.exe, find the icon of the photon server in the toolbar, right-click and select Default->Start as application (different versions of Photon may be different, and some are Default) to run the Photon server. The photon server icon is blue after success, otherwise it is running abnormally.

3. Setting up the Photon configuration file
We can modify the configuration of the Photon server through the PhotonServer.config configuration file under different platform directories in deploy. In the configuration file, the maximum message size and maximum/minimum timeout period for transmission can be set in the default node. In the UDPListener and TCPListener nodes, you can set the ip address and communication port for monitoring UDP and TCP connections. Some settings of Websocket communication can be made in the Websocket node. The Application node is more important, and the name of each Application is used when the communication interface method is called.

4. Check the port and firewall settings in the Windows system.
Start Default->Run TestClient and run the client test program. If the Windows firewall prompt pops up during the running process, you need to open the protocol set in the Photon configuration file in the Windows firewall port. If there is no prompt, if necessary, you need to manually create inbound rules in the advanced settings of the Windows firewall. Because in this case, it is possible that the firewall is an inbound rule created for the Photon server program and does not restrict the use of ports. And we can manually create inbound rules for specific ports for UDP and TCP communications.

5. Photon abnormal troubleshooting
When we start PhotonControl.exe, click Default->Start as application in the icon, and the server stops after running for a period of time, and the icon turns gray, indicating that the Photon server is operating abnormally. We need to check the port settings of the Photon server first. If the influence of the port and firewall settings is excluded, we can use the log file Photon-Default to check whether an exception occurred during the startup of the Photon server. If the abnormal startup of the Photon server is ruled out, you need to open the log file of each game server program to check which game server program is abnormal.

3. Photon client development

1. Unity 3D connects to the Photon server.
Import Photon3Unity3D.dll to the Assets/Plugins directory of the U3D project. Write the client script file to communicate with the Photon server, inherit the IPhotonPeerListener interface and implement the OnStatusChanged interface method. In the Update method, call the Service method of the PhotonPeer object to communicate with the Photon server. Only by calling the Service method can we connect and communicate with the server normally.
Insert picture description here

Insert picture description here

2. Send a request to the Photon server
First, you need to write a SendMessage function to send a request from the client to the server, and use the OpCustom method of the PhotonPeer object to send the request. At the same time, the OnOperationResponse and OnEvent interface methods need to be implemented to process the results returned by the server.
Insert picture description here

3. Process the response returned by the Photon server.
Implement the OnOperationResponse function;
the type of response returned by the server can be analyzed through OperationCode. ;
Write a function to process the message returned by the Photon server;
Insert picture description here

4. Custom events
Realize OnEvent function;
set game operation enumeration;
transmit custom data parameters;
Insert picture description here

3. Common Problems of Photon Server

1. Does the Photon engine have to be deployed on the windows platform?
The Photon application is mainly based on the .net environment, not limited to specific operating system platforms, but considering that the current support for .net virtual machines under linux is not too complete, it is recommended to use the windows platform as the production environment.

2. Does the Photon engine provide support for the database?
The Photon engine itself does not provide a database operation interface. Developers can choose to use current mainstream relational databases such as Oracle, MS Sql Server, MySql, DB2, and NHibernate's ORM technology or even NoSQL technology.

3. What are the advantages of Photon engine and Unity's built-in network functions?
Photon cannot be simply understood as a network library. In addition to the powerful server-side engine, Photon supports most client platforms and can develop applications on various platforms, including the Unity platform.

4. What model does the underlying network of the Photon engine use?
The network core of the Photon engine is mainly based on the most efficient completion port (IOCP) asynchronous network model under the Windows platform.

5. Can the server of Photon engine use C++, VB or other development languages?
Currently, the API provided by Photon is C# language. We recommend using C# as the application-level development language; if you use C++ or VB, you can do it, but you need to do a lot of extra work yourself.

6. Does the Phonton engine provide an interface based on unmanaged code?
Photon applications are mainly loaded and run by the Photon kernel, and are based on the .NET runtime environment, so unmanaged code interfaces are not provided.

7. Does the Photon engine need to write its own network synchronization algorithm?
For the Photon on the same network connection, the order can be guaranteed without synchronization; however, the synchronization at the business level needs to be implemented by the developer.

8. How does the Photon engine charge in detail?
Photon certificate is divided into personal version and enterprise version. The current price is shown in the following table:
Personal version:
no limit on the number of people, the price is 4800 yuan
Enterprise version:
500 people online at the same time 8900 yuan
1000 people online at 12500 yuan at the same time
no limit on the number of 19,600 yuan
Remarks: The certificate for personal and enterprise editions with 100 people online at the same time is permanently free

9. What is the difference between the Photon personal edition certificate and the enterprise edition certificate?
Developers who use the personal version of the certificate will have two restrictions:
1. The monthly income of the licensee is less than 60,000 yuan
2. The product of the licensee highlights the Photon LOGO

10. The app running PhotonControl will automatically stop within a few seconds. What is the reason?
There are three possible reasons:
1. The certificate verification failed: the certificate has expired or the floating certificate server is not connected.
2. The port is occupied: For example, Kugou occupies port 848 or port 9090 is occupied;
3. An application program If there is a problem, it will also cause the Photon engine to fail to start. Generally, it is necessary to check the construction method and setup method of the application entry class.

11. Why can't I use download tools such as Thunder to obtain the certificate normally?
Due to the security verification performed by the certificate generation server, if you download the certificate file with a download tool such as Thunder, the content of the file is incorrect, which will result in the failure of normal use; therefore, it is recommended to use the save as method to obtain the certificate file.

12. Which client platforms does Photon support?
Currently Photon mainly supports: Unity, iOS, Flash, Windows, .NET, Android, Silverlight, etc.

13. What is the connection between Photon and Unity? Does Photon include Unity or does Unity include Photon?
Photon and Unity are two relatively independent products, focusing on different technical fields. Photon mainly provides support for server-side engine functions and supports various client platforms (including iOS, Android, WinForm, Cocos2D, Flash, SilverLight, Unity, etc.) ) Provide relevant support, with the characteristics of high execution efficiency, high development efficiency, simple development and deployment, and flexible configuration; Unity is a product in the field of client-side graphics engines. Currently Photon and Unity are a strategic partnership. The former focuses on network and server functions, while the latter focuses on client-side graphics.
The Photon client SDK provides support for the Unity platform and other platforms, not a part of Unity. At the same time, Photon can be effectively combined with any mainstream client platform.

14. Can the higher version of Photon be compatible with the lower version in terms of function and usage?
The high version of Photon is compatible with the low version in terms of use, but some functions need to be adjusted slightly.

Guess you like

Origin blog.csdn.net/qq_43505432/article/details/115004335