WebSocket object

It is a basic implementation of WebSocket protocol .

Properties

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:

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.

send

( data: string | Bytes )

The method sends message as a text or binary frame (message) to the WebSocket server.

close

( )

Sends disconnect request to the server and closes the socket.