Not so long ago (a year ago), google suggested that everyone use the Protocol Buffers language.
I read, looked - funny. Great prospects for use.
there are ports on AS3, I tested this:
http://code.google.com/p/protobuf-actionscript3/I want to use as a replacement for AMF.
In a test example, I simulate the transfer of data on a binary socket.
Immediately, there were problems: In the form in which it has been done now, it is normally possible to work only according to the request / response scheme, i.e. when you know which class should come back.
When working with a "nameless" socket in AMF, you can find out what class you came for.
Let's try to implement it in ProtoBuf:
All the source code of the test example in the attachment, here I focus on only my finishing touch)
so the main class: Main.as (read the comments)
AS3 code:
')
- package
- {
- import com.google.protobuf.Message;
- import com.google.protobuf.Person;
- import com.google.protobuf.PhoneNumber;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.net.getClassByAlias;
- import flash.net.registerClassAlias;
- import flash.utils.ByteArray;
- import flash.utils.getDefinitionByName;
- import flash.utils.getQualifiedClassName;
- / **
- * Test class for working with protoBuf - the source code for the parser class has been changed
- * @author k0t0vich
- * /
- public class Main extends Sprite
- {
- public function Main (): void
- {
- if (stage) init ();
- else addEventListener (Event.ADDED_TO_STAGE, init);
- }
- private function init (e: Event = null): void
- {
- removeEventListener (Event.ADDED_TO_STAGE, init);
- // entry point
- // Register Aliases For Classes
- registerClassAlias ("py.Person", Person);
- registerClassAlias ("py.Number", PhoneNumber);
- // Create and encrypt a class .--------------------------
- // write
- var person: Person = new Person ();
- person.alias = "py.Person";
- person.setName ("John Doe");
- person.setId (1234);
- person.setEmail ("jdoe@example.com");
- var number: PhoneNumber = new PhoneNumber ();
- var number2: PhoneNumber = new PhoneNumber ();
- number.setNumber ("555-6789");
- person.addPhone (number);
- number2.setNumber ("123123123-12312");
- person.addPhone (number2);
- trace ("person:" + person);
- // Write it out to a IDataOutput
- var bytes: ByteArray = new ByteArray ();
- person.writeToDataOutput (bytes);
- trace ("writed bytes:" + bytes);
- // ------------------------------------------------ -------
- // simulate the arrival of data from the socket
- onData (bytes);
- }
- // Data from
- private function onData (bytes: ByteArray): void
- {
- bytes.position = 0;
- // read
- var message: Message = new Message ();
- // do not read the entire message, but only the name of the alias
- var alias: String = message.readClassAliasFromDataOutput (bytes);
- var classRef: Class = getClassByAlias (alias) as Class;
- var item: * = new classRef ();
- bytes.position = 0;
- item.readFromDataOutput (bytes);
- // execute a specific class method
- item.changeModel ();
- }
- }
- }
Then, in the onData (Main.as) handler, we first read the name of the alias, create a class using this alias and in it already, read the rest of the data
AS3 code:
- private function onData (bytes: ByteArray): void
- {
- bytes.position = 0;
- // read
- var message: Message = new Message ();
- // do not read the entire message, but only the name of the alias
- var alias: String = message.readClassAliasFromDataOutput (bytes);
- var classRef: Class = getClassByAlias (alias) as Class;
- var item: * = new classRef ();
- bytes.position = 0;
- item.readFromDataOutput (bytes);
- // execute a specific class method
- item.changeModel ();
- }