简体   繁体   English

实时Java游戏:Socket或RMI

[英]Real-time java game: Socket or RMI

I'm developing a game, and I thought I will add a multiplayer options. 我正在开发游戏,我想我会添加一个多人游戏选项。 It's a real time game, like Snake or something like this, so I'm updating the sprites positions really fast: 这是一个实时游戏,例如Snake或类似的东西,所以我要非常快地更新精灵位置:

while (isRunning) {
   ...
   if (sprite instanceof PlayerSprite) {
            PlayerSprite player = ((PlayerSprite) sprite);
            collisionManager.checkCollision(player, map, elapsedTime, currTime);
            updatePosition(player, elapsedTime);
            player.animeUpdate(elapsedTime);
   }
   ...
}

I tried first with RMI: I stored every position in an object and I made it remote. 我首先尝试使用RMI:我将每个位置存储在一个对象中,并将其设置为远程。 So both the server and the client can update it. 因此,服务器和客户端都可以更新它。 But as I said it needs to update real time. 但是正如我所说,它需要实时更新。 And the RMI doesn't look like it can solves this. 而且RMI似乎无法解决此问题。 But I'm really a beginner, so I don't know for sure. 但是我真的是一个初学者,所以我不确定。

So my question is: RMI or simple IO (socket) would be the best solution for a real time game? 所以我的问题是:RMI或简单的IO(插槽)将是实时游戏的最佳解决方案?

If socket is better: Can I send the position-storing object via ObjectOutput/ObjectInput to the client, update the object (and draw to the screen) and then send back the updated object to the server, and so on? 如果套接字更好:我可以通过ObjectOutput / ObjectInput将位置存储对象发送到客户端,更新对象(并绘制到屏幕上),然后将更新后的对象发送回服务器,依此类推吗?

Thanks in advance:) 提前致谢:)

So my question is: RMI or simple IO (socket) would be the best solution for a real time game? 所以我的问题是:RMI或简单的IO(插槽)将是实时游戏的最佳解决方案?

This question is easy enough to answer. 这个问题很容易回答。 Between the two, sockets are the way to go. 在两者之间,套接字是必经之路。 RMI contains a significant amount of overhead, which slows the transport time. RMI包含大量开销,这会减慢传输时间。 The goal with something real-time is to reduce the time between sending and receiving information to be as short as possible. 实时的目标是将发送和接收信息之间的时间缩短到尽可能短的时间。

If socket is better: Can I send the position-storing object via ObjectOutput/ObjectInput to the client, update the object (and draw to the screen) and then send back the updated object to the server, and so on? 如果套接字更好:我可以通过ObjectOutput / ObjectInput将位置存储对象发送到客户端,更新对象(并绘制到屏幕上),然后将更新后的对象发送回服务器,依此类推吗?

You can, but as already mentioned, it is slow. 您可以,但是如上所述,它很慢。 You will need to either use a library that has a client-server messaging system (I use Kryonet ) or write up your own protocol. 您将需要使用具有客户端服务器消息传递系统的库(我使用Kryonet )或编写您自己的协议。 The key thing, which may seem obvious, is that the client and server have an easy way of understanding each other. 看起来很明显的关键是,客户端和服务器之间有一种容易理解的方式。

I will not suggest to use either Socket or RMI! 我不建议使用Socket或RMI!

In my opinion you should go with REST Services to handle multi player. 我认为您应该使用REST服务来处理多人游戏。

If you're for speed, then don't use Java serialization as it is much slower than JSON serialization . 如果您追求速度,请不要使用Java序列化,因为它比JSON序列化要慢得多

JSON is fine , personally I would give a try to Akka Remote Actors which utilize Google Protocol Buffers, though it would require studying. JSON很好 ,就我个人而言,我将尝试使用Google协议缓冲区的Akka Remote Actors ,尽管它需要学习。

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

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