In this tutorial we show how to join chat rooms.

 

We assume that the developer already owns an account into the ArenaDaemon web platform and

owns the knowledge for creating a new application and retrieving the correspondent appKey.

For more information about how to create a developer account and for setup a new application,

please refer to the chapter "Create an appKey".

 

The chat module provides a set of methods within the SDK that allows players to join rooms

for exchanging textual messages.

 

The number of different rooms is theorically infinite.

A new room is created when the first player decides to join it calling the method

joinRoom() of the chatConnector property of the unique BDArenaConnector instance.

 

Said that, we can jump into the code! 

Create a new Android project and include the SDK as shown in chapter SDK Installation and setup.

 

chat_create_controllers 

This sample application uses a ViewPager and a FragmentStatePagerAdapter.

Each Fragment will represent a different chat room.

 

The most important class is the ChatFragment that will manage

all the actions and the events related to the chat room to which it will be associated with.

  

Before doing any action with the daemons, the local player needs to be authenticated

so we need to add the following code to the MainActivity class:

 

 

protected void onStart() {

        ...

 

        if(!BDArenaConnector.hasInstance()) {

                // init BDArenaConnector

                BDArenaConnector.initializeWithAppKey(this, "YOUR_APP_KEY", true);

                BDArenaSettings.setShowNotificationPanel(true);

        }

 

        ...

        // register observer to handle the callbacks

        authHandler = new ArenaAuthHandler();

        BDArenaConnector.getInstance().registerEventsObserver(authHandler);

        BDArenaConnector.getInstance().requestAuth();

 

}

 

protected void onStop() {

        ...

        BDArenaConnector.getInstance().unregisterEventsObserver(authHandler);

}

 

 

The above chunck of code will mind to perform player authentication in order to enable access to 

chat features.

 

In the MainActivity, we add the listener for retrieving local player's authentication results.

When the local player will be successfully authenticated, we'll connect to the chat module.

When the connection to the chat will be estabilished, we'll create the PageAdapter to handle

the different ChatFragment.

 

 

 

public void arenaConnectorAuthReceivedForLocalPlayerWithData(

    BDArenaPlayerData localPlayerData, boolean alreadyMet, boolean isOffline) {

 

        // connect to chat daemon

        BDArenaConnector.getInstance().getChatConnector().connect();

}

 

public void arenaConnectorAuthFailedForLocalPlayerWithError(BDArenaError error) {

        // Handle errors

}

 

public void arenaChatConnectionEstabilished() {

 

        // init views on chat connection enstabilished

        viewPager.setAdapter( new ChatPagerAdapter( getSupportFragmentManager() ) );

        ...

 

}

 

public void arenaChatConnectionFailedWithError(BDArenaError error) {

        // Handle errors

}

 

public void arenaChatConnectionDidClose() {

        // Handle errors

}

 

 

The ChatFragment implements several mechanisms and takes care fill a ListAdapter

in order to show the messages received from players.

We put the focus on the EditText used by the local player to send messages to the chat room

and on the BDArenaConnector delegates that inform about the events that occur during a chat session.

 

First of all, the ChatFragment registers a new events observer, joins the proper chat room (through

the method joinRoom()) and allocates a ChatListAdapter and uses it to store the messages

received into the chat room (both from remote players and sent by the local player).

 

Besides, we implement the OnEditorActionListener method as follow:

 

 

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if(actionId == EditorInfo.IME_ACTION_SEND) {

                ChatMessage message = new ChatMessage();

 

                message.setAvatar(BDArenaConnector.getInstance().

                    getLocalPlayerData().getAvatarImageThumb());

                message.setSender(BDArenaConnector.getInstance().

                    getLocalPlayerData().getNickname());

                message.setText(sendMessageET.getText().toString());

 

                listAdapter.newMessage(message, roomName);

                sendMessageET.setText("");

 

                BDArenaConnector.getInstance().

                    getChatConnector().sendMessageInRoom(message.getText(), roomName);

       }

 

       return true;

}

 

 

The code above sends the message in room and adds the sent message to the listAdapter 

associating it with the local player's information:

 

For the management of the events generated by remote players, we implement the following

BDArenaChatConnectorListener methods:

 

 

public void arenaChatRoomJoined(String _roomName,

    int numberOfPlayers, ArrayList<BDArenaPlayerData> players) {

 

        if( _roomName.equalsIgnoreCase(roomName) ){

                // do your stuff for the room managed by this fragment

        }

}

 

public void arenaChatPlayerJoinedRoom(BDArenaPlayerData playerData, String _roomName) {

        if( _roomName.equalsIgnoreCase(roomName) ){

                // do your stuff for the room managed by this fragment

        }

}

 

public void arenaChatPlayerLeftRoom(BDArenaPlayerData playerData, String _roomName) {

        if( _roomName.equalsIgnoreCase(roomName) ){

                // do your stuff for the room managed by this fragment

        }

}

 

public void arenaChatNewMessageReceivedInRoomFromPlayer(String msg,

    String _roomName, BDArenaPlayerData playerData) {

 

        if( _roomName.equalsIgnoreCase(roomName) ){

 

                ChatMessage message = new ChatMessage();

                message.setAvatar(playerData.getAvatarImageThumb());

                message.setSender(playerData.getNickname());

                message.setText(msg);

 

                listAdapter.newMessage(message, roomName);

 

        }

 

}

 

 

IMPORTANT: in each method of the listener we have implemented the

if( _roomName.equalsIgnoreCase(roomName) ) instruction.

This because each ChatFragment, as said above, registers itself

as observer of the SDK's events and, therefore, will receive events related to every room joined

by the local player.

In order to filter events and ensure that each ChatFragment will process only

the events related to the chat room it is associated with, we store the room name

in an instance variable (roomName) and we use it as filter when some event occurs.

 

The application looks like follows:

tutorial_chat_app 


 

 

Sample project

Download the sample project for this tutorial.