[Unity] Native network framework

      The project The Kingdom of Sound, which I did a while ago, used Unity's native network framework RPC, which is not as powerful as Photon, but it is very easy to understand.

1. Basic principles

    The server program initializes a server-type network. After initialization, all clients establish transmission initialization with the server through ip and port. When data transmission is really needed, it is based on a mechanism called rpc-remote host Call, such as the client wants to execute the business logic, first declare the prototype of the function on the client, but there is no specific implementation, he passes the parameters to the server, finds the specific implementation of the function in the server, and executes the specific logic , the server returns the result to all clients to complete the network synchronization interaction.

2. Initialize the server

if(Network.peerType==NetworkPeerType.Disconnected)
		{
			var error=Network.InitializeServer (12,Port,false);
			switch (error)
			{
			case NetworkConnectionError.NoError:
				console.text = Network.player.ipAddress;
				break;
			default:
				break;
			}
		}
Port is an integer number, and the approximate range is within tens of thousands. It is recommended to use a later number, and the port may be less likely to be occupied by other programs.

3. The client and server establish a connection

public void connect_server(string ip)
	{
		if (Network.peerType == NetworkPeerType.Disconnected)
		{
			var error = Network.Connect (ip, port);
			Debug.Log (error.ToString());
			switch (error)
			{
			case NetworkConnectionError.NoError:
				IP_PLAYER = Random.Range (int.MinValue,int.MaxValue);
				break;
			default:
				break;
			}
		}
	}
The ip here is the ip of the server. You can check the ip of your own server host in advance and put it here.
An identifier of IP_PLAYER is also generated here. This is because there are few players in my project, so I generated a random identifier. This is a very low probability of collision, so I use such an int instead of a The ip of the string as the identifier.

4. Take registration as an example to demonstrate the usage of RPC

The client declares the function prototype of register:

[RPC]
	void Register(string account,string password,int info){}
The server has a concrete implementation:

[RPC]
	void  Register(string account,string password,int Virtual_IP,NetworkMessageInfo info)
	{
		if (!dic.ContainsKey (account))
		{
			XmlElement xe = xml_document.CreateElement ("USERMESSAGE");
			xe.InnerText = account;
			xe.SetAttribute ("password",password);
			xml_document.FirstChild.AppendChild (xe);
			xml_document.Save (Path);
			dic.Add (account, password);
			GetComponent<NetworkView> ().RPC ("RegisterSucceed", RPCMode.Others, Virtual_IP);
		}
		else
		{
			GetComponent<NetworkView> ().RPC ("RegisterFailed", RPCMode.Others, Virtual_IP);
		}
	}
transfer:

[RPC]
	public void RegisterUI()
	{
		if (Network.peerType == NetworkPeerType.Client)
			network_manager.GetComponent<NetworkView> ().RPC ("Register", RPCMode.Server, UserAccount.text,UserPassWord.text,IP_PLAYER);
		else
			Debug.LogError ("DisConnect");
	}




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325803059&siteId=291194637