1
\$\begingroup\$

I recently built a game with libgdx for my desktop. I would like now to make it a multiplayer game.

How could I do this? I was thinking about using node.js but there are some problems: I use Box2D and other Java libraries to help me making the game simulation and this would be hard to write all over again in Javascript.

The other option would be to write a Java server. Honestly I never used sockets or anything related to networks in Java (I have always been a C++ guy). So, what would you guys recommend? What would be the best option?

Also I would like to consider the performance since Box2D simulations are a bit heavy and with Java I am able to use multi-threading while node.js doesn´t allow me to do that. (I also think Java itself is a bit faster than Javascript).

Just one more note. As I said before I never done anything network related with Java, so if you think Java is the best option could you also recommend some technologies and/or java libraries and/or articles about networking or server creation in Java?

Thanks for your time!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Turning a single player game into a networked game after the fact is quite difficult. \$\endgroup\$
    – Almo
    Commented Jan 24, 2017 at 21:38
  • \$\begingroup\$ You also most definitely don't want to rewrite it in another language, it's already really fricking hard to make a multiplayer game. It's WAY harder to make a singleplayer game into a multiplayer game and probably even harder to make it in a different language. \$\endgroup\$
    – Elva
    Commented Jan 24, 2017 at 21:41
  • \$\begingroup\$ Thank you for you replies, I really appreciate. I will probably try to make the server in Java. Should I use any library you should I use only the tools that Java provides? \$\endgroup\$ Commented Jan 24, 2017 at 21:42

1 Answer 1

1
\$\begingroup\$

Java sockets are not much farther from C++ sockets. It is actually a lot similar, with some managed stuff here and there.

You should gather some info on how other people are doing it, that should help you decide:

  • I recommend checking on eAthena code to check how to handle multiple users online, receiving data from a tcp channel without blocking and other techniques (check the login-server, then char-server, then map-server, this order)

  • I recommend you read the specs/protocol of any multiplayer game (Minecraft for example is very detailed) if you've never seen one before. This will help you have an idea on what kind of data to send over the sockets.

  • These posts from GafferOnGames are also very useful.

Your better option is to indeed just make a new java module under your already made game, share the codebase and start coding.

Just one more note. As I said before I never done anything network related with Java, so if you think Java is the best option could you also recommend some technologies and/or java libraries and/or articles about networking or server creation in Java?

Libgdx has some networking built-in. But it's very simplistic and it assumes a lot of stuff. It could work for you, but if it doesn't then I really recommend you use pure java sockets.

Opening a connection to a server is very easy:

channel = SocketChannel.open();
channel.configureBlocking(false);
channel.socket().setTcpNoDelay(true);

and receiving data is as easy as:

ByteBuffer rdata = ByteBuffer.allocateDirect(1024);;
channel.read(rdata);

But there are some stuff you might need to keep in mind: Java's ByteBuffer interface is a pain in the ***, you might spend some time trying to understand the concepts on flip, compact, drain, when you can read/write etc.

The rest should be easy. Good luck!

\$\endgroup\$
5
  • \$\begingroup\$ wiki.vg/Protocol is a documentation of the current minecraft protocol. Also, I'd recommend using something like netty for networking. \$\endgroup\$
    – tkausl
    Commented Jan 25, 2017 at 3:45
  • \$\begingroup\$ @tkausl the minecraft protocol is bad and unnecessarily complicated, don't use it as a resource \$\endgroup\$
    – Bálint
    Commented Jan 25, 2017 at 7:00
  • \$\begingroup\$ @Bálint Could you suggest some alternatives? \$\endgroup\$
    – Vaillancourt
    Commented Jan 25, 2017 at 14:45
  • \$\begingroup\$ Is there any Java multiplayer game at Github that I could use as resource? \$\endgroup\$ Commented Jan 25, 2017 at 16:21
  • 2
    \$\begingroup\$ @Daniel You should just learn networking then apply that for your choice of language. \$\endgroup\$
    – Bálint
    Commented Jan 25, 2017 at 17:17

Not the answer you're looking for? Browse other questions tagged .