• Examples (iOS)
• Examples (Java/Android)
• Examples (C++)
Server API Documentation

 

» Simple Chat

» Overview

The Simple Chat example extends the Connector sample to create a basic chat environment for connected users.

Exchanging chat 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 buddy messages) if the Buddy List system is enabled. Also, additional "super powers" are given to users being recognized as moderators or administrators.

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

The Simple Chat example just shows how to use the basic public messages, to keep the graphical interface as simple as possible. We leave to you to extend it to include private messaging, moderation controls or activate the anti-flood and words filtering.
After the application is launched, the user is prompted to enter a user name and connect. Once submitted, the user is connected to SmartFoxServer 2X and a logged into the BasicExamples Zone available in SFS2X by default. As a logged in user, we can then participate in a basic chat room, by sending and receiving public messages.

>> DOWNLOAD the source files <<

» Installation

» Source code setup

The example assets are fully contained in a project file and don't require a specific setup: simply open the SimpleChat.xcodeproj file contained in the folder with Xcode.

» Running the example

In order to run the application follow these steps:

  1. make sure your SmartFoxServer 2X installation contains the BasicExamples Zone definition;
  2. start SmartFoxServer 2X;
  3. make sure the client will connect to the right IP address by editing the <ip> entry in the xml file available under the Supporting Files folder in Xcode;
  4. select the iPhone Simulator from the Scheme dropdown and click the Run button to launch the simulator.

» Code highlights

Unlike the previous Connector example, the SmartFox client for the application is instantiated within the AppDelegate and SFS events are delegated to the AppDelegate as well. Once the application loads, a connection is automatically made with SmartFoxServer in the AppDelegate's application method:

	- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
	{
	    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
	    self.viewController = [[[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil] autorelease];
	    self.window.rootViewController = self.viewController;
	    [self.window makeKeyAndVisible];
	
		/*
	     Create a connection to SmartFoxServer as soon as the application has launched.
	     First, allocate a SmartFox2XClient instance
	     */
		_smartFox = [[SmartFox2XClient alloc] initSmartFoxWithDebugMode:YES delegate:self];
	
	    /*
	     Load our XML config file and once done, attempt a connection
	     */
		[_smartFox loadConfig:@"config.xml" connectOnSuccess:YES];
	
	    return YES;
	}

You'll notice that the client loads the config.xml file and connects to SFS on success. Once connected, a message is presented to the user that they are able to log on.

You'll also notice, that the interface declaration in the header file for the AppDelegate includes two protocol implementations:

	@interface AppDelegate : UIResponder <UIApplicationDelegate,ISFSEvents>

The inclusion of ISFSEvents is what enables us to listen for SFS2X events in the AppDelegate.

The login button press is handled by onLoginTouchUp in the LoginViewController implementation which simply calls the AppDelegate's login method. The following listener handles the related event when the login is performed successfully.

	- (void)onLogin:(SFSEvent *)evt
	{
	    NSLog(@"AppDelegate::onLogin");
		[_smartFox send:[JoinRoomRequest requestWithId:@"The Lobby"]];
	}

Once logged in, a JoinRoomRequest is sent to connect to the Room called "The Lobby" and, if successful, the application will run through the onRoomJoin method which will switch the view to the ChatViewController.

The ChatViewController is divided up into three distinct parts: chat entry, chat log, and user list. Our UI components are defined in the header file:

	@property (nonatomic, retain) IBOutlet UITextField *textField;
	@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
	@property (nonatomic, retain) IBOutlet UITableView *users;

Our ChatViewController implementation contains numerous methods to handle events coming from the AppDelegate (remember, the SFS2X client is a property of the AppDelegate and handles events there). When a public message is received, the event gets handled in the AppDelegate and is subsequently passed to the ChatViewController for display.

The following AppDelegate method is executed to send a public message:

	- (void)sendPublicMessage:(NSString *)message
	{
		[_smartFox send:[PublicMessageRequest requestWithMessage:message params:nil targetRoom:nil]];
	}

While this method passes the public messages sent by other clients to the controller to be processed:

	- (void)onPublicMessage:(SFSEvent *)evt
	{
	    NSLog(@"AppDelegate::onPublicMessage");
	    ChatViewController * cvc = (ChatViewController *)_viewController;
		if ([cvc respondsToSelector:@selector(onPublicMessage:)])
	    {
			[cvc onPublicMessage:evt];
		}
	}

The same methodology applies for users entering and exiting the Room. Each time there is a change in the Room users, the onUserCountChange event is handled in the AppDelegate, which calls a similarly named method in the ChatViewController to refresh the user list.

You can now proceed to a more advanced example showing the implementation of a turn-based game.

» More resources

You can learn more about the SmartFoxServer basics by consulting the following resources: