XMLSocket class



The XMLSocket class implements client sockets that let Flash Media Interactive Server communicate with a server identified by an IP address or domain name. The XMLSocket class is useful for client-server applications that require low latency, such as real-time chat systems. A traditional HTTP-based chat solution polls the server frequently 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 send incoming messages immediately, without a request from the client.

Note: You can also use the XMLSocket class to create an XMLStreams object. See XMLSocket constructor and XMLStreams class.

To use the XMLSocket class, the server computer must run a daemon that understands the protocol used by this class. The protocol has the following characteristics:

  • 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 restriction applies to how and where an XMLSocket object can connect to the server:

  • 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 less than 1024 are often used by system services such as FTP, Telnet, and HTTP, which prohibits XMLSocket objects from these ports for security reasons. The port number restriction limits the possibility that these resources can be inappropriately accessed and abused.

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

Availability

Flash Media Server 2

Property summary

Property

Description

XMLSocket.maxUnprocessedChars

The number of characters the connection can receive from the XML server without receiving an end tag or the XMLSocket connection closes.

Method summary

Method

Description

XMLSocket.close()

Closes the connection specified by the XMLSocket object.

XMLSocket.connect()

Establishes a connection to the specified Internet host by using the specified TCP port (must be 1024 or higher), and returns true or false, depending on whether a connection is successfully established.

XMLSocket.send()

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.

Event handler summary

Event handler

Description

XMLSocket.onClose()

Invoked when an open connection is closed by the server.

XMLSocket.onConnect()

Invoked by Flash Media Interactive Server when a connection request initiated through XMLSocket.connect() succeeds or fails.

XMLSocket.onData()

Invoked when a message has been downloaded from the server, terminated by a zero (0) byte.

XMLSocket.onXML()

Invoked when the specified XML object containing an XML document arrives through an open XMLSocket connection.

XMLSocket constructor

new XMLSocket(streamOrFlash)

Creates a new XMLSocket object ("flash") or a new XMLStreams object ("stream"). The XMLSocket and XMLStreams objects are not initially connected to any server. You must call XMLSocket.connect() to connect the object to a server.

For more information about the XMLStreams class, see XMLStreams class.

Availability

Flash Communication Server 1.5

Parameters

streamOrFlash
A string indicating whether this object is an XMLSocket object or an XMLStreams object. This parameter can have one of the following two values: "flash" or "stream".

Returns

A reference to an XMLSocket object or an XMLStreams object.

Example

The following example creates an XMLSocket object:

var socket = new XMLSocket("flash");

The following example creates an XMLStreams object:

var stream = new XMLSocket("stream");

XMLSocket.close()

myXMLSocket.close()

Closes the connection specified by the XMLSocket object.

Availability

Flash Media Server 2

Example

The following simple example creates an XMLSocket object, attempts to connect to the server, and then closes the connection:

var socket = new XMLSocket(); 
socket.connect(null, 2000); 
socket.close();

XMLSocket.connect()

myXMLSocket.connect(host, port)

Establishes a connection to the specified Internet host by using the specified TCP port (must be 1024 or higher), and returns true or false, depending on whether a connection is successfully established. If you don’t know the port number of the Internet host computer, contact your network administrator.

If you specify null for the host parameter, the local host is contacted.

The Server-Side ActionScript XMLSocket.connect() method can connect to computers that are not in the same domain as the SWF file.

If XMLSocket.connect() returns a value of true, the initial stage of the connection process is successful. Later, the XMLSocket.onConnect() handler is invoked to determine whether the final connection succeeded or failed. If XMLSocket.connect() returns false, a connection could not be established.

Availability

Flash Media Server 2

Parameters

host
A string; a fully qualified DNS domain name or an IP address. Specify null to connect to the local host. Do not enclose IPv6 addresses in square brackets.

port
A number; the TCP port number on the host used to establish a connection. The port number must be 1024 or higher.

Returns

A boolean value; true if successful, otherwise, false.

Example

The following example uses XMLSocket.connect() to connect to the local host:

var socket = new XMLSocket() 
socket.onConnect = function (success) { 
    if (success) { 
        trace ("Connection succeeded!") 
    } else { 
        trace ("Connection failed!") 
    } 
}; 
if (!socket.connect(null, 2000)) { 
    trace ("Connection failed!") 
}
Note: Server-side trace() statements are output to the application log file and to the Live Log panel in the Administration Console.

XMLSocket.maxUnprocessedChars

myXMLSocket.maxUnprocessedChars

The number of characters the connection can receive from the XML server without receiving an end tag or the XMLSocket connection closes. The value of XMLSocket.maxUnprocessedChars can be -1 or any value greater than 0. The value -1 means that an unlimited amount of data can be processed. However, the value of maxUnprocessedChars cannot exceed the value specified in the Application.xml file. The default value in the Application.xml file is 4096 bytes.

Setting this property in a server-side script overrides the value of the MaxUnprocessedChars element in the Application.xml file for each XMLSocket object. If the property is not set in a server-side script, the server uses the value set in the MaxUnprocessedChars element of the Application.xml file.

Availability

Flash Media Interactive Server

XMLSocket.onClose()

myXMLSocket.onClose = function() {}

Invoked when an open connection is closed by the server. The default implementation of this method performs no actions. To override the default implementation, you must assign a function containing custom actions.

Availability

Flash Media Server 2

Example

The following example executes a trace() statement if an open connection is closed by the server:

var socket = new XMLSocket(); 
socket.connect(null, 2000); 
socket.onClose = function () { 
    trace("Connection to server lost."); 
}
Note: Server-side trace() statements are output to the application log file and to the Live Log panel in the Administration Console.

XMLSocket.onConnect()

myXMLSocket.onConnect = function(success) {}

Invoked by Flash Media Interactive Server when a connection request initiated through XMLSocket.connect() succeeds or fails. If the connection succeeded, the success parameter is true; otherwise, false.

The default implementation of this method performs no actions. To override the default implementation, you must assign a function containing custom actions.

Availability

Flash Media Server 2

Parameters

success
A boolean value indicating whether a socket connection is successfully established (true or false).

Example

The following example defines a function for the onConnect() handler:

socket = new XMLSocket(); 
socket.onConnect = myOnConnect; 
 
socket.connect(null,2000); 
 
function myOnConnect(success) { 
    if (success) { 
        trace("Connection success") 
    } else { 
        trace("Connection failed") 
    } 
}

XMLSocket.onData()

myXMLSocket.onData = function(src) {}

Invoked when a message has been downloaded from the server, terminated by a zero (0) byte. You can override XMLSocket.onData() to intercept the data sent by the server without parsing it as XML. This is useful if you’re transmitting arbitrarily formatted data packets and you would prefer to manipulate the data directly when it arrives, rather than have Flash Media Interactive Server parse the data as XML.

By default, the XMLSocket.onData() method invokes the XMLSocket.onXML() method. If you override XMLSocket.onData() with custom behavior, XMLSocket.onXML() is not called unless you call it in your implementation of XMLSocket.onData().

Availability

Flash Media Server 2

Parameters

src
A string containing the data sent by the server.

Example

In the following example, the src parameter is a string containing XML text downloaded from the server. The zero-byte (0) terminator is not included in the string.

XMLSocket.prototype.onData = function (src) { 
    this.onXML(new XML(src)); 
}

XMLSocket.onXML()

myXMLSocket.onXML = function(object) {}

Invoked when the specified XML object containing an XML document arrives through an open XMLSocket connection. An XMLSocket connection can be used to transfer an unlimited number of XML documents between the client and the server. Each document is terminated with a zero (0) byte. When Flash Media Interactive Server receives the zero byte, it parses all of the XML received since the previous zero byte or, if this is the first message received, since the connection was established. Each batch of parsed XML is treated as a single XML document and passed to the onXML() handler.

The default implementation of this method performs no actions. To override the default implementation, you must assign a function containing actions that you define.

Availability

Flash Media Server 2

Parameters

object
An XML object that contains a parsed XML document received from a server.

Example

The following function overrides the default implementation of the onXML() method in a simple chat application. The myOnXML() function instructs the chat application to recognize a single XML element, MESSAGE, in the following format:

<MESSAGE USER="John" TEXT="Hello, my name is John!" />.  
var socket = new XMLSocket();

The following displayMessage() function is assumed to be a user-defined function that shows the message that the user receives:

socket.onXML = function (doc) { 
    var e = doc.firstChild; 
    if (e != null && e.nodeName == "MESSAGE") { 
        displayMessage(e.attributes.user, e.attributes.text); 
    } 
};

XMLSocket.send()

myXMLSocket.send(object)

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. If object is an XML object, the string is the XML textual representation of the XML object.

If the myXMLSocket object is not connected to the server (by using XMLSocket.connect()), the XMLSocket.send() operation fails.

Availability

Flash Media Server 2

Parameters

object
An XML object or other data to transmit to the server.

Returns

A boolean value; true if the server is able to get the socket and the socket state is connected; otherwise, false. A true value does not mean that the data has been transmitted successfully. The send() method is asynchronous; it returns a value immediately, but the data may be transmitted later.

Example

The following example shows how you can specify a user name and password to send the XML object my_xml to the server:

var myXMLSocket = new XMLSocket(); 
var my_xml = new XML(); 
var myLogin = my_xml.createElement("login"); 
myLogin.attributes.username = usernameTextField; 
myLogin.attributes.password = passwordTextField; 
my_xml.appendChild(myLogin); 
myXMLSocket.send(my_xml);