Chapter 7. Communication Between haXe Programs
Using haXe remoting.
haXe is a language perfectly suited for web-applications. When one says web applications, it generally means connected applications. Fortunately, haXe has a system to help applications communicate: haXe remoting.
In this chapter, we will talk about haXe remoting and how it can be used to make our haXe applications communicate. As you will see, it is in fact pretty easy to use.
In this chapter, we will:
Learn about the haXe serializer
Use Neko and PHP applications as clients and servers
Create a JS client
Create a Flash client
Communicate between Flash and JS
Although this is all pretty easy to do and understand, it is also very useful.
So, if you are ready, let's move on!
haXe has its own serializing system. This serializing system is the one used by haXe remoting therefore, before continuing with haXe remoting, it is important to understand how the haXe serializer works.
The haXe serializer is used by the haXe remoting system, but you can also use it on your own.
Doing so will allow you to get a string representation of almost any value. This way, you can, for example, store it on disk, in a database, or send it over any network connection.
The haXe serializer allows you to serialize almost any value, but still there are some things that you should know about how some values are serialized.
Basically, you should know that you can serialize:
Ints
Floats
Bools
Strings
nulls
haXe.io.Bytes
Arrays
List
Hash
Class instances
Enum instances
Anonymous objects
Exceptions
Now, let's see what you need to know about how these are serialized.
When you serialize a class instance, all of its fields are serialized and it also stores...
The haXe remoting system can be used with all haXe's targets, but the functionalities which you will be able to use won't be the same as they vary/alter according to the type of platform used.
In addition, haXe remoting can be used over several protocols or transport layers. Again, the ability to use some of them will depend on the platform you are targeting.
Supported protocols and layers
As we discussed earlier, haXe remoting can be used on top of several protocols and transport layers. Functionalities and supported protocols depend on the target platform.
Here are the main supported protocols, by which class they are implemented, and an example of what they can be used for.
It is possible to use haXe remoting over HTTP. This allows a client to query a server over the HTTP transport and has the advantage of being possible from most platforms.
It is possible to do so in both asynchronous and synchronous modes. In the first case, one will use the haxe.remoting.HttpAsyncConnection...
Time for action – Writing a zoo management tool
To illustrate what we have learned in this chapter, we are going to write a very simple tool based on zoo management.
This tool will have a server on which the list of animals will be stored and that will execute clients' queries.
Therefore, the server will basically offer the following two methods:
The first one will return the list of animals.
The second one will allow creating and adding an animal in the list.
An animal will simply consist in a name and a number of this animal.
On the client side, we will create a neko application in command line that will simply offer the same two functionalities, but exposed for the user.
In order to store information about animals, we will create a class named Animal
. This class will be very simple, but it will have to be shared by both the client and the server. Indeed, the server will serialize several instances of this class, which means that the client will need this class to be able to unserialize it.
So, let's see how this class looks:
On the server side, we will create a class named Zoo
, which will hold the list of animals and will provide two methods to load and save this list onto disk.
Time for action – Saving the list
In order to save the list, we will serialize it and save the result in a file. This indeed is certainly not the best way to do things but in our case, it will be enough.
Very simple, isn't it? We are simply serializing the animals list and dumping the result directly in the zookeeper.data
file.
Time for action – Loading the list
Loading the list is a bit more complicated; it is possible that the file does not exist and in this case, we just want to start with a new list.
Let's see how we can handle that:
Ok, it is not so complicated in fact. We just had to wrap our reading with a try
block. It could have been handled a bit better—we could have just had a look to see if the file existed. If it existed but the unserializing had failed, then it would mean that we have a true problem with the stored data.
So, here is our complete Zoo
class:
We will now add our remoting service. This remoting service will provide clients with the following two methods:
getList
: This method will return the list of animals.
addAnimal
: This method will take a name and a number to create an animal and add it to the list of animals in the zoo.
In order to provide this service, we will simply create a class named Service
and implement the preceding two functions in it.
The getList
function will simply return the list from the Zoo
class:
Ok, this one was pretty simple in our case.
The createAnimal function
The addAnimal
method will create an instance of animal setting its name and number fields with the parameters it takes and adding it to the list of animals.
Here it is:
The client will be pretty easy to implement—it will simply display a menu with a list of actions. There will be only two:
As you have certainly guessed, for each action it will simply call the remoting server and display the result or take information from the user and pass it onto the server.
Time for action – Initializing the client connection
The first thing we need to do is to initialize our connection. We will store it in a static variable, as follows:
We have simply created a connection over HTTP to our server by passing its address as the parameter.
Now, let's create our two functions.
The createAnimal function
We will create a createAnimal
function, which will be called by the main menu when the user chooses to add an animal to the list.
It will first ask for the required information and then make the request to the server:
In this chapter, we learned how you can make your haXe programs communicate together. Specifically, we covered how to communicate between Flash and JS, how to communicate across the network, and how the haXe serializer works.
In the next chapter, we will learn how to access databases.