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.
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
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
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()
).
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").
|
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.
|
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.
|
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
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:
Property | Value |
bubbles | false |
cancelable | false ; there is no default behavior to cancel. |
currentTarget | The object that is actively processing the Event
object with an event listener. |
target | The object whose connection has been closed. |
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).
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:
Property | Value |
bubbles | false |
cancelable | false ; there is no default behavior to cancel. |
currentTarget | The object that is actively processing the Event
object with an event listener. |
errorID | A reference number associated with the specific error (AIR only). |
target | The network object experiencing the input/output error. |
text | Text to be displayed as an error message. |
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:
Property | Value |
bubbles | false |
cancelable | false ; there is no default behavior to cancel. |
currentTarget | The object that is actively processing the Event
object with an event listener. |
target | The network object reporting the security error. |
text | Text to be displayed as an error message. |
Event object type: flash.events.WebSocketEvent
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Dispatched when the WebSocket receives a message.
public static const fmtBINARY:uint = 2
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Code for a BINARY websocket message.
public static const fmtCLOSE:uint = 8
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Code for a CLOSE websocket message.
public static const fmtPING:uint = 9
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Code for a PING websocket message.
public static const fmtPONG:uint = 10
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Code for a PONG websocket message.
public static const fmtTEXT:uint = 1
Language version: | ActionScript 3.0
|
Runtime version: | AIR 51.0
|
Code for a TEXT websocket message.
© 2004-2022 Adobe Systems Incorporated. All rights reserved.
Thu Mar 21 2024, 10:06 AM GMT