Android Examples: Plain Talk

» Plain Talk

The Elementary Talk example uses extra Android UI elements to build on the basic Connector example and shows how to distribute public messages in SmartFoxServer 2X using the Android Two.Two emulator.

Exchanging talk messages is the most basic functionality provided by SmartFoxServer 2X: users connected to the same Zone can send public messages to all the other users in the same Room, private messages to single users (regardless the Room they are in) and a different kind of private messages (called mate messages) if the Pal List system is enabled. Also, extra "super powers" are given to users being recognized as moderators or administrators.

The talk functionalities provided by SmartFoxServer 2X can be the core of a fully featured talk website/application, or a complementary system to permit users to exchange messages during game matches or in other kind of applications. In all cases SmartFoxServer provides extra features to build a finish talk system, like moderation controls, anti-flood filtering capabilities and an advanced words filter which supports regular expressions and white-list/black-list modes.

The Elementary Talk example just shows how to use the basic public messages, to keep the graphical interface as elementary as possible. We leave to you to extend it to include private messaging, moderation controls or activate the anti-flood and words filtering.

As with the Connector example, when the application is launched, the user is prompted to come in SmartFoxServer 2X connection details (the pre-filled connection parameters used by this example are the same as for the previous examples; check the tutorial for extra informations on the default value for the IP address).

After the details are submitted, a connection to SmartFoxServer 2X is attempted and an event is then fired to notify if the connection has been established successfully.

Once connected to SmartFoxServer 2X the user is asked to come in a treat for the talk session. Using this treat, the user is logged into the BasicExamples Zone available in SFS2X by default. As a logged in user, we can then toggle inbetween two views, one that displays status updates and public talk messages, and the other that simply lists all users logged into the Room joined by default.

» Installation

In order to access the application’s source code and run it in the Android Virtual Device (emulator), go after these steps:

  1. Make sure your SmartFoxServer 2X installation contains the BasicExamples Zone definition;
  2. Begin SmartFoxServer 2X;
  3. In Eclipse, import the Android project contained in the /source folder from the download above;
  4. Launch the app and switch the connection details if your SmartFoxServer 2X is not installed on your machine’s localhost.

» Code highlights

In this example we go after the same scheme as in the Connector, so the connection steps basically the same the only difference is that we ask the user for a Nick instead of using an empty string

The application contains three layouts (located in the layout folder of the project):

  • The connector layout: Where we choose the IP and port and select “Connect”
  • The login layout: Where the nick name is entered
  • And the talk layout: This view contains a TabHost with two tabs, the very first one for the talk itself and the other one for the list that provides the users presently connected to the room.

To keep the code as ordinary as possible, layouts are packaged in a FrameLayout. This container uses the showLayout method to showcase the adequate layout (In a real application, activities or fragments are more suitable to implement this idea)

The class ChatMessage.java represents a message for the talk holding the following attributes and its getters/setters

An ArrayAdapter is used to manage the list of users as they join and leave the default room and a custom-made ArrayAdapter (MessagesAdapter.java) to display the messages. For each message the MessagesAdapter inflates the layout row_message.xml and sets the texts, background and gravity depending on the kind of message (incoming, outgoing or system message).

The following code is invoked when the user hits the Send button at the bottom of the Talk tab. It takes the text entered in the input field and, as long as it is not empty, creates a fresh PublicMessageRequest which is sent to the server.

Incoming server events are, like in the previous example, treated in an overridden dispatch method. For this example, we listen to a fresh event, PUBLIC_MESSAGE. When we receive an incoming public message we build a ChatMessage and add it to the MessagesAdapter. We also listen the USER_ENTER_ROOM and USER_EXIT_ROOM in order to keep the user’s tab up to date.

As in the Connector example we free the resources in the Activity’s onDestroy:

» More resources

You can learn more about the SmartFoxServer basics described in this tutorial by consulting the following resources:

Related video:

Leave a Reply

Your email address will not be published. Required fields are marked *