Flash Lite 2.x and 3.0 ActionScript Language Reference

XMLSocket

Object
    |
    +-XMLSocket

public class XMLSocket
extends Object

The XMLSocket class implements client sockets that let the device running the Flash Lite player communicate with a server computer that an IP address or domain name identifies. The XMLSocket class is useful for client-server applications that require low latency, such as real-time chat systems. A traditional HTTP-based chat system frequently polls the server and downloads new messages by using an HTTP request. In contrast, an XMLSocket chat solution maintains an open connection to the server, which lets the server immediately send incoming messages without a request from the client.

To use the XMLSocket class, the server computer must run a daemon process that understands the protocol that the XMLSocket class uses. The following list describes the protocol:

  • XML messages are sent over a full-duplex TCP/IP stream socket connection.
  • Each XML message is a complete XML document, terminated by a zero (0) byte.
  • An unlimited number of XML messages can be sent and received over a single XMLSocket connection.

The following restrictions apply to how and where an XMLSocket object can connect to the server:

  • To connect an XMLSocket to a port lower than 1024, you must first load a policy file with the System.security.loadPolicyFile() method, even when your application connects to its own exact domain.
  • The XMLSocket.connect() method can connect only to computers in the same domain where the SWF file resides. This restriction does not apply to SWF files running on a local disk. (This restriction is identical to the security rules for the loadVariables() function, and the XML.sendAndLoad() and XML.load() methods.) To connect to a server daemon running in a domain other than the one where the SWF file resides, you can create a security policy file on the server that allows access from specific domains.

Setting up a server to communicate with the XMLSocket object can be challenging. If your application does not require real-time interactivity, use the loadVariables() function, or Flash HTTP-based XML server connectivity methods (XML.load(), XML.sendAndLoad(), XML.send()), instead of the XMLSocket class.

To use the methods of the XMLSocket class, you must first use the constructor, XMLSocket(), to create an XMLSocket object.

See also

loadPolicyFile (security.loadPolicyFile method)

Property summary

Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)

Event summary

Event

Description

onClose = function() {}

Invoked only when the server closes an open connection.

onConnect = function(success:Boolean) {}

An asynchronous callback that the Flash Lite player invokes when a connection request initiated through XMLSocket.connect() succeeds or fails.

onData = function(src:String) {}

Invoked when a message is downloaded from the server and terminated by a zero (0) byte.

onXML = function(src:XML) {}

Invoked by the Flash Lite player when the specified XML object containing an XML document arrives over an open XMLSocket connection.

Constructor summary

Signature

Description

XMLSocket()

Creates a new XMLSocket object.

Method summary

Modifiers

Signature

Description

 

close() : Void

Closes the connection that the XMLSocket object specifies.

 

connect(url:String, port:Number) : Boolean

Establishes a connection to the specified Internet host by using the specified TCP port and returns true or false, depending on whether a connection is successfully initiated.

 

send(data:Object) : Void

Converts the XML object or data specified in the object parameter to a string and transmits it to the server, followed by a zero (0) byte.

Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)