5.1 Tutorials: Connecting to the server

The source project of this example is available under the Examples/Unity/simpleConnect folder.

» Introduction

In this first tutorial we will see how you load and use the SmartFoxClient object and how you establish a connection to the server.

» The basics

Open the simpleConnect project folder from the "Examples/simpleConnect" directory using Unity and open the connection scene and have a look at the script component that is attached called ConnectionGUI.

In the top of the script we include the SmartFoxClient:

using SmartFoxClientAPI;

The line will always be present in all your "SmartFoxServer" applications as it imports the SmartFoxClient class and its relative helper classes.

The structure of the file is a basic Unity C# script with a Start() and OnGUI() method. The last HandleConnection() will be covered later.

//----------------------------------------------------------
// Setup variables
//----------------------------------------------------------
private string serverName = "127.0.0.1";
private int serverPort = 9339;
private string statusMessage = "";

//----------------------------------------------------------
// Called when program starts
//----------------------------------------------------------
void Start() {
	SmartFoxClient smartFox = new SmartFoxClient();
	SFSEvent.onConnection += HandleConnection;
	smartFox.Connect(serverName, serverPort);
}

We start out with set up of two simple variables that we will use to connect to the server: ip, port, as well as a statusMessage that we can display on screen.
If you're running the server locally (on the same computer where you run the examples) you can leave the default value 127.0.0.1 otherwise you should replace it with the ip address of the machine running SmartFoxServer.

The default port number should always be 9339, if you have changed it for some reason then the port variable should reflect this change.

The 3 lines of code in the Start() method will do most of the "magic": first, we create a new instance of the SmartFoxClient object, then we specify an event handler for the onConnection event and then we finally call the Connect() method to establish a connection between client and server.

» Connection handling

If you are a bit familiar with Unity then the expression "event handler" should not sound new to you. In fact many of the built-in callback methods in Unity use events to handle situations that will occur at an undefined moment in the future. For example when two objects collide in your game you have to implement the OnCollisionEnter event handler to have your code react to that particular collision event.

SmartFoxServer uses events quite a lot because you can receive messages at any time from the server or from other users.
All you will have to do is write the appropriate functions to manage these different situations.

Back to the example of this tutorial we need to write a function that handles the connection event and takes the right action whether the connection succedeed or not.

The following code is a simple example of connection handling:

//----------------------------------------------------------
// Handle connection response from server
//----------------------------------------------------------
void HandleConnection(bool success, string error)
{
	if (success) {
		statusMessage = "Connection succesfull!";
	} else {
		statusMessage = "Can't connect! Error: " + error;;
	}
}	

Upon reception of a onConnection message the HandleConnection method is called.
Every time you receive this notification a boolean parameter is also sent saying if connection was successfull or not as well an an error string containing the error message (if this was not a successful connection).

In a real-world example we could open a dialog box asking for username and password if the client could connect or show an error message if it failed.

SmartFoxServer can fire many different events based in the type of request it receives, some of them are:

- onConnection
- onLogin
- onRoomListUpdate
- onJoinRoom
- onJoinRoomError
- onPublicMessage
etc...

We'll analyze each one of them as we progress with more complex tutorials.

» Drawing the GUI

For every frame the Unity engine calls the OnGUI method to draw the GUI

In the code we simply draw the statusMessage variable to the screen, so we can see if the connection was successful or not.

//----------------------------------------------------------
// Draw GUI every frame
//----------------------------------------------------------
void OnGUI()
{
	GUI.Label(new Rect(10, 10, 500, 100), "Status: " + statusMessage);
}	

That's it! Press the start button in Unity and the client will attempt to connect to the server running SmartFoxServer.


  doc index