[Kryonet] How to force client to disconnect?

Hello everyone! I am developing a StateBasedGame in Slick2D and i want to make it singleplayer and multiplayer.

My question is:
Is there any way to make the client DISCONNECT (not close) when exiting from the MP state?

I don’t know any way from the api, but you could just send a “You have to disconnect” message.

I’d do it that way:


public class Message {
    // Use these to send messages of different "types".
    // With bytes you can only have 255 different Messages.
    // Use "int" for example, to enable ~4.294.967.300 different Messages.
    public static final byte DISCONNECT = 1;
    public static final byte WHATEVER = 2;

    // The Message itself, so either 1 or 2.
    public byte msgValue;
}

That’s it! My problem was that i was looking for a disconnect function in client/server, and i found it in connection.

Thanks!

With int and Kryo’s variable length encoding you get 0-128 in 1 byte, 0-16384 in 2 bytes, etc. If you want to save space, you can use a different class for each message. In the above, KryoNet will send 1 byte (variable length int) to identify the Message class, then 1 byte for msgValue. You probably want to use separate classes anyway, as each class can have different fields. So I would use this:


public class Disconnect { }

When the client receives that, she disconnects.

Didn’t knew that… Thanks creator of Kryo :slight_smile:

Note Client extends Connection, so you can do client.disconnect().

:smiley: