#ws-client
WebSocket client library for webApp client developer
To use ws-client, you need to require freebird-websocket module and get websocket client constructor to new an instance. Just need to use start()
method to let websocket client start running.
var WsClient = require('freebird-websocket').Client;
wsClient = new WsClient();
wsClient.start('ws://192.168.1.103:3000', {});
Check whether the websocket client is running.
Arguments:
- (none)
Returns
- (Boolean): true or false
if (wsClient.isRunning()) {
// wsClient is running
// you can send request here
} else {
// wsClient is stop running
};
Start running websocket client, and sent authentication data to server to do authenticate.
Arguments:
addr
(String): host addressoption
(Object): An object to set up the websocket client. Please refer to ws.md to see more detail about options.authData
(Object): Authenticate data. It can contain any information you would like to authenticate.
Returns
- (Boolean): true or false
Example
var options = {
host: 'http://192.168.1.103'
},
authData = {
username: 'xxx',
password: 'xxxxxx'
};
wsClient.start('ws://192.168.1.103:3000', options, authData);
Stop running websocket client, and close the socket.
Arguments:
- (none)
Returns
- (Boolean): true or false
wsClient.stop();
Client sends to Server to request something or to ask the server to perform an operation.
Arguments:
subSys
(String): Only 3 types accepted. They are 'net', 'dev', and 'gad' to denote which subsystem is this message going to.cmd
(String): Command Identifier corresponding to the API name. It can be found in the Command Name field of the Request Data Model.args
(Object): A value-object that contains command arguments. Please see section Request Data Model to learn more about the args data object.callback
(Function):function (err, result) {}
. Get called when server respond to client with the results of the client asking for.'err'
(Error): Error object.'result'
(Object): result is an object of{ status, data }
.status
is corresponding to RSP Status Code.data
is a response data object, you can refer to Response Data Model to see more detail.
Returns
- (none)
Example
wsClient.sendReq('net', 'getAllDevIds', {ncName: 'ble-core'}, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
// result equal to
// {
// status: 0,
// data: {
// ids: [1, 5, 8, 15, 27]
// }
// }
}
});
The wsClient will fire event when receiving an indication from websocket server side.
evtType
(String): Event type. It is same with the Indication typesmsg
(Object): It is a message object with properties'subsys'
,'id'
and'data'
subsys
(String): They are 'net', 'dev', and 'gad' to denote which subsystem is this indication coming from.id
(Number): Id of the sender. id is meaningless ifsubsys === 'net'
. id is device id ifsubsys === 'dev'
. id is gadget id ifsubsys === 'gad'
data
(Object): Data along with the indication. Please see section Indication Data Model to learn more about the indication data format.