wilton.js  v202103141
 All Namespaces Functions
Functions
DelayedWebSocket Namespace Reference

wilton/DelayedWebSocket
Send WebSocket message to client from another thread More...

Functions

Undefined close (Function|Undefined callback)
 Close WebSocket.
Object DelayedWebSocket (Number webSocketHandle, Function|Undefined callback)
 Create DelayedWebSocket instance.
Undefined send (String|Object data, Object|Undefined options, Function|Undefined callback)
 Send string or JSON message to client and release WebSocket.

Detailed Description

This module allows to send a "delayed" WebSocket message to client. To create a DelayedWebSocket instance, first webSocketHandle needs to be obtained inside the Server request handler (view) using the req.retainWebSocket()function.

DelayedWebSocket may be created from any thread specifying the webSocketHandle as an argument.

Usage example:

// inside server request handler
GET: function(req) {
// delay sending a response
// and release the current worker thread
// creating a webSocketHandle
var handle = req.retainWebSocket();
// pass the handle to other thread using some channel
chan.send({
handle: handle
});
}
// in other thread
var obj = chan.receive();
var writer = new DelayedWebSocket(obj.handle);
writer.send({
foo: "bar"
});

Function Documentation

Undefined DelayedWebSocket::close ( Function|Undefined  callback)

Closes this WebSocket.

Parameters
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Object DelayedWebSocket::DelayedWebSocket ( Number  webSocketHandle,
Function|Undefined  callback 
)

Creates DelayedWebSocket object instance that can be used to send WebSocket message to client from the different thread.

Parameters
webSocketHandleNumber handle value, that must be obtained inside request handler (view) using req.retainWebSocket() call and (optionally) be passed to another thread after that
callbackFunction|Undefined callback to receive result or error
Returns
Object DelayedResponse instance
Undefined DelayedWebSocket::send ( String|Object  data,
Object|Undefined  options,
Function|Undefined  callback 
)

Sends message to client converting specified object into JSON if necessary. After this call previously retained WebSocket is released (this object cannot be used anymore) and continues to process incoming messages.

Parameters
dataString|Object response body, object will be converted to JSON
optionsObject|Undefined configuration object, currently not used
callbackFunction|Undefined callback to receive result or error
Returns
Undefined