[Tutorial] AS 3.0 socket communication, the whole basis of comparison compare, do not go around the

Transfer from http://520pig.blogbus.com/logs/32821429.html

Socket socket connection allows Flash player by specifying the port communicate with the server, but the socket after the data transfer communication is still not close to artificially close. We can use the class to create XML data format flash.net.XMLSocket the socket connection, use the class to create a binary data format flash.net.Socket socket connection, compared with xml data format, binary link is more low-level, but it can connect to almost all socket server-side program. as3.0 socket provided in the link are asynchronous, meaning that you must increase the event listener later in the transfer complete data available

To create a socket communication, we must first establish socket connection, we can use Socket.connect () or XMLSocket.connect () method to establish a connection and connect the event monitor to determine whether the established connection. Connect only two parameters, IP, and port.

Note conditions:

swf and the host must be in the same domain;
Swf on the network can not connect to local server;
Swf can not access any local network resources;
To allow cross-domain access or connection ports lower than 1024, the need for cross-domain policy file.

If you want to visit each other in different domains of the flash can flash.system.Security.loadPolicyFile () to read:
Security.loadPolicyFile ("cross-domain");
cross-domain policy file example:
<? Xml version = "1.0"?>
<! DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domainpolicy.
dtd ">
<allow-access-from domain="*" to-ports="80,110" />
</ Cross-domain-policy>

End connections can be established after the write server, by calling the write method, you can write to write data to the buffer pool, then call the flush () method can be sent to the data, this is for the binary socket, if xml call send () method. as3.0 can define different parameters for different write function: is writeBoolean (), writeByte (), writeBytes (),
writeDouble (), writeFloat (), writeInt (), writeMultiByte (), writeObject (), writeShort (), write-
UnsignedInt (), writeUTF (), and writeUTFBytes (). According to the function name can be aware of the need incoming parameters, here on socket.writeBytes (byteArray, 0, byteArray.length); to explain, because it can pass a data, and develop an array of start and end position for writing. , WriteUTFBytes (): write byte string, writeUTF: numeric string. writeMultiByte () to write to the specified encoding string, socket.writeMultiByte ("example", "unicode");

Once the data would have to send data, so we can ProgressEvent.SOCKET_DATA event handler to read the data, using readBoolean (): Boolean, readByte (): int, readDouble (): Number, readFloat (): Number, readInt (): int, readObject ():*, readShort (): int, readUnsignedByte (): uint, readUnsignedInt (): uint, readUnsignedShort (): uint, readUTF (): String, readBytes () no return parameters, readUTFBytes ( ) return utf8 data. Socket object receives the data is ASCII-encoded text, we can use readUTFBytes () method to reconstruct the string
readUTFBytes () method needs to know how many bytes need to convert, with bytesAvailable property specifies the number of bytes:
var string: String = socket.readUTFBytes (socket.bytesAvailable);

Mentioned above, the socket is in binary transfer mode, if the use xml approach, then the trigger event types, it is DataEvent.DATA trigger event, the event's data property contains the data passed to, if we are to read xml data must first be converted to string in xml format, and then read.

分类:Flash 时间:2010-08-27 人气:179
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.629 (s). 10 q(s)