Skip to content

Getting started with the Client

Daniel B edited this page Mar 31, 2016 · 4 revisions

###Initialising.

To create the client just initialise a new mphx.client.Client. First pass the IP of the server and then the port. This should be identical to what you setup on the server. When you are ready to connect, just call the connect method.

var clientObject = new mphx.client.Client("127.0.0.1",8000);
clientObject.connect();

###Working with 'players' Players are not automatically handled for you with MPHX, however it is simple to integrate them. You may already have a 'player' class with your library (eg- Extending FlxSprite in haxeflixel). The option that is easiest is to use the dynamic variable data that is present with every Connection object, How you wish to keep track of them is up to you though. Another option is to have a Map that connects a username or ID to a Player object. You can keep track of this username / ID on both the client and the server. When a player joins, it can send it's desired ID or username, the server and client both register this in their map, and whenever a specific players needs to be referenced, a username or ID is passed.

This has to be thought out. If a client sends a message along with it's username to move to a specific location, that username could be cheated to be another players username, thus allowing cheaters to control other players. In order to mitigate this, the client should not pass it's username every operation, it should only be passed once when it joins, and then the Server maps a Connection to an ID/username.

See the 'haxeflixel' example of this in practise.

###Sending a message to the server. In order to send a message and some data to the server, call the send method. The data can be anything you like, including anonymous objects. clientObject.send("Event Name",someData)

###Detecting a disconnection. Clients have a variable called onConnectionClose which is of type String->Void. This is a variable that points to a function with one string argument, and returns nothing. (more info here). The string argument will be the reason for the request to close. Here is an example of using the disconnect function binding.

client.onConnectionClose = function (reason:String){
    trace("The client disconnected because "+reason+". Check your internet and try again soon.");
}
Clone this wiki locally