U-X > XMLSocket (object) |  |

XMLSocket (object)

XMLsocket , XMLsocket object, built-in object

The XMLSocket object implements client sockets that allow the computer running the Flash Player to communicate with a server computer identified by an IP address or domain name.

Using the XMLSocket object

To use the XMLSocket object, the server computer must run a daemon that understands the protocol used by the XMLSocket object. The protocol is as follows:

| 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 byte.
| An unlimited number of XML messages can be sent and received over a single XMLSocket connection.

The XMLSocket object is useful for client-server applications that require low latency, such as real-time chat systems. A traditional HTTP-based chat solution frequently polls the server and downloads new messages using an HTTP request. In contrast, an XMLSocket chat solution maintains an open connection to the server, which allows the server to immediately send incoming messages without a request from the client.

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` action, or Flash HTTP-based XML server connectivity (`XML.load`, `XML.sendAndLoad`, `XML.send`), instead of the XMLSocket object.

To use the methods of the XMLSocket object, you must first use the constructor, `new XMLSocket`, to create a new XMLSocket object.

XMLSocket and security

Because the XMLSocket object establishes and maintains an open connection to the server, the following restrictions have been placed on the XMLSocket object for security reasons:

| The `XMLSocket.connect` method can connect only to TCP port numbers greater than or equal to 1024. One consequence of this restriction is that the server daemons that communicate with the XMLSocket object must also be assigned to port numbers greater than or equal to 1024. Port numbers below 1024 are often used by system services such as FTP, Telnet, and HTTP, thus the XMLSocket object is barred from these ports for security reasons. The port number restriction limits the possibility that these resources will be inappropriately accessed and abused.
| The `XMLSocket.connect` method can connect only to computers in the same subdomain where the SWF file (movie) resides. This restriction does not apply to movies running off a local disk. (This restriction is identical to the security rules for `loadVariables`, `XML.sendAndLoad`, and `XML.load`.)

Method summary for the XMLSocket object

Method | Description
--- | ---
`XMLSocket.close` | Closes an open socket connection.
`XMLSocket.connect` | Establishes a connection to the specified server.
`XMLSocket.send` | Sends an XML object to the server.

Event handler summary for the XMLSocket object

Method | Description
--- | ---
`XMLSocket.onClose` | A callback function that is invoked when an XMLSocket connection is closed.
`XMLSocket.onConnect` | A callback function that is invoked when an XMLSocket connection is established.
`XMLSocket.onData` | A callback function that is invoked when an XML message has been downloaded from the server.
`XMLSocket.onXML` | A callback function that is invoked when an XML object arrives from the server.

Constructor for the XMLSocket object

new XMLsocket, new, constructor

Availability

Flash Player 5.

Usage

    new XMLSocket()

Parameters

None.

Returns

Nothing.

Description

Constructor; creates a new XMLSocket object. The XMLSocket object is not initially connected to any server. You must call the `XMLSocket.connect` method to connect the object to a server.

Example

    myXMLSocket = new XMLSocket();

See also

`XMLSocket.connect`