To achieve simple master server connection with unity3d, refer to self-e ------------------------------------------------------------------------------------- I. UI and main logic
// It is worth noting that
// 1. Network. useNat is no longer directly replaced by a variable.
// 2. GUILayout. BeginScrollView () has better overload functions to use.
// 3. Network. InitializeServer (players, port); expired
// 4. Some functions in the test are outdated. You can refer to the Holy Book to modify them.
// 5. It seems that the master server cannot be connected.
Main Logical Functions
1. Awake ()
L initialize the Server ip address and port
L initialization window
L initialize the game mode [Optional]
2. OnGUI ()
L check whether there is a connection to the server or become a server [at the beginning, at least the connection is in progress]
N loading scenarios
N if there are no two possibilities: an error has occurred and the connection has not started [Classification]
U if an error occurs, an error is prompted in the pop-up window.
U At this time, it must have not been connected and can be used for fast game or player customization.
L if a player clicks a quick game, the game is not started yet]
N call the quick game Function
L if a player clicks a custom game
N you can create multiple logon methods for players. [three types are designed at this time:]
U add according to the room list
U according to the server ip Address [can be replaced with the name of the player who creates the server, etc.]
U creates a game by yourself
N players can choose to return to the main menu
L if the player hasn't clicked yet, [design and select UI]
N enter the player name
N create a quick game or custom game
Implementation Functions
1) playNowFunction () fast game
L you can choose to exit the game during loading
L if you try to connect to the host multiple times, you cannot create a host as a server [Create a room]
L call the script for handling the quick game to start the game
N if the quick game returns a failure, the player creates a host by himself. [Create a room]
N otherwise, the system is trying a quick game to display the processing progress [for example, countdown]
The following three are window callback functions [Create a UI suitable for interaction and then join the game]
2) hostGUI () create a game by yourself
L The creation window requires the gamer to enter the necessary information for creating the room [number of players using the port]
L confirm the information and create a room
3) directConnectGUIWindow () is added to the game through ip Address
L a message is displayed when the connection is in progress to notify the player that the connection is in progress.
L otherwise, no information is entered'
N players enter the target room information [whether ip port is useNat]
N try to connect to the target room
4) listGUI () room list
L create a scroll bar [read and display host information in the host list]
L players can actively refresh the room list [Optional]
L if the host is not explicitly stated to the player in the list
II. Implementation of multiplayer games
Main logical functions [mainly deal with connection problems]
1. Awake ()
L host list new space
L connection test
L determine and describe the local ip type [public or private]
L modify master server information [Optional]
2. You can perform collaborative operations in Start () start.
L list of request hosts
3. Update ()
L test Nat
Network Connection Functions
1) The OnFailedToConnectToMasterServer (info: NetworkConnectionError) wood has been connected to the master server
L in this example, wood provides a solution
2) OnFailedToConnect (info: NetworkConnectionError) failed to connect to the server
L call the connection failure Handler
3) Connect (ip: String, port: int, usenat: boolean) connection function [not necessary]
It seems to be called only when the connection fails to be processed and retried. [The encapsulation value is unclear]
L connect to the server based on parameters
L identify the current connection
4) The OnConnectedToServer () Client is called after it is successfully connected to the server
L mark the use of the network function pause because when the client is still loading, we should prevent some unrecognized network information from being sent from the game board.
L save the server's ip address and port to preference [for future use]
Call functions that implement specific functions
1) FailedConnRetry (info: NetworkConnectionError) handle various connection failures
L when the login server password is incorrect, the user will not try again
L when I identify that the current client is trying to connect to the current server [if the connect system has called the server based on the host, it is assumed that the client can still connect to the server again]
L if you can try again and useNat = true, and the host currently being tried requires NAT penetration, we do not need NAT penetration to connect to the current server again. Here we should forget to store ip records.
L otherwise, the current server will no longer be able to connect to the server that has reset and tried useNat + 1. Identify that the server is not currently connected
L when I identify that the current client is not trying to connect to the current server [that is, I didn't start the game using a quick game] // I don't understand
L in this example, the connection message is marked as "Failed to connect!" because no operation is performed on non-fast game connections !"
L [it seems that this step cannot be taken ...] When the server requires useNat and the client also uses useNat connection, you do not need useNat to try again [cannot penetrate or possibly connect to public ip addresses without private ip addresses]
L otherwise, the connection will fail.
Different failure prompts, port resetting, and useNat resetting are output based on the connection failure cause. The actual status of the current client is displayed]
2.) TestConnection () test Nat provides feedback on the result of the polling test connection being connected. 1. The test result is ConnectionTesterStatus. Error.
It indicates that an unknown problem has occurred. It may be that there is no available network connection ID to complete the Nat test.
2. ConnectionTesterStatus. Undetermined:
Note: The logo has not been tested. The Nat test has not been completed.
3. ConnectionTesterStatus. PrivateIPNoNATPunchthrough private ip address. Nat penetration is not allowed.
In this case, the client that uses Nat penetration can only use the LAN game logo to filter the Nat host and complete the test. useNat = true [Do you not understand it here ??? But this problem does not occur. I don't know the specific situation. How can I meet a friend to help me]
4. ConnectionTesterStatus. PrivateIPHasNATPunchThrough private ip address and Nat penetration
If the public ip address is being detected, the public ip address cannot be connected to [blocked by port].
[If it is a server, NAT penetration is enabled when it is enabled; if it is a client, it is enabled when the host requests]
Whether the public ip address is detected or not, useNat = true is used to complete the NAT test.
5. ConnectionTesterStatus. PublicIPIsConnectable
You can directly connect to the public IP address, Mark useNat = false, and identify to complete the NAT test.
6. ConnectionTesterStatus. PublicIPNoServerStarted public ip is blocked
A) if public ip is being detected, you can bypass the firewall to forcibly test the NAT connection, mark the public ip being detected, and design it for a period of time.
B) if it remains blocked after a period of time, reset the public ip Address [false], useNat = false, and mark the test to complete NAT testing.
7. other errors
Finally, the current useNat is marked as a formal Nat status.
If the test is complete, the test result is output to facilitate the check of errors.
External call interface
L StartHost (players: int, port: int) create a server to initialize and encapsulate the server
L if the number of players is less than 1, change it to at least 1 [ensure that at least one player is on the server]
L initialize the server
L PlayNow (timeStarted: float) a quick game attempts to connect all the games by accessing the game list
L traverse the host list to find the host that does not set the password, filters the penetration, and the host does not penetrate or uses the penetration and does not filter [do not penetrate if it cannot penetrate] [one category is not penetrating] to find mark
L if the number of players connecting to the server does not exceed the limit
L. I am trying to connect to the quick game. [do I feel that the connection is too long to bring people offline ???]
L if the connection is too long, it is interrupted by the timer.
Connection retry
L if the number of the host that is currently trying to connect to the host is smaller than the number of the host currently polling
L mark the host number that is trying to connect to the fast game set to the current number mark the last connected host useNat and the client's useNat attribute consistent with the server connection this server mark the last connected fast connection the current time.
L Bubble sorting of the host list [The purpose is to increase the number of rooms with a small number of people and reduce the waiting probability for the game to join quickly]
· FetchHostList (manual: boolean) // Request Host limit [update host list] automatic request at every 2 minutes takes the initiative to request at every 5 seconds
Iii. Handling connection, disconnection, and status after the game is loaded
Main logic of the class
1. Awake ()
Allow Network Information Transmission
If it is a server, you should register the game on the master server.
2. OnGUI () Round Robin processes the current connection status
Network. peerType = NetworkPeerType. Disconnected is Disconnected.
Network. peerType = NetworkPeerType. Connecting is trying to connect
Network. peerType = NetworkPeerType. Client When the frontend is a Client
Network. peerType = NetworkPeerType. Server when the frontend is a Server
You can choose to disconnect
Connection and disconnection
Client:
1. OnDisconnectedFromServer (info: NetworkDisconnection) is disconnected from the server
Server:
1. The OnPlayerConnected (player: NetworkPlayer) server detects that the client is connected to the server.
2. The OnPlayerDisconnected (player: NetworkPlayer) server detects that the client is disconnected from the server.