Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 3x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x 7x 7x 2x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x | /**
* Faye client implementation.
*
* Implements faye client to communicate with IdealNet's pubsub
* server.
*
* @author michael whobrey <mwhobrey@idealss.com>
*/
const Faye = require('faye');
const Extension = require('./fayeExtension');
const {constants} = require('./constants');
const {log} = require('./utils');
/**
* Client class wrapping faye client.
*
* Convenience methods and properties to facilitate setting up,
* connecting, subscribing and publishing messages to PubSub.
*/
class INRClient {
#api;
#group;
#siteId;
#token;
#key;
#client;
#extension;
#onSubscribed;
/**
* Create a client.
*
* @param _api {string} the api that we are connecting to
* @param _group {string} the group id for the connection
* @param _siteId {string} the site id for the connection
* @param _token {string} the token being used for the connection
* @param _key {string} the private key used for signing messages
*/
constructor(_api, _group, _siteId, _token, _key) {
console.log(`Connecting to pubsub on ${constants.pubsubEndpoint()}.`);
console.log("Client properties: ", {
api: _api,
group: _group,
siteId: _siteId,
token: _token
});
this.#api = _api;
this.#group = _group;
this.#siteId = _siteId;
this.#token = _token;
this.#key = _key;
this.#client = new Faye.Client(constants.pubsubEndpoint(), {timeout: 120});
this.#extension = new Extension(_api, _group, _siteId, _token, _key);
this.subscribe = this.subscribe.bind(this);
this.publish = this.publish.bind(this);
this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this);
}
/**
* Getter for #api.
*/
get api() {
return this.#api;
}
/**
* Getter for #group.
*/
get group() {
return this.#group;
}
/**
* Getter for #siteId.
*/
get siteId() {
return this.#siteId;
}
/**
* Getter for #token.
*/
get token() {
return this.#token;
}
/**
* Getter for #key.
*/
get key() {
return this.#key;
}
/**
* Getter for #client
*/
get client() {
return this.#client;
}
/**
* Setter for #client.
*
* @param value {Client} The value for client.
*/
set client(value) {
this.#client = value;
}
/**
* Getter for #state.
*/
get state() {
return this.#client._state;
}
/**
* Getter for #extension.
*/
get extension() {
return this.#extension;
}
/**
* Setter for #extension.
*
* @param value {Extension} The extension we are setting.
*/
set extension(value) {
this.#extension = value;
}
/**
* Setter for #onSubscribed.
*
* @param value {Function} The callback to call.
*/
set onSubscribed(value) {
this.#onSubscribed = value;
}
/**
* Getter for #onSubscribed.
*/
get onSubscribed() {
return this.#onSubscribed;
}
/**
* Subscribe to a channel.
*
* This function allows subscribing to a channel and processing
* the direct response from PubSub (note this is not the same as
* processing the response from another client). Also handles updating
* the extension to let it know the state of the subscription and set
* the property appropriately. Logs result to console.
*
* @param channel {string} The channel we are subscribing to.
*/
subscribe(channel) {
this.#client.subscribe(channel,
(msg) => {
log("Subscribe request response: ", msg);
});
}
/**
* Publish a message to a channel.
*
* Publishes the provided data packet to the provided channel and
* waits for the return to return the response to the caller.
*
* @param channel {string} The channel to publish to.
* @param data {Object} Data packet to publish.
*/
publish(channel, data) {
this.#client.publish(channel, data,
function(mess) {
log('PUBLISH RESPONSE:', mess);
},
function(err) {
log('PUBLISH ERROR:', err);
});
}
/**
* Connect to the faye server.
*
* Setup client and connect to PubSub faye server
*
* @param messageBack {Function} Function to be called when the response from a sent message
* comes back.
*/
async connect(messageBack) {
if (!this.#client) {
this.#client = new Faye.Client(constants.pubsubEndpoint(), {timeout: 120});
}
if (!this.#extension) {
this.extension = new Extension(this.#api, this.#group, this.#siteId, this.#token, this.#key);
}
if (!this.#client._extensions ||
(this.#client._extensions && this.#client._extensions.length === 0)) {
this.#client.addExtension(this.#extension.extend(messageBack));
}
this.#extension.doSubscribe = this.subscribe;
this.#extension.onSubscribed = this.#onSubscribed;
await this.#client.connect();
}
/**
* Disconnect from server.
*
* Disconnect the client from PubSub server.
*/
disconnect() {
this.#client.disconnect();
}
}
module.exports = INRClient;
|