-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCJsonRpcClient.js
More file actions
240 lines (240 loc) · 8.63 KB
/
CJsonRpcClient.js
File metadata and controls
240 lines (240 loc) · 8.63 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
/**
* 2018
*
* Contributor(s):
*
* Anatoly Yuzefovich <iskhartakh@gmail.com>
*
* A module JSON-RPC Client lib
*
* @module CJsonRpcClient
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { CLogger } from './CLogger';
/** Class representation a JSON RPC Client */
var CJsonRpcClient = /** @class */ (function () {
/**
* CJsonRpcClient class constructor
*
* @constructor
* @param {Object} options - Options object
*/
function CJsonRpcClient(options) {
this._queue = [];
this._current_id = 1; // the id to match request/response
this._ws_callbacks = {};
this._socketRetryCount = 0;
this._socketRetryTimeout = 1000;
this._options = Object.freeze(__assign({ socketUrl: NaN, onMessage: NaN, login: NaN, passwd: NaN, sessid: NaN, loginParams: NaN, userVariables: NaN, debug: false }, options));
if (typeof this._options.logger === 'object') {
this._logger = this._options.logger;
}
else {
this._logger = new CLogger(this._options.sessid + " CJsonRpcClient", this._options.debug);
}
this._socket = this.socket; // init connection
}
Object.defineProperty(CJsonRpcClient.prototype, "socket", {
/**
* Getter method for websocket
*
* @method socket
* @return {Object} socket - A WebSocket object
*/
get: function () {
var _this = this;
if (this._socket)
return this._socket;
if (this.socketRetrying) {
clearTimeout(this.socketRetrying);
this.socketRetrying = NaN;
}
this._socket = new WebSocket(this._options.socketUrl);
this._socket.onmessage = function (event) { return _this._onWSMessage(event); };
this._socket.onopen = function () { return _this._onWSConnect(); };
this._socket.onclose = function () { return _this._onWSClose(); };
this._socket.onerror = function (event) { return _this._onWSError(event); };
return this._socket;
},
enumerable: true,
configurable: true
});
/**
* Call verto method (response required, use notify othewise)
*
* @method call
* @param {string} method - The method to run on JSON-RPC server.
* @param {Object} params - The params object.
* @param {function} success_cb - A callback for successful request.
* @param {function} error_cb - A callback for error.
*/
CJsonRpcClient.prototype.call = function (method, params, success_cb, error_cb) {
if (params === void 0) { params = {}; }
if (success_cb === void 0) { success_cb = function (e) { return console.debug('CJsonRpcClient::call:success_cb: ', e); }; }
if (error_cb === void 0) { error_cb = function (e) { return console.debug('CJsonRpcClient::call:error_cb: ', e); }; }
var request = {
jsonrpc: '2.0',
method: method,
params: params,
id: this._current_id++
};
this._wsCall(request, success_cb, error_cb);
};
/**
* Notify verto method call
*
* @method notify
* @param {string} method - The method to run on JSON-RPC server.
* @param {Object} params - The params object.
*/
CJsonRpcClient.prototype.notify = function (method, params) {
if (this._options.sessid) {
params.sessid = this._options.sessid;
}
var request = {
jsonrpc: '2.0',
method: method,
params: params
};
this._wsCall(request);
};
/**
* Real ws call method. Internal
*
* @method _wsCall
* @param {Object} request - The request object.
* @param {function} success_cb - A callback for successful request.
* @param {function} error_cb - A callback for error.
*/
CJsonRpcClient.prototype._wsCall = function (request, success_cb, error_cb) {
var jsonRequest = JSON.stringify(request);
if (this.socket.readyState != WebSocket.OPEN) {
this._queue.push(jsonRequest);
return;
}
this.socket.send(jsonRequest);
// Setup callbacks per request. The request its Obj with 'id' field and response required
if (request.id) {
this._ws_callbacks[request.id] = { request: jsonRequest, requestObj: request, success_cb: success_cb, error_cb: error_cb };
}
};
/**
* OnMessage callback. Internal
*
* @method _onWSMessage
* @param {Object} event - Event object
*/
CJsonRpcClient.prototype._onWSMessage = function (event) {
var logger = this._logger.method('_onWSMessage', this._options.debug);
logger.debug(event);
var response;
try {
response = JSON.parse(event.data);
}
catch (err) {
logger.error(err);
return;
}
if (typeof response == 'object' && response.jsonrpc == '2.0' && response.id) {
if (response.result && this._ws_callbacks[response.id]) {
var success_cb = this._ws_callbacks[response.id].success_cb;
delete this._ws_callbacks[response.id];
success_cb(response.result);
return;
}
else if (response.error && this._ws_callbacks[response.id]) {
var error_cb = this._ws_callbacks[response.id].error_cb;
delete this._ws_callbacks[response.id];
error_cb(response.error);
return;
}
}
// Handler of the response with no request
if (this._options.onMessage) {
event.eventData = response || {};
var reply = this._options.onMessage(event);
if (reply && typeof reply === 'object' && event.eventData.id) {
var msg = {
jsonrpc: '2.0',
id: event.eventData.id,
result: reply
};
logger.debug('_onWSMessage: Sending Reply', msg);
this.socket.send(JSON.stringify(msg));
}
}
};
/**
* OnConnect callback. Internal
*
* @method _onWSConnect
*/
CJsonRpcClient.prototype._onWSConnect = function () {
var logger = this._logger.method('_onWSConnect', this._options.debug);
logger.debug('fired');
this._socketRetryCount = 0;
this._socketRetryTimeout = 1000;
if (this._options.onWSConnect) {
this._options.onWSConnect(this);
}
if (this._queue.length) {
logger.debug("_onWSConnect: Sending queued requests. Queue deep " + this._queue.length);
}
// Send queued requests
var req = this._queue.pop();
while (req) {
this.socket.send(req);
req = this._queue.pop();
}
};
/**
* OnClose callback. Internal
*
* @method _onWSClose
*/
CJsonRpcClient.prototype._onWSClose = function () {
var _this = this;
var logger = this._logger.method('_onWSClose', this._options.debug);
logger.debug('fired');
this._socket = NaN;
if (this._options.onWSClose) {
this._options.onWSClose(this);
}
logger.error(new Error("WebSocket Lost. Retry count: " + this._socketRetryCount + ". Going to sleep: " + this._socketRetryTimeout + " msec"));
if (this._socketRetryTimeout < 3000 && (this._socketRetryCount % 10) == 0) {
this._socketRetryTimeout += 1000;
}
this.socketRetrying = setTimeout(function () {
logger.info('_onWSClose: WebSocket Attempting Reconnection....');
_this._socket = _this.socket;
}, this._socketRetryTimeout);
this._socketRetryCount++;
};
/**
* OnError callback. Internal
*
* @method _onWSError
* @param {Object} event - Event object
*/
CJsonRpcClient.prototype._onWSError = function (event) {
var logger = this._logger.method('_onWSError', this._options.debug);
logger.debug(new Error(event));
this._socket = NaN;
if (this._options.onWSError) {
this._options.onWSError(this, event);
}
};
return CJsonRpcClient;
}());
export { CJsonRpcClient };
//# sourceMappingURL=CJsonRpcClient.js.map