WebSocketBridge
Direct Subclass:
Bridge between Channels and plain javascript.
Example:
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen(function(action, stream) {
console.log(action, stream);
});
Method Summary
Public Methods | ||
public |
Connect to the websocket server |
|
public |
demultiplex(stream: String, cb: Function) Adds a 'stream handler' callback. |
|
public |
Starts listening for messages on the websocket, demultiplexing if necessary. |
|
public |
Sends a message to the reply channel. |
|
public |
Returns an object to send messages to a specific stream |
Public Methods
public connect(url: String, protocols: String[] | String, options: Object) source
Connect to the websocket server
Params:
Name | Type | Attribute | Description |
url | String |
|
The url of the websocket. Defaults to
|
protocols | String[] | String |
|
Optional string or array of protocols. |
options | Object | Object of options for |
Example:
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
public demultiplex(stream: String, cb: Function) source
Adds a 'stream handler' callback. Messages coming from the specified stream will call the specified callback.
Example:
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen();
webSocketBridge.demultiplex('mystream', function(action, stream) {
console.log(action, stream);
});
webSocketBridge.demultiplex('myotherstream', function(action, stream) {
console.info(action, stream);
});
public listen(cb: Function) source
Starts listening for messages on the websocket, demultiplexing if necessary.
Params:
Name | Type | Attribute | Description |
cb | Function |
|
Callback to be execute when a message
arrives. The callback will receive |
Example:
const webSocketBridge = new WebSocketBridge();
webSocketBridge.connect();
webSocketBridge.listen(function(action, stream) {
console.log(action, stream);
});
public send(msg: Object) source
Sends a message to the reply channel.
Params:
Name | Type | Attribute | Description |
msg | Object | The message |
Example:
// We cheat by using the Redux-style Actions as our
// communication protocol with the server. Consider separating
// communication format from client-side action API.
webSocketBridge.send({type: 'MYACTION', 'payload': 'somepayload'});
public stream(stream: String): Object source
Returns an object to send messages to a specific stream
Params:
Name | Type | Attribute | Description |
stream | String | The stream name |
Example:
// We cheat by using the Redux-style Actions as our
// communication protocol with the server. Consider separating
// communication format from client-side action API.
webSocketBridge.stream('mystream').send({type: 'MYACTION', 'payload': 'somepayload'})