开发者

Writing objects to socket in AS3/actionscript?

开发者 https://www.devze.com 2022-12-19 03:20 出处:网络
I am trying to pass object to the server through socket connection in actionscript 3. What is the best way to do that?

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

精彩评论

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

关注公众号