查看: 1289|回复: 0
打印 上一主题 下一主题

[Unity 组件参考手册]桌面:网络参考指南之主服务器

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2013-2-19 17:05:49 |只看该作者 |倒序浏览
The Master Server is a meeting place for games that are actively seeking clients, and player clients who want to connect to them. Its purpose is also to hide IP address and port details and perform technical tasks around setting up network connections which might otherwise not be possible, like firewall handling and NAT punch-through.主服务器就像一个游戏大厅,会主动扫描想要连接进来的客户端。其目的是为了使隐藏的IP地址和端口也能够建立网络连接,像防火墙和网络穿透。Each individual&nbsp***nning game server provide a Game Type to the Master Server. All games with a matching Game Type are gathered together so that compatible clients can easily view them. When a player connects and queries the Master Server for their matching Game Type, useful information on the server is displayed for the player to see. This helps the player decide which server to connect to. This includes the Game Name, player count and whether or not a password is needed. The two functions used to transmit this data are MasterServer.RegisterHost() for the Server, and MasterServer.RequestHostList() for the player client.每个运行的游戏服务器都需要向Master Server提供一个游戏类型.具有相同类型的游戏被集中在一起以至于客户端可以很容易查看到,当一个玩家连接Master Server查找游戏时。Master Server可以提供该游戏的详细信息,帮助玩家决定连接哪个服务器.这些信息包括游戏名,玩家数量,是否需要密码。服器通过调用MasterServer.RegisterHost()传递相应信息,客户端通过调用MasterServer.RequestHostList()去获取相应信息。When calling RegisterHost(), you need to pass three arguments - gameTypeName (which is the previously mentioned Game Type), gameName and comment - for the host being registered. RequestHostList() takes as an argument the gameTypeName of the hosts you are interested in connecting to. All the registered hosts of that type will then be sent to the requesting client. This is an asynchronous operation and the actual host list can be retrieved with PollHostList() which returns the list when it has arrived.当调用RegisterHost()进行注册时,需要传递三个参数- 游戏类型明(就是上文提到的游戏类型),游戏名和注释。RequestHostList()函数将需要连接的游戏类型名作为参数,所有已注册主机的信息将被发送到客户端。这是一个异步操作,可以通过调用PollHostList()获取所有的主机列表。The NAT punchthrough portion of the Master Server is actually done by a separate entity called the Facilitator. The central Unity Master Server is also&nbsp***nning a Facilitator in addition to being a game lobby so it is OK to think of these functionalities as simply Master Server functionalities in that context.网络穿透部分的功能,是被另外一个从Master Server分割出来的叫Facilitator的实体所完成的。Unity中心服务器上也运行了一个Facilitator,可以把Facilitator看做是Master Server的子功能。The Game Type you set for your game should be unique for your game. This is to prevent clients&nbsp***nning a completely different game from seeing your servers and trying to connect to them. So don't use "Game1" or anything generic like that as the gameTypeName. Be specific and unique. If you have different versions of your game out, you could use the comment field to relay that information. That way the client will know if their version is incompatible with some servers in the host list. The comment field can also be used to relay any information you desire. It is a binary data field and can store objects, you just need to cast them properly yourself.为游戏所设置的游戏类型必须是独一无二的。防止客户端看到和实际连接上的游戏不一样的情况发生。因此,不要使用"Game1"或者其他像gameTypeName这样普遍的名字,名字一定是要特定的独特的,如果游戏有不同的版本,可以用注释来表明这个信息,这样的话客户端可以知道他们的版本是否和主机相匹配。这是一个二进制的可存储对象,你需要自己去正确转换它们。More advanced functionality can be done by using the comment field and customizing the Master Server so that it will use that information itself. For example you could reserve the first 10 bytes of the comment field for a password and then extract the password in the Master Server when it receives the host update. It can compare and reject the host update if appropriate (password check fails). Of course this would require editing of the Master Server source code itself, but this can be downloaded and modified. For more information about this, skip down the page to the Advanced Section.可以使用注释域定制并实现更多高级功能,例如,你可以利用注释域的前10字节来存储密码,Master Server可以从主机获取更新并提取密码.可以进行比较并拒绝主机的更新(密码检查错误)。当然,你需要下载并修改Master Server 的源代码,有关这方面更多的信息,请跳过该页并前往高级部分。
Registering a game 注册游戏Before registering a game it is important to set the NAT functionality properly by setting useNat parameter for Network.InitializeServer to ***e or false depending on the networking capabilities of the host. The user hosting the game should know if he has a NAT address or not and if the game port is connectable on the public internet, so before hosting the game he will indicate if NAT assistance is needed or not.A server might be started with code similar to this:根据主机的网络情况,在注册游戏前,最重要的是通过设置Network.InitializeServer的参数useNat为***e或者false来正确设置NAT(网络地址转换)功能。在连接游戏之前,用户主机应该知道他是否是一个NAT网络地址或者可连接的游戏端口是否在公网上,他需要知道是否需要NAT的协助。服务器端的启动代码看起来像这样:function OnGUI() {
if (GUILayout.Button ("Start Server"))
{
  // Use NAT punchthrough if no public IP present
  Network.InitializeServer(32, 25002, !Network.HavePublicAddress());
  MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all");
}
}Here we just decide if NAT punchthrough is needed based on if the machine has a public address or not. There is a more involved function available called Network.TestConnection() which can tell you if the host machine can do NAT or not. It also does connectivity testing for public IP addresses to see if a firewall is blocking the game port. Machines which have public IP addresses always pass the NAT test but if the test fails then the host will NOT be connectable to NAT clients. In such a case the user needs to be informed that in order to host a game he needs to set up port forwarding if possible. Usually people with cheap broadband connections will have a NAT address and not be able to set up port forwarding (as they don't have a personal public IP address at all). In these cases, if the NAT test fails, the user should be informed that&nbsp***nning a server is inadvisable as no one but clients on the same local network will be able to connect.这里我们根据机器是否具有一个公共地址来决定是否需要使用NAT穿透,有个函数 Network.TestConnection(),可以告诉我们主机是否可以NAT。它可以连接公网IP,测试是否有防火墙阻止了游戏端口。有公网IP的主机通常可以通过NAT测试,但是如果测试失败主机将不能连接NAT客户端。在这种情况下,用户需要知道,他可能需要建立端口转发。通常人们会有一个NAT地址,不能设置端口转发(因为没有一个专有的公网IP)。这种情况下,如果NAT测试失败,用户会被告知,运行服务器是不行的,但在同一个局域网的客户端是可以连接的。If a host enables NAT functionality without needing it, no harm is done and it will still be connectable, however, client which cannot do NAT punchthrough will think they cannot connect to him as he has NAT enabled.如果一个主机开启了NAT功能但却不能用,这样也没什么危害,它仍然可以连接。但是,不能实现NAT穿透的客户端将不能连接到主机,如果它开启NAT的话。
Connecting to a game 连接到游戏The host information, HostData, object sent during host registrations or queries contains the following information:主机信息,主机数据,对象在登记或查询时包括如下信息: This information can be used by clients to see the connection capabilities of the hosts. When NAT is enabled you need to use the GUID of the host when connecting. This is automatically handled for you when you connect to the HostData s***ct directly. The connect routine might look something like this:这些信息可以被客户端用来查看网络连接能力,当使用NAT连接时,需要使用到主机的GUID.当你直接连接到主机数据时,这些信息都会被自动使用,这里有个大致的连接的范例:function Awake() {
MasterServer.RequestHostList("MadBubbleSmashGame");
}function OnGUI() {
var data : HostData[] = MasterServer.PollHostList();
// Go through all the hosts in the host list
for (var element in data)
{
  GUILayout.BeginHorizontal();
  var name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
  GUILayout.Label(name);
  GUILayout.Space(5);
  var hostInfo;
  hostInfo = "[";
  for (var host in element.ip)
   hostInfo = hostInfo + host + ":" + element.port + " ";
  hostInfo = hostInfo + "]";
  GUILayout.Label(hostInfo);
  GUILayout.Space(5);
  GUILayout.Label(element.comment);
  GUILayout.Space(5);
  GUILayout.FlexibleSpace();
  if (GUILayout.Button("Connect"))
  {
   // Connect to HostData s***ct, internally the correct method is used (GUID when using NAT).
   Network.Connect(element);   
  }
  GUILayout.EndHorizontal();
}
}This example code prints out all the relevant information of the hosts returned by the Master Server. Other useful data like ping information or geographic location of hosts can be added to this.这个例子打印出所有主服务器返回主机的相关信息。其他的有用的数据,像ping信息或者主机的地理位置可以被加载到这里。
NAT punchthrough (NAT 穿透)Bear in mind that NAT punchthrough will not work 100% of the time so certain machines are not suited to&nbsp***n as servers as some clients can connect to them and certain client might have trouble connecting to any NAT server.要知道NAT穿透并不是100%都能工作,所以某些机器并不合适做服务器或客户端。By default NAT punch through is done with the help of the Master Server but this functionality does not need to rely on the Master Server. The Facilitator is the one who is actually used for the NAT punchthrough routine. If two machines are connected to the Facilitator, one can directly connect to the other if as long as it uses the external IP and port. The Master Server is used to provide this external IP and port information which is otherwise hard to determine. That is why the Master Server and Facilitator are so tightly integrated. The Master Server and Facilitator have the same IP address by default, to change either one use the MasterServer.ipAddress, MasterServer.port, Network.natFacilitatorIP and Network.natFacilitatorPort默认情况下NAT穿透是通过主服务器的帮助来完成的,但这个功能并不需要依靠Master Server。Facilitator是一个真正用于NAT穿透的程序。只要使用外部IP和端口就可以直接连接。主服务器是用来提供这种外部难以定位的IP和端口信息的。这就是为什么主服务器和调解器要如此紧密的结合起来。主服和调解器有相同的默认IP,改变它用MasterServer.ipAddress, MasterServer.port, Network.natFacilitatorIP 和 Network.natFacilitatorPortAdvanced 高级The Master Server is a completely separate entity from the Unity editor itself. It can be deployed on Windows, Linux and Mac OS, and anyone can have their own Master Server. Unity Technologies has a dedicated Master Server available for anyone to use which is the default server used.Master Server是一个完全独立的实体。它可以部署到windows,Linux,macOS。unity技术提供一个专门的主服给所有人作为默认的服务器使用。There might be cases where you want to modify how the Master Server handles information and how it communicates. Maybe you need to do some optimizations of how the Master Server handles host data information from the individual game servers. Maybe you need to limit how many hosts the clients will receive when they request the host list, either numerically or through some kind of filter like location. This would mostly be bandwidth optimizations but anything can be done as long as it communicates properly with the Unity player.有些情况下,你可能想知道如何修改主服的处理信息,如何进行通信。或许你需要去做一些优化,如何从主服向个别游戏服务器处理主机数据。也许你需要限制主机收到的列表,无论是数字或通过一些实物,如过滤器的位置。主要是宽带的优化。只要大家相互团结,妥善沟通,所有事情都可以做到。To perform any of these modifications, you must build, deploy, and host your own Master Server. Information about how to do this can be found on the Master Server Build page.要执行任何这些修改,您必须建立,部署和托管您自己的主服务器。有关如何执行此操作可以在Master Server Build page找到。
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-23 13:02 , Processed in 0.095023 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部