如何为Unity3d设计我的在线游戏服务器? [英] How to design my online Game Server for Unity3d?

查看:129
本文介绍了如何为Unity3d设计我的在线游戏服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


最近,我一直在用unity3d进行多人游戏开发.我对Unity的MasterServer如何统一工作有了很好的认识.
但是现在我想从头开始制作自己的游戏服务器.经过研究,我知道我们可以使用Google云计算和应用引擎来制作自己的配对服务器和游戏主机服务器.
但是我完全不知道我们如何开始对服务器进行编码.我们是否使用简单的http请求和json结果来做到这一点?还是有其他任何技术可以实时与FPS类型的游戏一起使用.我认为发送http请求并等待它的结果可能不够快,无法在FPS类型的游戏中正常工作,在这种游戏中,我们每秒钟发送的数据超过100个.
起初,我想到编写php脚本并将其托管到某个url,然后向该url发送请求并等待其响应.但是我发现,如果使用此过程,仅从服务器获得一个响应,则至少要花费我0.5秒或更长时间.在这种情况下,游戏会有很多滞后.
我知道游戏服务器可以在某种tcp或udp网络上运行.但是我的问题是,我应该为服务器提供什么样的应用程序来接收这些数据并快速发送处理后的结果.


Recently, I have been doing multiplayer game development in unity3d. I have got the pretty good idea of how the Unity's MasterServer works in unity.
But now I want to make my own game server from scratch. I researched and got to know that we can use the Google cloud compute and the app engine for making our own matchmaking server and the game host server.
But I am totally clueless how do we start doing coding my server. Do we do it using simple http request and json result? Or is there any other technique that can in real time work with FPS type games also. I dont think that the sending http requests and waiting for it result can be fast enough to work in a FPS type games where every seconds we send more than 100s of datas.
At first I thought of writing php scripts and hosting it to some url, and then sending request to that url and wait for its response. But I found out that if I use this process then for just getting one response from the server it would take me atleast 0.5 seconds or more. In this condition, there would be a lot of lag in game.
I know that game servers work on some kind of tcp or udp networking. But my question is this that what kind of application shall I make for the server to receive these datas and send the processed results fast.

推荐答案

首先,我认为您将 MasterServer 与在游戏过程中发送和接收游戏数据所需的连接相混淆.

First of all I think that you are confusing MasterServer with the connection required to send and receive game data during gameplay.

MasterServer用于检索创建游戏的服务器的信息,例如IP地址和端口号.检索IP和端口号后,客户端可以使用这些信息直接连接到服务器.

MasterServer is used to retrieve the information of the server that created the game such as IP Address and port number. After you retrieve the IP and port number, the client can directly connect to the server with these information.

我们使用简单的http请求和json结果来做到这一点吗?

Do we do it using simple http request and json result?

对于 MasterServer ,可以.您也可以将其与PHP结合使用,以将游戏会话保存在数据库中.

For MasterServer, yes you can. You can also use it in combination with PHP to save game sessions on the database.

例如,当玩家想要创建一个新游戏时,您带走他们的游戏信息,例如游戏名称,密码,最大玩家数,玩家的IP地址,端口号,然后将其序列化为json并使用RESTful发送.

For example, when player wants to create a new game, you take you take their game information such as game name, password, max players, player's ip address, port number then serialize it to json and send using RESTful.

您可以使用PHP接收该游戏信息并将其存储到服务器中,以便其他玩家可以找到它.

You can use PHP to receive that game information and store it into server so that other players can find it.

这不应该影响性能,因为您仅使用它来创建,查询和销毁游戏季节.您使用它在游戏过程中发送游戏数据.

This shouldn't affect performance since you only using it to create, query and destroy game seasons. You are not using it to send game data during game play.

对于在游戏过程中发送数据,您可以使用原始套接字进行.使用 C#的TCP或UDP.如果要获得最大的性能,请使用 C ++ 进行本地化.Unity的UNet用 C ++ 编写.大多数FPS服务器都是用 C ++ 编写的,这是为了确保它们有史以来最高的性能.

As for sending data during gameplay, you do this with raw socket. Either TCP or UDP with C#. Go native with C++ with if you want to get the maximum performance sine Unity's UNet is written in C++. Most FPS server's are written in C++ and this is to make sure that they get the maximum performance ever.

请记住,服务器的类型很多,因此在本文中我无法涵盖所有​​服务器.我只关注 MasterServer ,它告诉客户端创建的游戏的IP地址,然后是不能建立玩家之间的直接连接时使用的中继.

Remember there are many types of servers so I can't cover all of them in this post. I will just focus on MasterServer which tells client the IP addresses on Games created then Relay which is used when direct connection between players cannot be established.

我建议您使用Google的Protobuf而不是Json来提高其性能和轻巧性.如果您发现任何比Protobuf更快的序列化API,请使用它.

I suggest you use Google's Protobuf over Json for its performance and lightweight. If you find any serialization API that's faster than Protobuf then use it.

直接连接:

服务器:

1 .使用Nat执行 2 .通过创建 TCP/UDP服务器.

3 .将游戏信息(游戏名称,ip,端口号)发送到MasterServer.

3.Send the Game Info(game name, ip, port number) to the MasterServer.

4 .当客户端连接到该信息时,通过使用 Protobuf send将数据序列化(FPS播放器位置?)开始向客户端发送数据.

4.When client connects with that information, start sending data by serializing the data(FPS player position?) with Protobuf send, to client.

5 .当您从客户端收到数据时,请使用 Protobuf 反序列化.

5.When you receive data from the client, deserialize it with Protobuf.

客户:

1 .连接到MasterServer并检索正在运行的游戏信息.

1.Connect to the MasterServer and retrieve running Game Informations.

2 .创建

2.Create a TCP/UDP client and connect to one of the IP and port number.

要发送和接收数据,请使用服务器#4 #5 中的步骤.

To send and receive data, use steps from Server #4 and #5.

有时,端口转发在某些设备上不起作用.在这种情况下,具有不同全局IP地址的两个播放器无法连接在一起,这就是中继和C ++发挥作用的地方.中继不应该通过http请求,json或php来完成.它应该使用C ++或类似的快速语言(例如python)制成.

Sometimes, port forwarding won't work on some devices. In that case, two players with different global IP Addresses cannot connect together and this is where a relay and C++ comes into play. The relay should not be done with http requests, json or php. It should be made with C++ or some similar fast language such as python.

同样,有很多方法可以做到这一点.最简单的方法是为所创建的每个游戏生成一个私钥.服务器/客户端相互发送数据时,他们必须在该数据中包含此密钥,并且此密钥将用于确定中继服务器应将数据发送给谁.

Again, there are many ways to do this. The easiest way is to generate a private key for each game that is created. When server/client send data to one another, they must include this key in that data and this key will be used to determine who the relay server should send the data to.

与中继的连接:

服务器:

1 .通过将游戏信息(游戏名称)发送到 MasterServer 来创建游戏.

1.Create Game by connecting to sending the Game Info(game name) to the MasterServer.

2 . MasterServer 会通过生成私钥并将其返回给服务器来进行响应.

2.MasterServer responds by generating a private key and returning it to the Server.

3 .使用该私钥连接到中继服务器.

3.Connect to the relay server with that private key.

4 .要将数据发送到其他播放器,请使用 Protobuf 序列化数据(FPS播放器位置?),然后发送到中继服务器.该数据还必须包含私钥.

4.To send data to other players, serialize the data(FPS player position?) with Protobuf then send to the Relay server. That data must also contain the private key.

5 .中继服务器从该服务器接收数据,读取私钥,然后使用相同的私钥将数据转发/发送到任何其他客户端.

5.Relay server receives data from this server, reads the private key and forward/send the data to any other client with the-same private key.

6 .当您从其他播放器发送的中继服务器接收数据时,请使用 Protobuf 将其反序列化.

6.When you receive data from the Relay server that the other player sent, deserialize it with Protobuf.

只要连接仍然有效并且游戏仍未结束,就从#4 重复到#6 .

Repeat from #4 to #6 as long as the connection is still alive and game is still not over.

客户:

1 .连接到MasterServer并检索正在运行的游戏信息.

1.Connect to the MasterServer and retrieve running Game Informations.

2 . MasterServer 作为响应,将游戏的私钥返回给客户端.

2.MasterServer responds by returning a private key of that game to the client.

3 .使用该私钥连接到中继服务器.

3.Connect to the relay server with that private key.

4 .要将数据发送到其他播放器,请使用 Protobuf 序列化数据(FPS播放器位置?),然后发送到中继服务器.该数据还必须包含私钥.

4.To send data to other players, serialize the data(FPS player position?) with Protobuf then send to the Relay server. That data must also contain the private key.

5 .中继服务器从该服务器接收数据,读取私钥,然后使用相同的私钥将数据转发/发送到任何其他客户端.

5.Relay server receives data from this server, reads the private key and forward/send the data to any other client with the-same private key.

6 .当您从其他播放器发送的中继服务器接收数据时,请使用 Protobuf 将其反序列化.

6.When you receive data from the Relay server that the other player sent, deserialize it with Protobuf.

只要连接仍然有效并且游戏仍未结束,就从#4 重复到#6 .

Repeat from #4 to #6 as long as the connection is still alive and game is still not over.

这篇关于如何为Unity3d设计我的在线游戏服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆