Rate this page:

WebSocket

Represents a WebSocket object that provides the API for creating and managing an outbound or inbound WebSocket connection, as well as for sending and receiving data on it. Add the following line to your scenario code to use the class:

require(Modules.WebSocket);

Constructors

constructor

Copy URL

Parameters

  • url:

    string

  • protocols:

    string

    Optional

Methods

addEventListener

Copy URL
addEventListener(type: 

WebSocketEvents

,
callback:

Function

):

void

Adds a handler for the specified event triggered by a Websocket object.

Parameters

  • WebSocket event type to listen for.

  • callback:

    Function

    Function to run when the event occurs.

Returns

  • type:

    void

close

Copy URL
close(code: 

WebsocketCloseCode

,
reason:

string

):

void

Closes the WebSocket connection or connection attempt.

Parameters

  • Optional

    WebSocket close code.

  • reason:

    string

    Optional

    Reason why the connection was closed.

Returns

  • type:

    void

removeEventListener

Copy URL
removeEventListener(type: 

WebSocketEvents

,
callback:

Function

):

void

Removes a handler for the specified event triggered by a Websocket object.

Parameters

  • WebSocket event type whose event listener should be removed.

  • callback:

    Function

    Function to remove from the event target.

Returns

  • type:

    void

send

Copy URL
send(data: 

string

):

void

Enqueues the specified data to be transmitted over the WebSocket connection.

Parameters

  • data:

    string

    Data to send through a WebSocket.

Returns

  • type:

    void

sendMediaTo

Copy URL
sendMediaTo(targetMediaUnit: 

VoxMediaUnit

,
optional:

sendMediaOptions

):

void

Start sending media with the specified parameters to the target unit. WebSocket works in real time and the recommended duration of one audio chunk is 20ms.

Parameters

Returns

  • type:

    void

stopMediaTo

Copy URL
stopMediaTo(targetMediaUnit: 

VoxMediaUnit

,
optional:

sendMediaOptions

):

void

Stops sending media with the specified parameters to the target unit.

Parameters

  • targetMediaUnit:

    VoxMediaUnit

    Media unit that will stop receiving media.

  • Custom parameters for WebSocket interaction only.

Returns

  • type:

    void

Props

onclose

Copy URL
onclose: 

(ev: WebSocketEvents.CLOSE) => any

|

null

Event handler is called when the connection is closed.

onerror

Copy URL
onerror: 

(ev: ERROR) => any

|

null

Event handler is called when an error occurs.

onmediaended

Copy URL
onmediaended: 

(ev: WebSocketEvents.OPEN) => any,null

Event handler is called after the end of the audio stream.

onmediastarted

Copy URL
onmediastarted: 

(ev: WebSocketEvents.OPEN) => any,null

Event handler is called when the audio stream is started playing.

onmessage

Copy URL
onmessage: 

(ev: WebSocketEvents.MESSAGE) => any

|

null

Event handler is called when a message is received.

onopen

Copy URL
onopen: 

(ev: WebSocketEvents.OPEN) => any

|

null

Event handler is called when the connection is open (ready to send and receive data).

readyState

Copy URL
readyState: 

WebSocketReadyState

Returns the current state of the WebSocket connection.

url

Copy URL
url: 

string

Returns the absolute URL of the WebSocket. For outbound connection, it's the URL to which to connect; for inbound, it's the WebSocket session URL.