window.runtime propertywindow.runtime.flash.net.XMLSocket
InheritanceXMLSocket Inheritance EventDispatcher Inheritance Object
Runtime Versions: AIR 1.0, Flash Player 9

The XMLSocket class implements client sockets that let the AIR application communicate with a server computer 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 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 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 that understands the protocol used by the XMLSocket class. The protocol is described in the following list:

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, so XMLSocket objects are barred from these ports for security reasons. The port number restriction limits the possibility that these resources will be inappropriately accessed and abused.

Setting up a server to communicate with the XMLSocket object can be challenging. If your application does not require real-time interactivity, use the URLLoader class instead of the XMLSocket class. To use the methods of the XMLSocket class, you must first use the constructor, new XMLSocket, to create an XMLSocket object.

When you use this class, consider the following security model:

However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information, see the following:

See also

URLLoader.load()
URLLoader


Properties
 PropertyDefined By
  connected : Boolean
[read-only] Indicates whether this XMLSocket object is currently connected.
XMLSocket
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined By
  
XMLSocket(host:String = null, port:int = 0)
Creates a new XMLSocket object.
XMLSocket
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
  
Closes the connection specified by the XMLSocket object.
XMLSocket
  
connect(host:String, port:int):void
Establishes a connection to the specified Internet host using the specified TCP port.
XMLSocket
 Inherited
dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow.
EventDispatcher
 Inherited
hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
hasOwnProperty(name:String):Boolean
Indicates whether an object has a specified property defined.
Object
 Inherited
isPrototypeOf(theClass:Object):Boolean
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
propertyIsEnumerable(name:String):Boolean
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
send(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.
XMLSocket
 Inherited
setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
toString():String
Returns the string representation of the specified object.
Object
 Inherited
valueOf():Object
Returns the primitive value of the specified object.
Object
 Inherited
willTrigger(type:String):Boolean
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 Event Summary Defined By
 InheritedDispatched when Flash Player or an AIR application gains operating system focus and becomes active.EventDispatcher
  Dispatched when the server closes the socket connection.XMLSocket
  Dispatched after a successful call to the XMLSocket.connect() method.XMLSocket
  Dispatched after raw data is sent or received.XMLSocket
 InheritedDispatched when Flash Player or an AIR application loses operating system focus and is becoming inactive.EventDispatcher
  Dispatched when an input/output error occurs that causes a send or receive operation to fail.XMLSocket
  Dispatched if a call to the XMLSocket.connect() method attempts to connect either to a server outside the caller's security sandbox or to a port lower than 1024.XMLSocket
Property Detail
connectedproperty
connected:Boolean  [read-only]
Runtime Versions: AIR 1.0, Flash Player 9

Indicates whether this XMLSocket object is currently connected. You can also check whether the connection succeeded by registering for the connect event and ioError event.

See also

Constructor Detail
XMLSocket()Constructor
function XMLSocket(host:String = null, port:int = 0)
Runtime Versions: AIR 1.0, Flash Player 9

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.

Parameters
host:String (default = null) — A fully qualified DNS domain name or an IP address in the form aaa.bbb.ccc.ddd.
 
port:int (default = 0) — The TCP port number on the host used to establish a connection. The port number must be 1024 or greater, unless a policy file is being used.

See also

Method Detail
close()method
function close():void
Runtime Versions: AIR 1.0, Flash Player 9

Closes the connection specified by the XMLSocket object. The close event is dispatched only when the server closes the connection; it is not dispatched when you call the close() method.

See also

connect()method 
function connect(host:String, port:int):void
Runtime Versions: AIR 1.0, Flash Player 9

Establishes a connection to the specified Internet host using the specified TCP port. By default you can only connect to port 1024 or higher, unless you are using a policy file.

If you specify null for the host parameter, the host contacted is the one where the file calling XMLSocket.connect() resides. For example, if the calling file was downloaded from www.adobe.com, specifying null for the host parameter is the same as entering the IP address for www.adobe.com.

For more information, see the following:

Parameters

host:String — A fully qualified DNS domain name or an IP address in the form aaa.bbb.ccc.ddd.
 
port:int — The TCP port number on the host used to establish a connection. The port number must be 1024 or greater, unless a policy file is being used.


Events
securityError:SecurityErrorEvent — A connect operation attempted to connect to a server outside the caller's security sandbox, and/or to a port lower than 1024. Either problem may be worked around using an XMLSocket policy file on the server.
 
data:DataEvent — Dispatched when raw data has been received.
 
connect:Event — Dispatched when network connection has been established.

Throws
SecurityError — Local untrusted files may not communicate with the Internet. This may be worked around by reclassifying the file as local-with-networking or trusted.
 
SecurityError — You may not specify a socket port higher than 65535.

See also

send()method 
function send(object:*):void
Runtime Versions: AIR 1.0, Flash Player 9

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. The send operation is asynchronous; it returns immediately, but the data may be transmitted at a later time. The XMLSocket.send() method does not return a value indicating whether the data was successfully transmitted.

If you do not connect the XMLSocket object to the server using XMLSocket.connect()), the XMLSocket.send() operation fails.

Parameters

object:* — An XML object or other data to transmit to the server.


Throws
IOError — The XMLSocket object is not connected to the server.

See also

Event Detail
close Event
Event Object Type: flash.events.Event
property Event.type = flash.events.Event.CLOSE
Runtime Versions: AIR 1.0, Flash Player 9

Dispatched when the server closes the socket connection. The close event is dispatched only when the server closes the connection; it is not dispatched when you call the XMLSocket.close() method.

The Event.CLOSE constant defines the value of the type property of a close event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe object whose connection has been closed.
connect Event  
Event Object Type: flash.events.Event
property Event.type = flash.events.Event.CONNECT
Runtime Versions: AIR 1.0, Flash Player 9

Dispatched after a successful call to the XMLSocket.connect() method.

The Event.CONNECT constant defines the value of the type property of a connect event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe Socket or XMLSocket object that has established a network connection.
data Event  
Event Object Type: flash.events.DataEvent
property DataEvent.type = flash.events.DataEvent.DATA
Runtime Versions: AIR 1.0, Flash Player 9

Dispatched after raw data is sent or received.

Defines the value of the type property of a data event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
dataThe raw data loaded into Flash Player or Adobe AIR.
targetThe XMLSocket object receiving data.
ioError Event  
Event Object Type: flash.events.IOErrorEvent
property IOErrorEvent.type = flash.events.IOErrorEvent.IO_ERROR
Runtime Versions: AIR 1.0, Flash Player 9

Dispatched when an input/output error occurs that causes a send or receive operation to fail.

Defines the value of the type property of an ioError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
errorIDA reference number associated with the specific error.
targetThe network object experiencing the input/output error.
textText to be displayed as an error message.
securityError Event  
Event Object Type: flash.events.SecurityErrorEvent
property SecurityErrorEvent.type = flash.events.SecurityErrorEvent.SECURITY_ERROR
Runtime Versions: AIR 1.0, Flash Player 9

Dispatched if a call to the XMLSocket.connect() method attempts to connect either to a server outside the caller's security sandbox or to a port lower than 1024.

The SecurityErrorEvent.SECURITY_ERROR constant defines the value of the type property of a securityError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe network object reporting the security error.
textText to be displayed as an error message.

See also