Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
haXe 2 Beginner's Guide

You're reading from  haXe 2 Beginner's Guide

Product type Book
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Pages 288 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

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!

The haXe serializer


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.

Usefulness

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.

Possibilities

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.

Class instances

When you serialize a class instance, all of its fields are serialized and it also stores...

haXe remoting


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.

Over HTTP

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:

  1. The first one will return the list of animals.

  2. 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.

The Animal class


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:

package zooKeeper;

class Animal
{
   public var name : String;
   public var number : Int;
   
   public function new()
   {
      name = "";
      number = 0;
   }
}

Representing the zoo


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.

   public static function save()
   {
      php.io.File.putContent("zookeeper.data", haxe.Serializer.run(animals));
   }

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:

   public static function init()
   {
      //Read the data from the zookeeper.data file
      try
      {
         animals = haxe.Unserializer.run(php.io.File.getContent("zookeeper.data"));
      } catch(e:Dynamic)
      {
         //If reading from the file doesn't work
         //We can initialize a new list of animals
         animals = new List<Animal>();
      }
   }

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:

package zooKeeper;

class Zoo
{
   public static var animals : List<Animal...

The remoting service


We will now add our remoting service. This remoting service will provide clients with the following two methods:

  1. getList: This method will return the list of animals.

  2. 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

The getList function will simply return the list from the Zoo class:

   public static function getList() : List<Animal>
   {
      returnZoo.animals;
   }

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:

   public static function createAnimal(name : String, quantity : Int)
   {
      //Create a new Animal instance
      var a = new Animal();
      a.name = name...

The client


The client will be pretty easy to implement—it will simply display a menu with a list of actions. There will be only two:

  • Display the list of animals

  • Add an animal

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:

class ZooCommander
{
   public static var cnx : haxe.remoting.Connection;
   
   public static function main()
   {
      //Create the connection
      cnx = haxe.remoting.HttpConnection.urlConnect("http://localhost:8888/ZooKeeper.phpdir/index.php");
   }
}

What just happened?

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:

   public static function createAnimal()
   {
      //Ask the user for the Animal's name
      neko.Lib.println("Enter animal's name");
      var name = neko.io.File.stdin().readLine...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
haXe 2 Beginner's Guide
Published in: Jul 2011 Publisher: ISBN-13: 9781849512565
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}