Packageair.net
Classpublic class WebSocket
InheritanceWebSocket Inheritance EventDispatcher Inheritance Object

Language version: ActionScript 3.0
Runtime version: AIR 51.0

The WebSocket class is a utility that encapsulates a normal Socket object and implements the WebSocket protocol (RFC 6455).

This can be used in conjunction with a ServerSocket object to allow web pages to connect to and communicate with an AIR application - once a connection is received, create a new WebSocket instance and call startServer() to handle the protocol handshake.

To initiate an outgoing WebSocket connection to a server, use the connect() method, passing in the URL string in format "ws://some.example.com/ws/demo" ('wss' can be used for secure WebSocket over TLS connections). An optional set of protocols can be passed in, that will be passed to the server, and the chosen protocol can then be accessed via the protocol property.

If a PING message is received by a WebSocket object, it will automatically response with the appropriate PONG message. It is up to the developer to implement any outgoing PING messages (or unsolicited PONGs) should those be desired.

Note: messages are limited to 4GB in size, due to the restrictions of the ByteArray class.



Public Properties
 PropertyDefined by
  closeReason : int
[read-only] Retrieves the 'close' reason code, after the WebSocket has been closed.
WebSocket
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  protocol : String
The protocol for the WebSocket connection (for the Sec-WebSocket-Protocol field).
WebSocket
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined by
  
Constructor for a WebSocket object.
WebSocket
 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
  
close(reasonCode:uint = 1000):void
Closes the WebSocket.
WebSocket
  
connect(url:String, protocols:Vector.<String> = null):void
Initiates a connection as a WebSocket client.
WebSocket
 Inherited
Dispatches an event into the event flow.
EventDispatcher
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
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
  
sendMessage(opcode:uint, data:*):void
Sends a message via the WebSocket protocol.
WebSocket
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
Takes over an existing connection in order to act as a socket server.
WebSocket
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 EventSummaryDefined by
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
   Dispatched when the WebSocket is closed by the other party.WebSocket
   Dispatched when a client WebSocket is connected (following successful handshake).WebSocket
 Inherited [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
   Dispatched if there is a connection error when trying to open a WebSocket connection.WebSocket
   Dispatched if there is a security error when trying to open a WebSocket connection.WebSocket
   Dispatched when the WebSocket receives a message.WebSocket
Public Constants
 ConstantDefined by
  fmtBINARY : uint = 2
[static] Code for a BINARY websocket message.
WebSocket
  fmtCLOSE : uint = 8
[static] Code for a CLOSE websocket message.
WebSocket
  fmtPING : uint = 9
[static] Code for a PING websocket message.
WebSocket
  fmtPONG : uint = 10
[static] Code for a PONG websocket message.
WebSocket
  fmtTEXT : uint = 1
[static] Code for a TEXT websocket message.
WebSocket
Property detail
closeReasonproperty
closeReason:int  [read-only]

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Retrieves the 'close' reason code, after the WebSocket has been closed.

Implementation
    public function get closeReason():int
protocolproperty 
protocol:String  [read-write]

Language version: ActionScript 3.0
Runtime version: AIR 51.0

The protocol for the WebSocket connection (for the Sec-WebSocket-Protocol field).

For a server WebSocket, this can be used to specify the protocol to send back to the client. For a client WebSocket, this field will be populated with the value that the server had specified during handshaking. Following handshaking, the protocol value becomes read-only.

Implementation
    public function get protocol():String
    public function set protocol(value:String):void
Constructor detail
WebSocket()constructor
public function WebSocket()

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Constructor for a WebSocket object. Once created, the object can either be used as a server (via startServer()) or as a client (via connect()).

Method detail
close()method
public function close(reasonCode:uint = 1000):void

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Closes the WebSocket.

Note that if this method is used, no Event.CLOSE event is dispatched.

Parameters
reasonCode:uint (default = 1000) — The code to use in the 'close' WebSocket message, defaulting to 1000 ("normal closure").
connect()method 
public function connect(url:String, protocols:Vector.<String> = null):void

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Initiates a connection as a WebSocket client.

This will connect to the host provided in the url, either via a normal Socket or using a SecureSocket if a 'wss' protocol is requested. It will then initiate an HTTP protocol handshake to upgrade the connection to use WebSockets. If the upgrade fails, the socket will be closed and a Event.CLOSE event will be dispatched.

Parameters
url:String — The target WebSocket URL, for example "ws://localhost:4200/testing".
 
protocols:Vector.<String> (default = null) — An optional array of protocol strings that will be passed to the server.

Throws
ArgumentError — When there is already a socket connection open, or if the url is an invalid protocol.
sendMessage()method 
public function sendMessage(opcode:uint, data:*):void

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Sends a message via the WebSocket protocol.

Parameters
opcode:uint — The message type to send (text, binary, ping, pong etc).
 
data:* — The data to send (either a String or a ByteArray).

Throws
ArgumentError — When the data is null or an invalid object type, or is too long.
startServer()method 
public function startServer(s:Socket):void

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Takes over an existing connection in order to act as a socket server.

Calling this method will set up the WebSocket object to listen out for an HTTP request to upgrade the connection to the WebSocket protocol.

If the data that's received is not an HTTP upgrade request, the socket will be closed and a Event.CLOSE event will trigger.

Parameters
s:Socket — The Socket object to adopt for the WebSocket server.

Throws
ArgumentError — If the provided socket argument is null.
Event detail
closeevent 
Event object type: flash.events.Event
Event.type property = flash.events.Event.CLOSE

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Dispatched when the WebSocket is closed by the other party. The reason for the closure can be obtained from the closeReason property.

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.
connectedevent  
Event object type: flash.events.Event

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Dispatched when a client WebSocket is connected (following successful handshake).

ioErrorevent  
Event object type: flash.events.IOErrorEvent
IOErrorEvent.type property = flash.events.IOErrorEvent.IO_ERROR

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Dispatched if there is a connection error when trying to open a WebSocket connection.

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 (AIR only).
targetThe network object experiencing the input/output error.
textText to be displayed as an error message.
securityErrorevent  
Event object type: flash.events.SecurityErrorEvent
SecurityErrorEvent.type property = flash.events.SecurityErrorEvent.SECURITY_ERROR

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Dispatched if there is a security error when trying to open a WebSocket connection.

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.
websocketDataevent  
Event object type: flash.events.WebSocketEvent

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Dispatched when the WebSocket receives a message.

Constant detail
fmtBINARYconstant
public static const fmtBINARY:uint = 2

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Code for a BINARY websocket message.

fmtCLOSEconstant 
public static const fmtCLOSE:uint = 8

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Code for a CLOSE websocket message.

fmtPINGconstant 
public static const fmtPING:uint = 9

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Code for a PING websocket message.

fmtPONGconstant 
public static const fmtPONG:uint = 10

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Code for a PONG websocket message.

fmtTEXTconstant 
public static const fmtTEXT:uint = 1

Language version: ActionScript 3.0
Runtime version: AIR 51.0

Code for a TEXT websocket message.