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
Parameters
url:
string
protocols:
string
Optional
Methods
addEventListener
addEventListener(event: , callback: Function
): void
Adds a handler for the specified WebSocketEvents event. Use only functions as handlers; anything except a function leads to the error and scenario termination when a handler is called
Parameters
event:
Event class (i.e., WebSocketEvents.OPEN)
callback:
Function
Handler function. A single parameter is passed - object with event information
Returns
type:
void
close
close(code: , reason: string
): void
Closes the WebSocket connection or connection attempt.
Parameters
code:
OptionalWebSocket close code.
reason:
string
OptionalReason why the connection is closed.
Returns
type:
void
removeEventListener
removeEventListener(event: , callback: Function
): void
Removes a handler for the specified WebSocketEvents event
Parameters
event:
Event class (i.e., WebSocketEvents.OPEN)
callback:
Function
OptionalHandler function. If not specified, all event listeners are removed
Returns
type:
void
send
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
sendMediaTo(targetMediaUnit: , optional: ): 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
targetMediaUnit:
Media unit that receives media.
optional:
Custom parameters for WebSocket interaction only.
Returns
type:
void
stopMediaTo
stopMediaTo(targetMediaUnit: , optional: ): void
Stops sending media with the specified parameters to the target unit.
Parameters
targetMediaUnit:
Media unit that stops receiving media.
optional:
Custom parameters for WebSocket interaction only.
Returns
type:
void
Props
onclose
onclose: (ev: WebSocketEvents.CLOSE) => any
| null
Event handler is called when the connection is closed.
onerror
onerror: (ev: WebSocketEvents.ERROR) => any
| null
Event handler is called when an error occurs.
onmediaended
onmediaended: (ev: WebSocketEvents.MEDIA_ENDED) => any,null
Event handler is called after the end of the audio stream.
onmediastarted
onmediastarted: (ev: WebSocketEvents.MEDIA_STARTED) => any,null
Event handler is called when the audio stream is started playing.
onmessage
onmessage: (ev: WebSocketEvents.MESSAGE) => any
| null
Event handler is called when a message is received.
onopen
onopen: (ev: WebSocketEvents.OPEN) => any
| null
Event handler is called when the connection is open (ready to send and receive data).
url
url: string
Returns the absolute URL of the WebSocket. For outbound connection, it is the URL to which to connect; for inbound, it is the WebSocket session URL.