Search code examples
client-serverhaxeopenfl

How to send a list of objects in Haxe between client and server?


I am trying to write an online message board in Haxe (OpenFL). There are lots of server/client examples online. But I am new to this area and I do not understand any of them. What is the easiest way to send a list of objects between server and client? Could you guys give an example?


Solution

  • EDIT: I'm not still sure if the user's problem is really about sending "a list of objects"; see comment to the question...


    The easiest way is to use Haxe Serialization, either with Haxe Remoting or with your own protocol on top of TCP/UDP. The choice of protocol depends whether you already have something built and whether you will be calling functions or simply getting/posting data.

    In either case, haxe.Serializer/Unserializer will give you a format to transmit most (if not all) Haxe objects from client to server with minimal code.

    See the following minimal example (from the manual) on how to use the serialization APIs. The format is string based and specified.

    import haxe.Serializer;
    import haxe.Unserializer;
    
    class Main {
      static function main() {
        var serializer = new Serializer();
        serializer.serialize("foo");
        serializer.serialize(12);
        var s = serializer.toString();
        trace(s); // y3:fooi12
    
        var unserializer = new Unserializer(s);
        trace(unserializer.unserialize()); // foo
        trace(unserializer.unserialize()); // 12
      }
    }
    

    Finally, you could also use other serialization formats like JSON (with haxe.Json.stringify/parse) or XML, but they wouldn't be so convenient if you're dealing with enums, class instances or other data not fully supported by these formats.