WebsocketClient

This document describes the WebsocketClient available in the JavaScript environment for app serivices.

Table of content

Object WebsocketClient
Functions connect

Object WebsocketClientConnection
Functions onopen
onerror
onclose
onmessage
send
close
setFlowControl
messageComplete

WebsocketClient

connect
Creates a WebsocketClientConnection object. The actual connection is started asynchronously. So synchronous function calls on that object are handled before the connection is established.

Parameters

string uriAn absolute ws: or wss: URI.

Return value

WebsocketClientConnectionAn object representing the websocket connection.
var websocket = WebsocketClient.connect("wss://pbx.example.com/PBX0/APPS/websocket");

WebsocketClientConnection

onopen
Sets a callback function that is invoked when the connection is established. Optional.

Parameters

function callback(WebsocketClientConnection connection)A callback that will be called when the connection is established.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.onopen(function(conn) {
    log("connection is up");
});
onclose
Sets a callback function that is invoked when the connection is closed. Optional.

Parameters

function callback(WebsocketClientConnection connection)A callback that will be called when the connection is closed. After the callback the connection object is gone and can not be used anymore.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.onclose(function(conn) {
    log("connection closed");
});
onerror
Sets a callback function that is invoked when an error on the connection occurred. Optional.

Parameters

function callback(WebsocketClientConnection connection)A callback that will be called when the connection is closed. After the callback the connection object is gone and can not be used anymore.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.onerror(function(conn) {
    log("connection error");
});
onmessage
Sets a callback function that is invoked when a websocket message is received. Optional.

Parameters

function callback(WebsocketClientConnection connection, message)A callback that will be called when the connection is closed. The message attribute represents the text received with the message.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.onmessage(function(conn, msg) {
    var obj = JSON.parse(msg);
    if (obj.mt == "SomeMessage") {
        // ...
    }
});
send
Sends a websocket text message.

Parameters

string messageThe text message to be sent.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.send(JSON.stringify({ mt: "SomeMessage" }));
setFlowControl
Enables flow control for the connection. That means that after receiving a message with the onmessage callback, the WebsocketClientConnection waits for the application to call messageComplete before receiving more messages. Optional.

Parameters

bool enabledTrue if flow controll shall be used.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.setFlowControl(true);

websocket.onmessage(function(conn, msg) {
    var obj = JSON.parse(msg);
    if (obj.mt == "SomeMessage") {
        // ...
    }
    conn.messageComplete();
});
messageComplete
Tells the library to receive the next websocket message. See setFlowControl for details.

Return value

WebsocketClientConnectionA reference to the object itself. Useful for method chaining.
websocket.setFlowControl(true);

websocket.onmessage(function(conn, msg) {
    var obj = JSON.parse(msg);
    if (obj.mt == "SomeMessage") {
        // ...
    }
    conn.messageComplete();
});