开发者

Writing objects to socket in AS3/actionscript?

I am trying to pass object to the server through socket connection in actionscript 3. What is the best way to do that?

is serialization better? or should I encode it first and then sent it as string?

please help me to understand this?

开发者_如何学编程

thank you


If your object is implementing IExternalizable and you call registerClassAlias you are safe to use readObject and writeObject. Note however that no constructor parameters are allowed when implementing IExternalizable.

For instance:

package {
  import flash.net.*;
  import flash.utils.*;

  public class Foo implements IExternalizable {
    registerClassAlias("Foo", Foo);

    public var bar: String;

    public function Foo() { // No constructor parameters allowed.
    }

    public function writeExternal(output: IDataOutput): void { output.writeUTF(bar); }
    public function readExternal(input: IDataInput): void { bar = input.readUTF(); }
  }
}

You are then safe to call readObject and writeObject on any IDataOutput or IDataInput which is for instance a Socket, ByteArray or URLStream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜