Introduction to haXe Remoting
haXe Remoting is a way to talk between different platforms. With haXe remoting you can communicate transparently, send data and call methods, between Server Side, JavaScript and Flash.
In order to use remoting, you need first to establish a Connection. There is two kind of haXe Remoting connections : haxe.remoting.Connection and haxe.remoting.AsyncConnection. As the name shows, the first one is used for synchronous connection where for example the method result can be directly obtained when calling a method, and the other one is an asynchronous connection where call results are events that will happen later in the execution process.
The following constructors are available to build a connection :
- on all platforms :
haxe.remoting.AsyncConnection.urlConnect(url : String)
will return an asynchronous connection to the given URL that can be a haXe/Server application. See below on how to implement the Server side.
- in Flash :
haxe.remoting.Connection.jsConnect()
will return a connection to the local JavaScript haXe code. The JS haXe code must be compiled with the classhaxe.Connectionincluded. This is only working with Flash Player 8 and more.haxe.remoting.AsyncConnection.amfConnect( url : String )
allows you to connect to an AMF Remoting server such as Flash Media Server or AMFPHP.haxe.remoting.SocketConnection.connect( sock : flash.XMLSocket )
allows remoting communications over anXMLSockethaxe.remoting.LocalConnection.connect( name : String )
allows remoting communications over a FlashLocalConnection
- in Javascript :
haxe.remoting.Connection.flashConnect( objname : String )
will return a connection to the given Flash Object. Some HTML properties must be set. This will only work with Flash 8 and more. Please note that the haXe Flash content must be loaded and include thehaxe.remoting.Connectionclass.
- on the Server :
haxe.remoting.Connection.urlConnect(url : String)
will work as the asynchronous version but in synchronous mode.haxe.remoting.SocketConnection.socketConnect(...)
can be used for real time communications with a Flash client which is using an XMLSocket to connect to the server.
Flash/JavaScript communications are only supported for Flash Player >= 8.
Data Serialization
haXe Remoting can send a lot of different kind of data:
- int and float numbers
- strings
- booleans
- null
- arrays
- anonymous objects
- classes instances (see below)
- enums instances (see below)
haXe Serialization can be used apart from haXe Remoting by using the classes haxe.Serializer and haxe.Unserializer. It enable you to store all kind of datas for medium or long term storage as a String.
Classes and Enum instances can be serialized as well using haXe Serialization. You only have to be sure that the class exists when unserializing the data or an exception will be raised. The fields and the class/enum names are stored in the serialized data, so for example you add methods after serialization, they will be present when unserializing the data.
Please be careful, there is no versioning, so if you modify a class field type by changing "Int" in "String" or rename a field, the previously serialized data will not be synchronized with your new class type.
Using the Connection
Using a synchronous connection is pretty easy. Once you obtained your connection, you can simply use classic dot-access to evaluate a path and then use call() to call the method and get the result:
var cnx = haxe.remoting.Connection.jsConnect(); // call the JS method my.package.MyOtherClass.myMethod(0,1) trace(cnx.my.package.MyOtherClass.myMethod.call([0,1]));
When an error occur in a synchronous call, an exception is raised on the caller-side, as if you were calling a local method.
Using the AsyncConnection
The asynchronous connection is a bit more tricky since instead of directly returning the result, it's taking an additional function parameter that will be called when the result is available :
var cnx = haxe.remoting.AsyncConnection.urlConnect("http://localhost:2000"); // setup error handler cnx.onError = function(err) { trace("Error : "+Std.string(err)); }; // result handler var onResult = function(r) { trace("Result : "+Std.string(r)); }; cnx.my.package.MyOtherClass.myMethod.call([0,1],onResult);
When an error occur in a asynchronous call, the exception value is passed to the onError handler.
From Here
Now that you know how to use a Remoting Connection, you can process to the other tutorials that will show examples of different kind of connections.