Network.TestConnection 测试连接

static function TestConnection (forceTest : bool = false) : ConnectionTesterStatus

Description描述

Test this machines network connection.

测试这台机器的网络连接。

Two types of tests are performed depending on if the machine has a public IP address present or if it only has a private address (or addresses).

测试执行两种测试,这取决机器是公网IP还是一个私有IP。

The public IP address test is primarily for when running a server as no tests are needed for clients with public addresses. In order for the public IP test to succeed a server instance must be started. A test server will try to connect to the IP and port of the local server and thus it is shown in the server is connectable. If not then a firewall is most likely blocking the server port. A server instance needs to be running so that the test server has something to connect to.

公网IP测试主要用于服务器,不需要测试具有公网IP的客户端。为了公网IP测试成功,必须开启一个服务器实例。一个测试服务器将尝试连接到本地服务器的IP地址和端口,因此它被显示在服务器中是可连接状态。如果不是,那么防火墙是最有可能阻断服务端口的。服务器实例需要运行以便测试服务器已经连接。

The other test is for checking NAT punchthrough capabilities. This is a valid test for both servers and clients and can be performed without any prior setup. If the NAT test fails for a server then it is a bad idea to proceed without setting up port forwarding as no client outside the local LAN network will be able to connect. If the test fails for a client then it cannot connect to servers with NAT addresses, those kinds of servers should then not be be presented to the user as possible game hosts.

另一个测试检测NAT穿透能力。服务器和客户端都可以进行,无需任何事先设定。如果用于服务器NAT测试失败,那么不设置端口转发是一个坏主意。本地LAN网络之外的客户端将不能连接。如果测试失败,客户端就不能使用NAT穿透连接到服务器,这些服务器将不会提供给用户作为主机。

This function is asynchronous and might not return a valid result right away because the tests needs some time to complete (1-2 seconds). After test completion the test result is only returned when the function is called again, a full network test is not redone. That way it is safe to poll the function frequently. If another test is desired, like if the network connection has been altered, then the forceTest parameter should be passed as true.

这个函数是异步的,并可能不会返回有效的结果。因为这个测试需要一些时间来完成(1-2秒)。测试完成之后,测试结果只在函数被再次调用时返回。这样,频繁访问该函数是安全的。如果需要其他的测试,如网络连接已更改,那么forcTest参数应该为true。

The function returns a ConnectionTesterStatus enum.

该函数返回一个ConnectionTesterStatus枚举。

var testStatus = "Testing network connection capabilities.";
var testMessage = "Test in progress";
var shouldEnableNatMessage : String = "";
var doneTesting = false;
var probingPublicIP = false;
var serverPort = 9999;
var connectionTestResult = ConnectionTesterStatus.Undetermined;

// Indicates if the useNat parameter be enabled when starting a server
//表示在服务器启动时,useNat参数是否启用
var useNat = false;

function OnGUI() {
	GUILayout.Label("Current Status: " + testStatus);
	GUILayout.Label("Test result : " + testMessage);
	GUILayout.Label(shouldEnableNatMessage);
	if (!doneTesting)
		TestConnection();
}

function TestConnection() {
	// Start/Poll the connection test, report the results in a label and
	// react to the results accordingly
	//开始/轮询连接测试,在标签上显示结果并按照结果做出相应的反应
	connectionTestResult = Network.TestConnection();
	switch (connectionTestResult) {
		case ConnectionTesterStatus.Error:
			testMessage = "Problem determining NAT capabilities";
			doneTesting = true;
			break;

		case ConnectionTesterStatus.Undetermined:
			testMessage = "Undetermined NAT capabilities";
			doneTesting = false;
			break;

		case ConnectionTesterStatus.PublicIPIsConnectable:
			testMessage = "Directly connectable public IP address.";
			useNat = false;
			doneTesting = true;
			break;

		// This case is a bit special as we now need to check if we can
		// circumvent the blocking by using NAT punchthrough
		//这是一个比较特殊的情况,现在需要检查,如果我们可以使用NAT穿透规避阻塞
		case ConnectionTesterStatus.PublicIPPortBlocked:
			testMessage = "Non-connectble public IP address (port " +
			serverPort +" blocked), running a server is impossible.";
			useNat = false;
		// If no NAT punchthrough test has been performed on this public
		// IP, force a test
		//在这个公网IP,如果没有NAT穿透测试被执行,强制测试
		if (!probingPublicIP) {
			connectionTestResult = Network.TestConnectionNAT();
			probingPublicIP = true;
			testStatus = "Testing if blocked public IP can be circumvented";
			timer = Time.time + 10;
		}
		// NAT punchthrough test was performed but we still get blocked
		//NAT穿透测试已经执行,但是我们仍然被阻塞
		else if (Time.time > timer) {
			probingPublicIP = false; // reset 重置
			useNat = true;
			doneTesting = true;
		}
		break;
		case ConnectionTesterStatus.PublicIPNoServerStarted:
			testMessage = "Public IP address but server not initialized, "+
				"it must be started to check server accessibility. Restart "+
				"connection test when ready.";
			break;

		case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
			testMessage = "Limited NAT punchthrough capabilities. Cannot "+
				"connect to all types of NAT servers.";
				useNat = true;
			doneTesting = true;
			break;

		case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
			testMessage = "Limited NAT punchthrough capabilities. Cannot "+
				"connect to all types of NAT servers. Running a server "+
				"is ill advised as not everyone can connect.";
			useNat = true;
			doneTesting = true;
			break;

		case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
			testMessage = "Limited NAT punchthrough capabilities. Cannot "+
				"connect to all types of NAT servers. Running a server "+
				"is ill advised as not everyone can connect.";
			useNat = true;
			doneTesting = true;
			break;

		case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
		case ConnectionTesterStatus.NATpunchthroughFullCone:
			testMessage = "NAT punchthrough capable. Can connect to all "+
				"servers and receive connections from all clients. Enabling "+
				"NAT punchthrough functionality.";
			useNat = true;
			doneTesting = true;
			break;

		default:
			testMessage = "Error in test routine, got " + connectionTestResult;
	}
	if (doneTesting) {
		if (useNat)
			shouldEnableNatMessage = "When starting a server the NAT "+
				"punchthrough feature should be enabled (useNat parameter)";
		else
			shouldEnableNatMessage = "NAT punchthrough not needed";
				testStatus = "Done testing";
	}
}
最后修改:2011年4月4日 Monday 17:49

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。