Beetle uses amf3 protocol and Flash Communication in TCP Communication

Source: Internet
Author: User

Flash is the most popular rich client performance software on existing web pages. Therefore, it is necessary for components to support Flash Object Protocol Communication. because the components can be compatible with different protocols through the extension protocol analyzer, you can use fluorinefx to implement TCP Communication Based on amf3 and flash. if you know the extensions of protobuf, it is very easy to extend amf3.

Define protocol format

The Protocol format must also be clarified before interaction. Only in this way can the Protocol be conveniently implemented.CodeWrite implementation.

The Protocol compares the total message length and content respectively. The content is amf3 protocol data.

Implement a specific analyzer and message Adapter

After the protocol is developed, the analyzer can be implemented by describing the size in the first 4 bytes. Therefore, the analyzer can download and rewrite related methods from the headsizeofpackage base class. The complete implementation code is as follows:

Public class amf3package: headsizeofpackage {public amf3package () {} public amf3package (tcpchannel channel): Base (Channel) {} public override object readcast (Object message) {return (messageadapter) message ). message;} public override object writecast (Object message) {messageadapter adapter = new messageadapter (); adapter. message = message; return adapter;} protected override iMessage readmessagebytype (bufferreader, out object typetag) {typetag = "messageadapter"; return New messageadapter ();} protected override void writemessagetype (iMessage MSG, bufferwriter writer) {} public override void messagewrite (iMessage MSG, bufferwriter writer) {MSG. save (writer);} public override iMessage messageread (bufferreader reader) {iMessage MSG = NULL; object typetag; MSG = readmessagebytype (reader, out typetag); If (MSG = NULL) throw nettcpexception. typenotfound (typetag. tostring (); try {MSG. load (Reader);} catch (exception e) {nettcpexception err = nettcpexception. objectloaderror (typetag. tostring (), e); throw err;} return MSG;} class messageadapter: iMessage {public object message {Get; set;} public void load (bufferreader reader) {bytearraysegment segment = bytesegment; int COUNT = reader. readint32 (); reader. read (count-4, segment); Using (system. io. memorystream steram = new system. io. memorystream (segment. array, segment. offset, segment. count) {fluorinefx. amf3.bytearray BA = new fluorinefx. amf3.bytearray (steram); BA. objectencoding = fluorinefx. objectencoding. amf3; message = BA. readobject () ;}} public void save (bufferwriter writer) {bytearraysegment segment = bytesegment; using (system. io. memorystream steram = new system. io. memorystream (segment. array) {fluorinefx. amf3.bytearray BA = new fluorinefx. amf3.bytearray (steram); BA. objectencoding = fluorinefx. objectencoding. amf3; BA. writeobject (Message); segment. setinfo (0, (INT) steram. position);} writer. write (segment. count + 4); writer. write (segment. array, segment. offset, segment. count);} [threadstatic] Private Static bytearraysegment mbytesegment; public static bytearraysegment bytesegment {get {If (mbytesegment = NULL) mbytesegment = new bytearraysegment (tcputils. datapacketmaxlength); Return mbytesegment ;}}}}
Flash protocol analyzer
Package {import flash.net. socket; import flash. utils. bytearray; import flash. utils. endian; import MX. controls. alert; import MX. graphics. shaderclasses. exclusionshader; public class headsizeofpackage {public function headsizeofpackage () {} private var mmessagereceive: function; // message receiving callback function public function get messagereceive (): function {return response ;} public Function set messagereceive (value: function): void {Mmessagereceive = value;} private var mreader: bytearray = new bytearray (); Private var mwriter: bytearray = new bytearray (); Private var msize: Int = 0; // import the data received by the current socket Public Function Import (socket: socket): void {socket. endian = endian. little_endian; while (socket. bytesavailable> 0) {If (msize = 0) {msize = socket. readint ()-4; mreader. clear ();} If (socket. bytesavailable> = msize) {socket. readbytes (mreader, mreader. len Callback, msize); var MSG: Object = mreader. readobject (); If (messagereceive! = NULL) messagereceive (MSG); msize = 0;} else {msize = mSize-socket.bytesAvailable; socket. readbytes (mreader, mreader. length, socket. bytesavailable) ;}}// protocol data encapsulated in payload public function send (message: object, socket: socket): void {socket. endian = endian. little_endian; mwriter. clear (); mwriter. writeobject (Message); socket. writeint (mwriter. length + 4); socket. writebytes (mwriter, 0, mwriter. length); socket. flush ();}}}
Running Effect

As the implementation of the server is the same as the previous one, there is no change to the general parameters, so the specific implementation is no longer described. The following is the operation.

Download Code: Code

Summary

By using fluorinefx, components can communicate with Flash files. fluorinefx has good support for amf3, but it seems that the components have not been updated for a long time, looking at his source code, I found that there is still much room for optimization on its performance. if a friend using fluorinefx does not feel good about his performance, you can use the memory analysis tool to analyze the causes of slow performance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.