Aksirba
Aksirba

Reputation: 111

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:

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. 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. 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?

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?

Thanks in advance:)

Upvotes: 0

Views: 1889

Answers (3)

Chill
Chill

Reputation: 1113

So my question is: RMI or simple IO (socket) would be the best solution for a real time game?

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. 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?

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. The key thing, which may seem obvious, is that the client and server have an easy way of understanding each other.

Upvotes: 2

Andrey Chaschev
Andrey Chaschev

Reputation: 16526

If you're for speed, then don't use Java serialization as it is much slower than JSON serialization.

JSON is fine, personally I would give a try to Akka Remote Actors which utilize Google Protocol Buffers, though it would require studying.

Upvotes: 1

VINAY SHARMA
VINAY SHARMA

Reputation: 11

I will not suggest to use either Socket or RMI!

In my opinion you should go with REST Services to handle multi player.

Upvotes: 1

Related Questions