3. Adding a client-server message

We have created a new game server project and succeeded to build and to run, but the game server doesn’t do anything meaningful so far. To make your game server do specific jobs, you should add message handlers for messages coming from the client. In this chapter, we will augment the server by adding a client message handler and to test the added handler.

3.1. Adding a client message handler

We will add a client message handler so that let the game server reply with a word world on the receipt of a client message hello. Open up src/event_handlers.cc from the generated source tree and you will be able to locate code like this:

HandlerRegistry::Register("login", OnAccountLogin, login_msg);
HandlerRegistry::Register("echo", OnEchoMessage);

Have you already noticed what the code means? Yes, the code maps client messages (login and echo) to their matching message handlers. (OnAccountLogin and OnEchoMessage), so that the game server correctly invokes a right handler once it receives one of those messages from the client. We will append another handler registration code after the echo stuff.

HandlerRegistry::Register("hello", OnHello);

And we need to define the handler function OnHello before the new registration code. It simply returns a server message type world to the client.

 void OnHello(const Ptr<Session> &session, const Json &message) {
  Json empty_response;
  session->SendMessage("world", empty_response);

Yay! We’re done. Store the change and build the game server. If no compiler error, you can run the game server like this:

$ make
$ ./hello_world-local

3.2. Specifying the server’s listening ports

By default, iFun Engine listens at port 8012 for TCP connections and port 8018 for HTTP requests. Please remember that the same message handler is invoked on the receipt of a specific message type, regardless of network protocol. So, you can easily change network protocol (e.g., HTTP to TCP, or vice versa) while keeping your handlers and not changing the server’s behavior, if needed.

And listening ports can be changed by a configuration file hello_world/source/src/MANIFEST.json. Locate a component named SessionService and you can find JSON verse like this:

"SessionService": {
  "tcp_json_port": 8012,
  "http_json_port": 8018,

3.3. Testing a client message

From the configuration file illustrated above, we know that the server is enabled with both TCP and HTTP. For simplicity, we will test with HTTP rather than TCP in this example. Since we added a client-server message hello, we will invoke a url http://localhost:8018/v1/messages/hello with POST method and an empty body. Type in the command like this in your terminal window.

$ wget -qO- --post-data="{}" http://localhost:8018/v1/messages/hello


Instead of the terminal, you may do this using your Chrome or Firefox with a REST client extension. For example, Postman for Chrome.

If everything goes well, you will see a result like this (_sid might differ):


The result indicates that the game server responded a message world with a new session id on the receipt of hello.


iFun Engine assigns a new session id if incoming message does not have a session id. For an invalid session id, it silently ignore to prevent malicious probing.

In the next section, I will explain how iFun Engine handles MySQL database.


The format of iFun Engine’s header is the same as HTTP header. And the body can be of either JSON or Google Protobuf. So, it would not be difficult for you to implement own client-side module that handles incoming bare messages. But iFun Engine provides client-side modules for various game client engines including Unity3D and Unreal for fast development. Please visit iFunFactory Github account .


If you use JSON as a client-server message format, field name with a leading underscore is reserved. You must not use such a name.