Working with external data in Flash - XMLSocket Class
(Page 6 of 6 )
XMLSocket class in ActionScript lets you open a continuous connection with a server. A socket connection lets the server publish, or push, information to the client as soon as that information is available. The data is sent over the socket connection as one string and should be formatted as XML. You can use the XML class to structure the data.
The methods of XMLSocket Class create and use TCP/IP socket connections to send and load information as XML.
You can use the connect()and send()methods of the XMLSocket class to transfer XML to and from a server over a socket connection. The connect()method establishes a socket connection with a Web server port. The send()method passes an XML object to the server specified in the socket connection.
Now let us see an example that shows how to use the XMLSocket Class.
Example:
// Create XMLSocket object
var sample_socket:XMLSocket = new XMLSocket();
// Connects to localhost server with port number 1024
sample_socket.connect("localhost", 1024);
// Displays connection status information
sample_socket.onConnect = function(status) {
if (status) {
conn_txt.text = "connection successful";
} else {
conn_txt.text = "no connection made";
}
};
// Function that creates data to send it to the server using
send() method
function sendData() {
var data:XML = new XML();
var send = data.createElement("user");
send.attributes.id = "asp123";
data.appendChild(send);
sample_socket.send(data);
}
// onData event handler function to track the data returned from
socket connection and perform actions based on that data
sample_socket.onData = function(msg:String):Void {
trace(msg);
};
Note:The XMLSocket.connect()method can connect only to TCP port numbers greater than or equal to 1024. Port numbers below 1024 are often used by system services such as FTP, Telnet, and HTTP, so XMLSocket objects are barred from these ports for security reasons.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |