XMLScanner

WebSocket object

It is a basic implementation of WebSocket protocol .

Constants

return values of WebSocket.state property

INITIAL
– initial state of the websocket immediately after construction.
CONNECTING
– web socket is connecting to the host
CONNECTED
– socket is connected – ready to accept WebSocket.sendText() calls and data sent by the server.
CLOSING
– the socket is closing.
CLOSED
– the socket is closed. Any operation on it will throw an error.

Properties

state
integer, socket state – one of constants above.
pending
integer, number of bytes in outbound buffer. Zero means socket had sent all data queued for sending by WebSocket.sendText() and WebSocket.sendBinary() methods.

Methods

connect

( url: string [, timeout: duration] ) : WebSocket

Static constructor of the WebSocket. Accepts websocket schema URL: "ws://domain/path". Returns new socket in INITIAL state. The timeout is a connection timeout. If timeout is exceeded while connecting error event will be generated.

on

( event: string, callback: function ) : this

Subscribe  callback on one of socket events:

  • "connect" -> function(), socket just connected to the host;
  • "text" -> function(data), text packet received, data contains string;  
  • "binary" -> function(data), binary packet received, data contains Bytes object;
  • "error" -> function(errmsg), error occured;
  • "close" -> function(), socket was closed;

The event name may contain ".namespace" part that can be used in .off() call.

once

( event: string, callback: function ) : this

Same as on() but subscribes the callback for once execution ( record is auto removed after first callback )

off

( event: string | callback: function ) : this

Unsubscribe callback either by its name or by its function reference.

Event name may contain only namespace part, so this: socket.off(".namespace") will unsubscribe all handlers that were set with that namespace.

sendText

( text: string )

The menthod sends message as a text frame to the WebSocket server.

sendBinary

( data: Bytes )

The menthod sends data as a binary frame to the WebSocket server.

close

( )

Sends disconnect request to the server and closes the socket.