WebsocketClient
This document describes the WebsocketClient available in the JavaScript environment for app serivices.
Table of content
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 uri | An absolute ws: or wss: URI. |
Return value
var websocket = WebsocketClient.connect("wss://pbx.example.com/PBX0/APPS/websocket");
onopen
Sets a callback function that is invoked when the connection is established.
Optional.
Parameters
Return value
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
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
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
websocket.onmessage(function(conn, msg) {
var obj = JSON.parse(msg);
if (obj.mt == "SomeMessage") {
// ...
}
});
send
Sends a websocket text message.
Parameters
string message | The text message to be sent. |
Return value
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 enabled | True if flow controll shall be used. |
Return value
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
websocket.setFlowControl(true);
websocket.onmessage(function(conn, msg) {
var obj = JSON.parse(msg);
if (obj.mt == "SomeMessage") {
// ...
}
conn.messageComplete();
});