Introducing MessagePack for ActionScript3
According to the offical website:
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it’s faster and smaller. Small integers are encoded into a single byte, and typical short strings require only one extra byte in addition to the strings themselves.
The format was designed to have a small memory footprint, using as few bytes as possible.
MessagePack for ActionScript3
About one year ago, I worked in a small team which created a panic-board application. The frontend was written in ActionScript3 and the backend in Node.js.
We decided to use MessagePack (instead of XML, which was the original idea) to create the communication protocol between the client and the server, because our client was wanting a lightweight application to run within his intranet.
At that time, the implementations of MsgPack for AS3 were too simple, and frankly, quite abandoned.
Then I decided to write my own version.
The usage is pretty simple (similar to AS3 JSON parsers). Your code will work with two methods: read and write - which respectively, decodes and encodes data.
Example of basic usage:
// create messagepack object var msgpack:MsgPack = new MsgPack(); // encode an array var bytes:ByteArray = msgpack.write([1, 2, 3, 4, 5]); // rewind the buffer bytes.position = 0; // print the decoded object trace(msgpack.read(bytes));
Moreover, as3-msgpack supports stream reading, I mean, you can decode an object using a step-by-step process (useful to work with sockets, for example, where your code doesn’t receives all the bytes at the same time).
How to use
- Client/server communication:
Since MessagePack is independent from language/platform it’s a good choice when you need a small and fast packaging model to encapsulate your protocol.
- Making sockets more friendly:
MsgPack can be easily connected with flash native Socket objects (because they implements the interfaces IDataInput/IDataOutput), thus you can decode the data straight from a binary stream.
- Persistent storage:
In webgames, for instance, you might use MessagePack to save the game data in ShardObjects - or even in a Adobe AIR application, you can use as3-msgpack to save the state of your application in a binary file.
- Check the repository of as3-msgpack and download the latest tag
- See the MessagePack offical page and the MessagePack specification document
blog comments powered by Disqus