chat-application

Chat is another sample app build over the AppWarp S2 gaming solution. This chat app processes every user’s chat messages and looks for abusive words, if the chat contains any unacceptable word, the chat is blocked and the user is prompted not to send such kind of words and other users don’t get that message.

Note You will first need to create an application zone first through the admin dashboard. This is described in in our getting started page.

Complete API Guide

To start the server, call the AppWarpServer.start() with the object of BaseServerAdaptor and path of config file as parameters.

                String appconfigPath=System.getProperty("user.dir")+System.getProperty("file.separator")+"AppConfig.json";
AppWarpServer.start(new ChatServerAdaptor(), appconfigPath);
                

Here ChatServerAdaptor is an extended class from BaseServerAdaptor.

public class ChatServerAdaptor extends BaseServerAdaptor{}

The format for config file is as follows:

               {
    "TickTime" : 500,
    "LogLevel" : "INFO",
    "AdminUsername" : "admin",
    "AdminPassword" : "password",
    "HSQLDBFile" : "F:\\Suyash\\Shephertz\TestAdmin"
}           
 

In onZoneCreated() event of server adapter we set the zone adapter for the zones. Every time you create a zone through admin tool, onZoneCreated() is executed. Once a zone is created, it is also saved in local db, so even when you stop the server, on restarting the server, those zones are re-created and onZoneCreated() is called automatically.

In Zone adapter, the adapter for room is set on onAdminRoomAdded() function. Rooms can be created through either Admin tool or through client API. The rooms created through API are dynamic rooms and rooms created through admin tool are static. The static rooms are those rooms that are persistent i.e. on restarting the server, all rooms are restored. Whereas dynamic rooms are those rooms which only stay in memory, as soon as server is stopped, they are completely gone. When server starts, all static rooms are loaded and onAdminRoomAdded() is called for each room. Whereas handleCreateRoomRequest() is called for each create room request during run time.

To filter the chat, messages we had created an array of popular abusive words. Every time a chat is received, handleChatRequest() is called.

               @Override
    public void handleChatRequest(IUser sender, String message, HandlingResult result)
    {
        System.out.println(sender.getName() + " says " + message);
        for(String word:blacklist)
        {
            if(message.indexOf(word) != -1)
            {
                sender.SendChatNotification("Admin", "You are not allowed to use abusive language" , sender.getLocation());
                result.code = 1;
                result.description = "Bad Words Used";
                result.sendResponse = true;
                result.sendNotification = false;
            }
        }
    }               

Here we checked if the received message contains any abusive word or not. If there exists any such word, the sender is notified that he is not allowed to use such language and his message is blocked. To block his message, we utilized the HandlingResult. Any value other than 0 set to code parameter of HandlingResult means an error. You can use any value to depict the nature of error, like I used error code 1 to represent the use of abusive language. The description parameter is provided to describe the error. Since its a failure, and we don’t want other users to see that chat, we set the sendNotification to false and sendResponse to true.