简体   繁体   English

在java中正确发送播放器数据的方式

[英]Correct way of sending data of a player in java

I have some knowledge in using nitro-net for developing my games. 我对使用硝基网开发我的游戏有一些了解。 As of now, I am sending data of the player like this: 截至目前,我正在发送播放器的数据,如下所示:

  • Player attacks 玩家攻击
  • Client sends data to server that player has attacked 客户端将数据发送到玩家已攻击的服务器
  • Server sends this data to every other connected client 服务器将此数据发送到每个其他连接的客户端
  • The clients that receive the data check what action was sent ex ("attack_left") 接收数据的客户端检查发送了什么操作(“attack_left”)
  • An if statement checks what action was sent, then the client has the player execute the code for this action if语句检查发送了什么操作,然后客户端让播放器执行此操作的代码

Is this the best way of handling sending data to a server and then to multiple clients? 这是处理将数据发送到服务器然后发送到多个客户端的最佳方法吗? Should I just send a full player object instead? 我应该只发送一个完整的玩家对象吗?

Thanks! 谢谢!

In general, yes. 一般来说,是的。 It is best to only send the data you need. 最好只发送您需要的数据。 Sending the entire character's information can quickly become an issue if there are a lot of other players connected to the server. 如果有很多其他玩家连接到服务器,发送整个角色的信息很快就会成为一个问题。

For example, if you have 10 players all attacking at the same time, then the server receives 10 attacking commands from each client. 例如,如果您有10个玩家同时进行攻击,则服务器会从每个客户端接收10个攻击命令。 The server then will have to send each of those attacking commands to each other client that is connected. 然后,服务器必须将每个攻击命令发送给所连接的每个其他客户端。 So you end up with 90 [10 x 9 = 90] commands that are sent out by the server. 因此,您最终会得到服务器发出的90 [10 x 9 = 90]个命令。 If each of these commands is sending the entire character's information, that can easily get out of hand. 如果这些命令中的每一个都在发送整个角色的信息,那么这很容易失控。

If only a limited number of players are connected at a time though, you might be able to get away with sending the entire character's information, but you should decide how many players you want to support for your game. 如果一次只连接有限数量的玩家,你可以放弃发送整个角色的信息,但是你应该决定你想要为你的游戏支持多少玩家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM