I would like your opinion on sending commands

As the title states I want your opinion. I am making a mutliplayer game to get better with java, and I am improving but I need your assistance. What do you think the best way to send a command from client to server? I was thinking output.write and placing a switch case on the server end. However with adding a chat feature this will end up causing problems. This being said, what do you guys think is the best way to proceed?

You could look into Kryonet, which is a very good java library for sending objects over the internet.

With Kryonet you’d simply send an object (yes, an instance of a java class) describing your Command you want to send and on the other side you’d chain a couple of [icode]if (sentObject instanceof SomeKindOfClass)[icode] to distinguish between chat commands and anything else.

The advantage over simple text output is that you don’t need to serialize something like numbers. If you’d send a position update for example, with simple text networking it would look like this:
Sent text: “Position x:10 y:20” and you’d need to break it up in parts (e.g. “Position”, “x:10”, “y:20”) and then you would need to care about String->Number conversion, etc.

With Kryonet it would look like this:


public class PositionUpdate {
    public int x;
    public int y;
}

And you could simply send that, and it would even be in a binary format :slight_smile:

Alright, I will take a look at it, thank you.