Skip to content

Latest commit

 

History

History
159 lines (111 loc) · 4.84 KB

client.md

File metadata and controls

159 lines (111 loc) · 4.84 KB

#ws-client

WebSocket client library for webApp client developer


Table of Contents

  1. Usage
  2. APIs
  3. Events

1. Usage

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', {});

2. APIs and Events


.isRunning()

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(addr[, options], authData)

Start running websocket client, and sent authentication data to server to do authenticate.

Arguments:

  1. addr (String): host address
  2. option (Object): An object to set up the websocket client. Please refer to ws.md to see more detail about options.
  3. 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()

Stop running websocket client, and close the socket.

Arguments:

  • (none)

Returns

  • (Boolean): true or false
wsClient.stop();

.sendReq(subSys, cmd, args, callback)

Client sends to Server to request something or to ask the server to perform an operation.

Arguments:

  1. subSys (String): Only 3 types accepted. They are 'net', 'dev', and 'gad' to denote which subsystem is this message going to.
  2. cmd (String): Command Identifier corresponding to the API name. It can be found in the Command Name field of the Request Data Model.
  3. args (Object): A value-object that contains command arguments. Please see section Request Data Model to learn more about the args data object.
  4. 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]
        //     }
        // }
    }    
});

3. Event

The wsClient will fire event when receiving an indication from websocket server side.

.on(evtType, function(msg) {...})

  • evtType (String): Event type. It is same with the Indication types
  • msg (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 if subsys === 'net'. id is device id if subsys === 'dev'. id is gadget id if subsys === 'gad'
    • data (Object): Data along with the indication. Please see section Indication Data Model to learn more about the indication data format.