-
Notifications
You must be signed in to change notification settings - Fork 0
/
CJsonRpcClient.ts
282 lines (234 loc) · 8.03 KB
/
CJsonRpcClient.ts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* 2018
*
* Contributor(s):
*
* Anatoly Yuzefovich <iskhartakh@gmail.com>
*
* A module JSON-RPC Client lib
*
* @module CJsonRpcClient
*/
import { CLogger } from './CLogger';
/** Class representation a JSON RPC Client */
class CJsonRpcClient {
private _options: { [key: string]: any };
private _logger: CLogger;
private _socket: any;
private _queue: Array<string> = [];
private _current_id: number = 1; // the id to match request/response
private _ws_callbacks: { [key: string]: any } = {};
private _socketRetryCount: number = 0;
private _socketRetryTimeout: number = 1000;
private socketRetrying: any;
/**
* CJsonRpcClient class constructor
*
* @constructor
* @param {Object} options - Options object
*/
constructor(options: { [key: string]: any }) {
this._options = Object.freeze({
socketUrl: NaN,
onMessage: NaN, // Not requested response callback
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
}
/**
* Getter method for websocket
*
* @method socket
* @return {Object} socket - A WebSocket object
*/
get socket() {
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 = (event: any) => this._onWSMessage(event);
this._socket.onopen = () => this._onWSConnect();
this._socket.onclose = () => this._onWSClose();
this._socket.onerror = (event: any) => this._onWSError(event);
return this._socket;
}
/**
* 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.
*/
call(
method: string,
params = {},
success_cb = (e: any) => console.debug('CJsonRpcClient::call:success_cb: ', e),
error_cb = (e: any) => console.debug('CJsonRpcClient::call:error_cb: ', e)
) {
const 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.
*/
notify(method: string, params: { [key: string]: any }) {
if (this._options.sessid) {
params.sessid = this._options.sessid;
}
const 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.
*/
_wsCall(request: { [key: string]: any }, success_cb?: CallableFunction, error_cb?: CallableFunction) {
const 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
*/
_onWSMessage(event: any) {
const logger = this._logger.method('_onWSMessage', this._options.debug);
logger.debug(event);
let 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]) {
const 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]) {
const 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 || {};
const reply = this._options.onMessage(event);
if (reply && typeof reply === 'object' && event.eventData.id) {
const 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
*/
_onWSConnect() {
const 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
let req = this._queue.pop();
while (req) {
this.socket.send(req);
req = this._queue.pop();
}
}
/**
* OnClose callback. Internal
*
* @method _onWSClose
*/
_onWSClose() {
const 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(() => {
logger.info('_onWSClose: WebSocket Attempting Reconnection....');
this._socket = this.socket;
}, this._socketRetryTimeout);
this._socketRetryCount++;
}
/**
* OnError callback. Internal
*
* @method _onWSError
* @param {Object} event - Event object
*/
_onWSError(event: any) {
const 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);
}
}
}
export { CJsonRpcClient };