Network.useProxy 使用代理

static var useProxy : bool

Description描述

Indicate if proxy support is needed, in which case traffic is relayed through the proxy server.

表示是否需要代理支持,在这种情况下,流量通过代理服务器传递。

The proxy server is a solution to connectivity problems with servers as well as clients. When a machine has an non NAT punchthrough capable router his connectivity options are limited. A game cannot be hosted as nobody external can connect to him (only clients on the local network can). By using the proxy server the machine can be fully connectable but with the extra cost of having all traffic relayed through another server. A non NAT punchthrough capable client can connect to any server through the proxy server, as long as the proxy server is set up properly.

代理服务器是一种与服务器和客户端连接问题的解决方案。当机器有一个非NAT穿透能力的路由器,其连接的选择非常优先。一个游戏不能没有外部连接(只可以客户端在本地网络)。通过使用代理服务器,该机器可以具有完全的连接性,但是额外的成本是所有的流量都会通过另一台服务器传递。一个没有NAT穿透能力的客户端通过代理能够连接到任何服务器,只要在代理服务器设置正确。

Unity Technologies does not provide a proxy server for public use, so to use this feature you will need to set up your own proxy server. Of course it is advisable to set up a proxy server with a public IP address and a lot of available bandwidth.

Unity并不提供

代理服务器为公众使用。所以你需要自己建立代理服务器。当然,用公有IP地址设置代理服务器并保证有大量可用带宽是明智的。

When running as a client, just enabling Network.useProxy is all you have to do. Connect to the server as usual with Network.Connect(). All traffic will be relayed through the proxy server. The servers external IP and internal IP still work as usual. So clients can connect to him directly without the proxy in case they are located on the same network.

当作为客户端运行时,只要启用Network.useProxy就可以。像通常使用 Network.Connect()连接到服务器。所有浏览通过代理服务器传递。服务器的外部IP和内部IP还像往常一样工作。这样如果它们位于同一网络中,客户端可以直接连接而不经过代理。

When running as a server OnServerInitialized(NetworkPlayer) returns a NetworkPlayer structure which indicates what the game servers relayed IP/port is, i.e. what port the proxy server allocated to the game server. This is the IP/port others can use to connect to. When connecting to the server, the clients don't treat the server any differently than other servers. Technically speaking, they don't need to know at all that the game server is getting help from a proxy server.

作为一个服务器运行时,OnServerInitialized(NetworkPlayer)返回一个NetworkPlayer结构,表示游戏服务器中继的IP/端口,即,代理服务器分配给游戏服务器的端口是什么。这个是其他客户端可以连接的IP/端口。当连接到服务器时,客户端不会将这个服务器与其他服务器区别对待。严格的说,它们不需要知道这个服务器得到代理服务器的帮助。

When using the master server you can no longer only rely on the IP/port he registers for servers when using proxy support. The proxy server IP address and port which the server is using can be placed in the comment field of the data sent to the master server. A client which received host information from the master server can peek into the comment field and find out if he can use an alternative IP/port for that host.

当使用主服务器时,使用代理支持,你不能只依赖于为服务器IP/端口注册。服务器使用的代理服务器的IP地址和端口,可以放置在数据域的注释中来发送给主服务器。从主服务器接收主机信息的客户端可以去除注释域并查看它是否能够为那个主机使用另一个可选的IP端口。

IMPORTANT: You should never enable proxy support for both the server and the client connecting to him. Unexpected things are bound to happen.

重要事项:你不应该同时为连接到它的服务器和客户端启用代理支持,会发生意想不到的事情。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public bool imaserver;
	public string serverIP;
	public int serverPort;
	public bool serverUsesNAT;
	void Awake() {
		Network.proxyIP = "1.1.1.1";
		Network.proxyPort = 1111;
		if (imaserver)
			StartServerWithProxy();
		else
			ConnectToServerWithProxy();
	}
	void StartServerWithProxy() {
		Network.useProxy = true;
		Network.InitializeServer(2, 25000, false);
	}
	void OnServerInitialized(NetworkPlayer player) {
		if (Network.useProxy)
			Debug.Log("Successfully started server with proxy support. We are connectable through " + player.ipAddress + ":" + player.port);

	}
	void OnFailedToConnect(NetworkConnectionError msg) {
		if (Network.useProxy && imaserver)
			Debug.LogError("Failed to connect to proxy server: " + msg);

	}
	void ConnectToServerWithProxy() {
		Network.useProxy = true;
		Network.Connect(serverIP, serverPort);
	}
	void OnConnectedToServer() {
		Debug.Log("Connected successfully to server");
	}
}
var imaserver: boolean;
var serverIP: String;
var serverPort: int;
var serverUsesNAT: boolean;

function Awake() {
	// Set custom proxy server address
	//设置自定义代理服务器地址
	Network.proxyIP = "1.1.1.1";
	Network.proxyPort = 1111;

	if (imaserver)
		StartServerWithProxy();
	else
		ConnectToServerWithProxy();
}

function StartServerWithProxy() {
	Network.useProxy = true;
	Network.InitializeServer(2,25000, false);
}

function OnServerInitialized(player: NetworkPlayer) {
	if (Network.useProxy)
		Debug.Log ("Successfully started server with proxy support. We are connectable through "
			+ player.ipAddress + ":" + player.port);
}

function OnFailedToConnect(msg: NetworkConnectionError) {
	if (Network.useProxy && imaserver) {
		Debug.LogError("Failed to connect to proxy server: " + msg);
	}
}

function ConnectToServerWithProxy() {
	Network.useProxy = true;
	Network.Connect(serverIP, serverPort);
}

function OnConnectedToServer() {
	Debug.Log("Connected successfully to server");
}
Page last updated: 2011-4-2