mardi 4 juillet 2017

What is the best way to communicate between client socket and server socket which suppors multiple types of action?

In short:

The main problem is, for an online game, server should support multiple types of action which client wants to do, such like: "Sign in game." , "Create a game room." , "Launch a game.", "send a chat message.", "Get a room list." ...

But if I'm going to use Socket, I'm wondering what is the best way to distinguish out the type of the action from each message received or sended between server and client.

This is what the deployment of my online game looks like:

Architecture

For supporting multiple types of action, my first idea:

Communication

If I choose to use JSON, it might look like:

{
    "action":"signIn",
    "data":{
        "id":"00001",
        "name":"waterball"
    }
}

and then the Server side can distinguish out the action by parsing the JSON property.

But a problem occurs, I will have lots of "if-else" descriptions like:

if ( action.equals("signIn") )
    ....
else if ( action.equals("createRoom") )
    ....
else if ( action.equals("launchGame") )
    ....

And the program can be very unscalable and unmaintainable.

So please share your idea about what is the better way to support multiple types of action between Server/Client socket.

Thanks.

Aucun commentaire:

Enregistrer un commentaire