@tchain/tcabci-read-js-client 2.3.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/client.js +198 -117
- package/client_test.js +99 -42
- package/errors.js +11 -11
- package/message.js +43 -28
- package/package.json +6 -5
- package/transaction.js +66 -66
- package/util.js +12 -2
- package/websocket.js +344 -0
- package/websocketOptions.js +277 -0
- package/websocketOptions_test.js +129 -0
- package/websocket_test.js +539 -0
package/README.md
CHANGED
package/client.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import ReconnectingWebSocket from 'reconnecting-websocket'
|
|
2
1
|
import {
|
|
3
2
|
ADDRESSES_IS_EMPTY,
|
|
4
3
|
ALREADY_CONNECTED,
|
|
@@ -18,7 +17,9 @@ import {
|
|
|
18
17
|
TX_TYPE,
|
|
19
18
|
} from './constants.js'
|
|
20
19
|
import Message from './message.js'
|
|
21
|
-
import { toJSON } from './util.js'
|
|
20
|
+
import { isJSON, toJSON } from './util.js'
|
|
21
|
+
import { DefaultTimeout, Options } from './websocketOptions.js'
|
|
22
|
+
import { TWebSocket } from './websocket.js'
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* @callback successCallback
|
|
@@ -41,32 +42,36 @@ import { toJSON } from './util.js'
|
|
|
41
42
|
* @return void
|
|
42
43
|
*/
|
|
43
44
|
export default class TCaBCIClient {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
_subscribed = false
|
|
46
|
+
_subscribedAddresses = []
|
|
47
|
+
_subscribedSignedData = {}
|
|
48
|
+
_connected = false
|
|
49
|
+
_chainName = 'transferchain'
|
|
50
|
+
_chainVersion = 'v1'
|
|
51
|
+
_version = 'v2.3.0'
|
|
50
52
|
/**
|
|
51
53
|
* @type {?successCallback}
|
|
52
54
|
*/
|
|
53
|
-
|
|
55
|
+
_successCb = null
|
|
54
56
|
/**
|
|
55
57
|
* @type {?errorCallback}
|
|
56
58
|
*/
|
|
57
|
-
|
|
59
|
+
_errorCb = null
|
|
58
60
|
/**
|
|
59
61
|
* @type {?closeCallback}
|
|
60
62
|
*/
|
|
61
|
-
|
|
63
|
+
_closeCb = null
|
|
62
64
|
/**
|
|
63
65
|
* @type {?listenCallback}
|
|
64
66
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
_listenCb = null
|
|
68
|
+
_wsLibrary = null
|
|
69
|
+
/**
|
|
70
|
+
* @type {TWebSocket}
|
|
71
|
+
*/
|
|
72
|
+
_ws
|
|
73
|
+
_readNodeAddress = READ_NODE_ADDRESS
|
|
74
|
+
_readNodeWSAddress = READ_NODE_WS_ADDRESS
|
|
70
75
|
|
|
71
76
|
/**
|
|
72
77
|
* @param {Array<String>} readNodeAddresses
|
|
@@ -80,19 +85,21 @@ export default class TCaBCIClient {
|
|
|
80
85
|
chainName = null,
|
|
81
86
|
chainVersion = null,
|
|
82
87
|
) {
|
|
83
|
-
if (!wsLibrary) throw INVALID_ARGUMENTS
|
|
84
|
-
this.
|
|
88
|
+
if (!wsLibrary) throw new Error(INVALID_ARGUMENTS)
|
|
89
|
+
this._wsLibrary = wsLibrary
|
|
85
90
|
|
|
86
91
|
if (Array.isArray(readNodeAddresses) && readNodeAddresses.length === 2) {
|
|
87
|
-
this.
|
|
88
|
-
this.
|
|
92
|
+
this._readNodeAddress = readNodeAddresses[0]
|
|
93
|
+
this._readNodeWSAddress = readNodeAddresses[1]
|
|
89
94
|
|
|
90
|
-
if (!readNodeAddresses[0].startsWith('http'))
|
|
91
|
-
|
|
95
|
+
if (!readNodeAddresses[0].startsWith('http'))
|
|
96
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
97
|
+
if (!readNodeAddresses[1].startsWith('ws'))
|
|
98
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
92
99
|
}
|
|
93
100
|
|
|
94
|
-
if (chainName) this.
|
|
95
|
-
if (chainVersion) this.
|
|
101
|
+
if (chainName) this._chainName = chainName
|
|
102
|
+
if (chainVersion) this._chainVersion = chainVersion
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
/**
|
|
@@ -103,7 +110,7 @@ export default class TCaBCIClient {
|
|
|
103
110
|
httpClient(uri, req) {
|
|
104
111
|
req.cache = 'no-cache'
|
|
105
112
|
req.headers = {
|
|
106
|
-
Client: `tcabaci-read-js-client${this.
|
|
113
|
+
Client: `tcabaci-read-js-client${this._version}`,
|
|
107
114
|
}
|
|
108
115
|
|
|
109
116
|
if (typeof AbortSignal !== 'undefined') {
|
|
@@ -112,13 +119,13 @@ export default class TCaBCIClient {
|
|
|
112
119
|
|
|
113
120
|
req.priority = 'high'
|
|
114
121
|
|
|
115
|
-
return fetch(this.
|
|
122
|
+
return fetch(this._readNodeAddress + uri, req).then((response) =>
|
|
116
123
|
this.handleRestResponse(response),
|
|
117
124
|
)
|
|
118
125
|
}
|
|
119
126
|
|
|
120
127
|
Socket() {
|
|
121
|
-
return this.
|
|
128
|
+
return this._ws
|
|
122
129
|
}
|
|
123
130
|
|
|
124
131
|
/**
|
|
@@ -126,7 +133,7 @@ export default class TCaBCIClient {
|
|
|
126
133
|
* @constructor
|
|
127
134
|
*/
|
|
128
135
|
SetSuccessCallback(cb) {
|
|
129
|
-
this.
|
|
136
|
+
this._successCb = cb
|
|
130
137
|
|
|
131
138
|
return this
|
|
132
139
|
}
|
|
@@ -136,7 +143,7 @@ export default class TCaBCIClient {
|
|
|
136
143
|
* @constructor
|
|
137
144
|
*/
|
|
138
145
|
SetErrorCallback(cb) {
|
|
139
|
-
this.
|
|
146
|
+
this._errorCb = cb
|
|
140
147
|
|
|
141
148
|
return this
|
|
142
149
|
}
|
|
@@ -146,7 +153,7 @@ export default class TCaBCIClient {
|
|
|
146
153
|
* @constructor
|
|
147
154
|
*/
|
|
148
155
|
SetCloseCallback(cb) {
|
|
149
|
-
this.
|
|
156
|
+
this._closeCb = cb
|
|
150
157
|
|
|
151
158
|
return this
|
|
152
159
|
}
|
|
@@ -156,7 +163,7 @@ export default class TCaBCIClient {
|
|
|
156
163
|
* @constructor
|
|
157
164
|
*/
|
|
158
165
|
SetListenCallback(cb) {
|
|
159
|
-
this.
|
|
166
|
+
this._listenCb = cb
|
|
160
167
|
|
|
161
168
|
return this
|
|
162
169
|
}
|
|
@@ -165,41 +172,58 @@ export default class TCaBCIClient {
|
|
|
165
172
|
return this.connect()
|
|
166
173
|
}
|
|
167
174
|
|
|
168
|
-
|
|
175
|
+
/**
|
|
176
|
+
* @return {Awaited<TCaBCIClient>}
|
|
177
|
+
* @throws {Error}
|
|
178
|
+
*/
|
|
179
|
+
async Stop() {
|
|
169
180
|
if (!this.getConnected()) {
|
|
170
|
-
throw NOT_CONNECTED
|
|
181
|
+
throw new Error(NOT_CONNECTED)
|
|
171
182
|
}
|
|
172
|
-
this.Disconnect(1000)
|
|
183
|
+
await this.Disconnect(1000)
|
|
173
184
|
this.setConnected(false)
|
|
174
185
|
this.setSubscribed(false)
|
|
175
186
|
|
|
176
187
|
return this
|
|
177
188
|
}
|
|
178
189
|
|
|
179
|
-
|
|
190
|
+
/**
|
|
191
|
+
* @param {Array<string>} addresses
|
|
192
|
+
* @param {Object} signedData
|
|
193
|
+
* @param {?Array<string>} txTypes
|
|
194
|
+
* @return {TCaBCIClient}
|
|
195
|
+
* @throws {Error}
|
|
196
|
+
*/
|
|
197
|
+
Subscribe(addresses, signedData, txTypes = null) {
|
|
180
198
|
if (!Array.isArray(addresses)) {
|
|
181
|
-
throw INVALID_ARGUMENTS
|
|
199
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (typeof signedData !== 'object') {
|
|
203
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
182
204
|
}
|
|
183
205
|
|
|
184
206
|
if (txTypes && !Array.isArray(addresses)) {
|
|
185
|
-
throw INVALID_ARGUMENTS
|
|
207
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
186
208
|
}
|
|
187
209
|
|
|
188
|
-
if (txTypes
|
|
189
|
-
throw INVALID_ARGUMENTS
|
|
210
|
+
if (txTypes && txTypes.length > Object.values(TX_TYPE).length) {
|
|
211
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
190
212
|
}
|
|
191
213
|
|
|
192
214
|
if (!this.getConnected()) {
|
|
193
|
-
throw NOT_CONNECTED
|
|
215
|
+
throw new Error(NOT_CONNECTED)
|
|
194
216
|
}
|
|
195
217
|
|
|
196
|
-
let addrs = []
|
|
218
|
+
let addrs = [],
|
|
219
|
+
sdata = {}
|
|
197
220
|
|
|
198
221
|
if (addresses.length === 0) {
|
|
199
|
-
throw ADDRESSES_IS_EMPTY
|
|
222
|
+
throw new Error(ADDRESSES_IS_EMPTY)
|
|
200
223
|
}
|
|
201
224
|
|
|
202
225
|
addrs = addresses
|
|
226
|
+
sdata = signedData
|
|
203
227
|
|
|
204
228
|
if (this.getSubscribeAddresses().length > 0) {
|
|
205
229
|
let newAddress = []
|
|
@@ -212,26 +236,40 @@ export default class TCaBCIClient {
|
|
|
212
236
|
addrs = newAddress
|
|
213
237
|
}
|
|
214
238
|
|
|
215
|
-
|
|
239
|
+
if (this.getSubscribedSignedData().length > 0) {
|
|
240
|
+
sdata = this.getSubscribedSignedData()
|
|
241
|
+
}
|
|
216
242
|
|
|
217
|
-
|
|
243
|
+
const message = new Message(
|
|
244
|
+
true,
|
|
245
|
+
MESSAGE_TYPE.SUBSCRIBE,
|
|
246
|
+
addrs,
|
|
247
|
+
sdata,
|
|
248
|
+
txTypes,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
this._ws.send(message.ToJSON())
|
|
218
252
|
this.setSubscribeAddresses(addrs, true)
|
|
219
|
-
|
|
253
|
+
.setSubscribeSignedData(signedData)
|
|
254
|
+
.setSubscribed(true)
|
|
255
|
+
|
|
256
|
+
return this
|
|
220
257
|
}
|
|
221
258
|
|
|
222
259
|
Unsubscribe() {
|
|
223
260
|
if (!this.getSubscribed()) {
|
|
224
|
-
throw NOT_SUBSCRIBED
|
|
261
|
+
throw new Error(NOT_SUBSCRIBED)
|
|
225
262
|
}
|
|
226
|
-
this.
|
|
263
|
+
this._ws.send(
|
|
227
264
|
new Message(
|
|
228
265
|
true,
|
|
229
266
|
MESSAGE_TYPE.UNSUBSCRIBE,
|
|
230
267
|
this.getSubscribeAddresses(),
|
|
231
|
-
).
|
|
268
|
+
).ToJSON(),
|
|
232
269
|
)
|
|
233
270
|
this.setSubscribed(false)
|
|
234
271
|
this.setSubscribeAddresses([])
|
|
272
|
+
this.setSubscribeSignedData({})
|
|
235
273
|
}
|
|
236
274
|
|
|
237
275
|
/**
|
|
@@ -241,7 +279,7 @@ export default class TCaBCIClient {
|
|
|
241
279
|
*/
|
|
242
280
|
LastBlock(chainName = null, chainVersion = null) {
|
|
243
281
|
return this.httpClient(
|
|
244
|
-
`/v1/blocks?chain_name=${chainName ?? this.
|
|
282
|
+
`/v1/blocks?chain_name=${chainName ?? this._chainName}&chain_version=${chainVersion ?? this._chainVersion}&limit=1&offset=0`,
|
|
245
283
|
{
|
|
246
284
|
method: 'GET',
|
|
247
285
|
},
|
|
@@ -249,15 +287,23 @@ export default class TCaBCIClient {
|
|
|
249
287
|
.then((res) => {
|
|
250
288
|
return { blocks: res.data, total_count: res.total_count }
|
|
251
289
|
})
|
|
252
|
-
.catch((e) => this.handleRestError(e, BLOCK_NOT_FOUND))
|
|
290
|
+
.catch((e) => this.handleRestError(e, new Error(BLOCK_NOT_FOUND)))
|
|
253
291
|
}
|
|
254
292
|
|
|
255
|
-
|
|
293
|
+
/**
|
|
294
|
+
* @param {string} id
|
|
295
|
+
* @param {string} signature
|
|
296
|
+
* @return {Promise<*>}
|
|
297
|
+
*/
|
|
298
|
+
Tx(id, signature) {
|
|
256
299
|
if (!id || typeof id !== 'string') {
|
|
257
|
-
return Promise.reject(INVALID_ARGUMENTS)
|
|
300
|
+
return Promise.reject(new Error(INVALID_ARGUMENTS))
|
|
258
301
|
}
|
|
259
302
|
|
|
260
|
-
return this.httpClient(`/v1/tx/${id}`, {
|
|
303
|
+
return this.httpClient(`/v1/tx/${id}`, {
|
|
304
|
+
method: 'GET',
|
|
305
|
+
headers: { 'X-Signature': signature },
|
|
306
|
+
})
|
|
261
307
|
.then((res) => {
|
|
262
308
|
return { tx: res.data }
|
|
263
309
|
})
|
|
@@ -267,6 +313,7 @@ export default class TCaBCIClient {
|
|
|
267
313
|
/**
|
|
268
314
|
* @param {?Array<string>} recipientAddrs
|
|
269
315
|
* @param {?Array<string>} senderAddrs
|
|
316
|
+
* @param {?Object} signedData
|
|
270
317
|
* @param {?string} typ
|
|
271
318
|
* @param {?Array<string>} types
|
|
272
319
|
* @param {?string} chainName
|
|
@@ -276,22 +323,24 @@ export default class TCaBCIClient {
|
|
|
276
323
|
TxSummary({
|
|
277
324
|
recipientAddrs,
|
|
278
325
|
senderAddrs,
|
|
326
|
+
signedData,
|
|
279
327
|
typ,
|
|
280
328
|
types = null,
|
|
281
329
|
chainName = null,
|
|
282
330
|
chainVersion = null,
|
|
283
331
|
}) {
|
|
284
332
|
if (!recipientAddrs && !senderAddrs) {
|
|
285
|
-
return Promise.reject(INVALID_ARGUMENTS)
|
|
333
|
+
return Promise.reject(new Error(INVALID_ARGUMENTS))
|
|
286
334
|
}
|
|
287
335
|
|
|
288
336
|
return this.httpClient('/v1/tx_summary', {
|
|
289
337
|
method: 'POST',
|
|
290
338
|
body: JSON.stringify({
|
|
291
|
-
chain_name: chainName ?? this.
|
|
292
|
-
chain_version: chainVersion ?? this.
|
|
339
|
+
chain_name: chainName ?? this._chainName,
|
|
340
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
293
341
|
recipient_addrs: recipientAddrs,
|
|
294
342
|
sender_addrs: senderAddrs,
|
|
343
|
+
signed_addrs: signedData,
|
|
295
344
|
...(types ? { types: types } : { typ: typ }),
|
|
296
345
|
}),
|
|
297
346
|
})
|
|
@@ -316,6 +365,7 @@ export default class TCaBCIClient {
|
|
|
316
365
|
* @param {?number} lastOrder
|
|
317
366
|
* @param {?Array<string>} recipientAddrs
|
|
318
367
|
* @param {?Array<string>} senderAddrs
|
|
368
|
+
* @param {?Object} signedData
|
|
319
369
|
* @param {?Array<string>} hashes
|
|
320
370
|
* @param {string} typ
|
|
321
371
|
* @param {?Array<string>} types
|
|
@@ -334,6 +384,7 @@ export default class TCaBCIClient {
|
|
|
334
384
|
lastOrder,
|
|
335
385
|
recipientAddrs,
|
|
336
386
|
senderAddrs,
|
|
387
|
+
signedData,
|
|
337
388
|
hashes,
|
|
338
389
|
typ,
|
|
339
390
|
types,
|
|
@@ -347,13 +398,14 @@ export default class TCaBCIClient {
|
|
|
347
398
|
return this.httpClient('/v1/tx_search/p', {
|
|
348
399
|
method: 'POST',
|
|
349
400
|
body: JSON.stringify({
|
|
350
|
-
chain_name: chainName ?? this.
|
|
351
|
-
chain_version: chainVersion ?? this.
|
|
401
|
+
chain_name: chainName ?? this._chainName,
|
|
402
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
352
403
|
height: `${heightOperator} ${height}`,
|
|
353
404
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
354
405
|
...(lastOrder ? { last_order: lastOrder } : {}),
|
|
355
406
|
...(recipientAddrs ? { recipient_addrs: recipientAddrs } : {}),
|
|
356
407
|
...(senderAddrs ? { sender_addrs: senderAddrs } : {}),
|
|
408
|
+
...(signedData ? { signed_addrs: signedData } : {}),
|
|
357
409
|
...(hashes ? { hashes: hashes } : {}),
|
|
358
410
|
...(limit ? { limit: limit } : {}),
|
|
359
411
|
...(offset ? { offset: offset } : {}),
|
|
@@ -373,10 +425,10 @@ export default class TCaBCIClient {
|
|
|
373
425
|
|
|
374
426
|
Status() {
|
|
375
427
|
return {
|
|
376
|
-
chain_name: this.
|
|
377
|
-
chain_version: this.
|
|
378
|
-
connected: this.
|
|
379
|
-
subscribed: this.
|
|
428
|
+
chain_name: this._chainName,
|
|
429
|
+
chain_version: this._chainVersion,
|
|
430
|
+
connected: this._connected,
|
|
431
|
+
subscribed: this._subscribed,
|
|
380
432
|
}
|
|
381
433
|
}
|
|
382
434
|
|
|
@@ -497,7 +549,7 @@ export default class TCaBCIClient {
|
|
|
497
549
|
commit = false,
|
|
498
550
|
) {
|
|
499
551
|
if (Object.values(TX_TYPE).indexOf(type) < 0) {
|
|
500
|
-
throw TRANSACTION_TYPE_NOT_VALID
|
|
552
|
+
throw new Error(TRANSACTION_TYPE_NOT_VALID)
|
|
501
553
|
}
|
|
502
554
|
|
|
503
555
|
return this.httpClient(
|
|
@@ -519,11 +571,14 @@ export default class TCaBCIClient {
|
|
|
519
571
|
.then((res) => {
|
|
520
572
|
return { data: res.data }
|
|
521
573
|
})
|
|
522
|
-
.catch((e) =>
|
|
574
|
+
.catch((e) =>
|
|
575
|
+
this.handleRestError(e, { 400: new Error(TRANSACTION_NOT_BROADCAST) }),
|
|
576
|
+
)
|
|
523
577
|
}
|
|
524
578
|
|
|
525
579
|
/**
|
|
526
580
|
* @param {Array<string>} addresses
|
|
581
|
+
* @param {Object} signedData
|
|
527
582
|
* @param {?number} maxHeight
|
|
528
583
|
* @param {?string} chainName
|
|
529
584
|
* @param {?string} chainVersion
|
|
@@ -532,6 +587,7 @@ export default class TCaBCIClient {
|
|
|
532
587
|
*/
|
|
533
588
|
Bulk(
|
|
534
589
|
addresses = [],
|
|
590
|
+
signedData = {},
|
|
535
591
|
maxHeight = null,
|
|
536
592
|
chainName = null,
|
|
537
593
|
chainVersion = null,
|
|
@@ -539,120 +595,144 @@ export default class TCaBCIClient {
|
|
|
539
595
|
return this.httpClient('/v1/bulk_tx', {
|
|
540
596
|
method: 'POST',
|
|
541
597
|
body: JSON.stringify({
|
|
542
|
-
chain_name: chainName ?? this.
|
|
543
|
-
chain_version: chainVersion ?? this.
|
|
598
|
+
chain_name: chainName ?? this._chainName,
|
|
599
|
+
chain_version: chainVersion ?? this._chainVersion,
|
|
544
600
|
addresses: addresses,
|
|
601
|
+
signed_addrs: signedData,
|
|
545
602
|
...(maxHeight ? { max_height: maxHeight } : {}),
|
|
546
603
|
}),
|
|
547
604
|
})
|
|
548
605
|
}
|
|
549
606
|
|
|
550
|
-
Disconnect(code = 1000) {
|
|
607
|
+
async Disconnect(code = 1000) {
|
|
551
608
|
if (this.getConnected()) {
|
|
552
|
-
this.
|
|
609
|
+
await this._ws.disconnect(code)
|
|
610
|
+
|
|
611
|
+
this.setConnected(false)
|
|
612
|
+
this.setSubscribed(false)
|
|
553
613
|
}
|
|
554
614
|
}
|
|
555
615
|
|
|
556
|
-
Reconnect(code = 1000) {
|
|
616
|
+
async Reconnect(code = 1000) {
|
|
557
617
|
if (this.getConnected()) {
|
|
558
|
-
this.
|
|
618
|
+
await this.Disconnect(code)
|
|
559
619
|
}
|
|
560
620
|
|
|
621
|
+
await this.connect()
|
|
622
|
+
|
|
561
623
|
return this
|
|
562
624
|
}
|
|
563
625
|
|
|
564
626
|
callSuccessCallback(event) {
|
|
565
|
-
if (this.
|
|
627
|
+
if (this._successCb) this._successCb(event)
|
|
566
628
|
}
|
|
567
629
|
|
|
568
630
|
callErrorCallback(event) {
|
|
569
|
-
if (this.
|
|
631
|
+
if (this._errorCb) this._errorCb(event)
|
|
570
632
|
}
|
|
571
633
|
|
|
572
634
|
callCloseCallback(event) {
|
|
573
|
-
if (this.
|
|
635
|
+
if (this._closeCb) this._closeCb(event)
|
|
574
636
|
}
|
|
575
637
|
|
|
576
638
|
callListenCallback(message) {
|
|
577
|
-
if (this.
|
|
639
|
+
if (this._listenCb) this._listenCb(message)
|
|
578
640
|
}
|
|
579
641
|
|
|
580
642
|
/**
|
|
581
|
-
* @return {Promise<
|
|
643
|
+
* @return {Promise<TWebSocket>}
|
|
582
644
|
*/
|
|
583
645
|
async connect() {
|
|
584
646
|
if (this.getConnected()) {
|
|
585
|
-
return Promise.reject(ALREADY_CONNECTED)
|
|
647
|
+
return Promise.reject(new Error(ALREADY_CONNECTED))
|
|
586
648
|
}
|
|
587
649
|
|
|
588
|
-
const options =
|
|
589
|
-
|
|
590
|
-
connectionTimeout: 1000,
|
|
591
|
-
maxRetries: 10,
|
|
592
|
-
}
|
|
650
|
+
const options = new Options(this._readNodeWSAddress)
|
|
651
|
+
options.setCustomWS(this._wsLibrary)
|
|
593
652
|
|
|
594
|
-
this.
|
|
653
|
+
this._ws = new TWebSocket(options)
|
|
595
654
|
|
|
596
|
-
|
|
597
|
-
this.
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
655
|
+
this._ws.addErrorListener((e) => {
|
|
656
|
+
this.setConnected(false)
|
|
657
|
+
this.setSubscribed(false)
|
|
658
|
+
this.setSubscribeAddresses([], false)
|
|
659
|
+
this.setSubscribeSignedData({})
|
|
660
|
+
this.callErrorCallback(e)
|
|
661
|
+
})
|
|
602
662
|
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
this.
|
|
606
|
-
|
|
607
|
-
this.callSuccessCallback(event)
|
|
663
|
+
this._ws.addOpenListener((e) => {
|
|
664
|
+
this.setConnected(true)
|
|
665
|
+
this.callSuccessCallback(e)
|
|
666
|
+
})
|
|
608
667
|
|
|
609
|
-
|
|
668
|
+
this._ws.addMessageListener((message) => {
|
|
669
|
+
if (message.data === 'OK' && message.data.length < 10) {
|
|
670
|
+
return { status: message.data }
|
|
610
671
|
}
|
|
611
|
-
this.ws.onmessage = (message) => {
|
|
612
|
-
if (message.data === 'OK' && message.data.length < 10) {
|
|
613
|
-
return { status: message.data }
|
|
614
|
-
}
|
|
615
672
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
this.ws.onclose = (event) => {
|
|
619
|
-
this.setConnected(false)
|
|
620
|
-
this.setSubscribed(false)
|
|
621
|
-
this.setSubscribeAddresses([], false)
|
|
622
|
-
this.callCloseCallback(event)
|
|
673
|
+
this.callListenCallback(!isJSON(message.data) ? message.data : toJSON(message.data))
|
|
674
|
+
})
|
|
623
675
|
|
|
624
|
-
|
|
625
|
-
|
|
676
|
+
this._ws.addCloseListener((e) => {
|
|
677
|
+
console.log('came gggg ', e)
|
|
678
|
+
this.setConnected(false)
|
|
679
|
+
this.setSubscribed(false)
|
|
680
|
+
this.setSubscribeAddresses([], false)
|
|
681
|
+
this.setSubscribeSignedData({})
|
|
682
|
+
this.callCloseCallback(e)
|
|
626
683
|
})
|
|
684
|
+
|
|
685
|
+
return this._ws.connect()
|
|
627
686
|
}
|
|
628
687
|
|
|
629
688
|
getConnected() {
|
|
630
|
-
return this.
|
|
689
|
+
return this._connected
|
|
631
690
|
}
|
|
632
691
|
|
|
633
692
|
setConnected(value) {
|
|
634
|
-
this.
|
|
693
|
+
this._connected = value
|
|
635
694
|
}
|
|
636
695
|
|
|
637
696
|
getSubscribed() {
|
|
638
|
-
return this.
|
|
697
|
+
return this._subscribed
|
|
639
698
|
}
|
|
640
699
|
|
|
641
700
|
setSubscribed(value) {
|
|
642
|
-
this.
|
|
701
|
+
this._subscribed = value
|
|
643
702
|
}
|
|
644
703
|
|
|
645
704
|
getSubscribeAddresses() {
|
|
646
|
-
return this.
|
|
705
|
+
return this._subscribedAddresses
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
getSubscribedSignedData() {
|
|
709
|
+
return this._subscribedSignedData
|
|
647
710
|
}
|
|
648
711
|
|
|
712
|
+
/**
|
|
713
|
+
* @param {Array<string>} addresses
|
|
714
|
+
* @param {?boolean} push
|
|
715
|
+
* @return {TCaBCIClient}
|
|
716
|
+
*/
|
|
649
717
|
setSubscribeAddresses(addresses, push = false) {
|
|
650
718
|
if (push) {
|
|
651
|
-
this.
|
|
719
|
+
this._subscribedAddresses.push(...addresses)
|
|
652
720
|
|
|
653
|
-
return
|
|
721
|
+
return this
|
|
654
722
|
}
|
|
655
|
-
this.
|
|
723
|
+
this._subscribedAddresses = addresses
|
|
724
|
+
|
|
725
|
+
return this
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* @param {Object} signedData
|
|
730
|
+
* @return {TCaBCIClient}
|
|
731
|
+
*/
|
|
732
|
+
setSubscribeSignedData(signedData) {
|
|
733
|
+
this._subscribedSignedData = signedData
|
|
734
|
+
|
|
735
|
+
return this
|
|
656
736
|
}
|
|
657
737
|
|
|
658
738
|
/**
|
|
@@ -680,6 +760,7 @@ export default class TCaBCIClient {
|
|
|
680
760
|
/**
|
|
681
761
|
* @param {Error} err
|
|
682
762
|
* @param {?Object|Error} custom
|
|
763
|
+
* @throws Error
|
|
683
764
|
*/
|
|
684
765
|
handleRestError(err, custom = null) {
|
|
685
766
|
const code = err.code
|
|
@@ -692,9 +773,9 @@ export default class TCaBCIClient {
|
|
|
692
773
|
|
|
693
774
|
switch (code) {
|
|
694
775
|
case 400:
|
|
695
|
-
throw INVALID_ARGUMENTS
|
|
776
|
+
throw new Error(INVALID_ARGUMENTS)
|
|
696
777
|
case 'ERR_NETWORK':
|
|
697
|
-
throw ERR_NETWORK
|
|
778
|
+
throw new Error(ERR_NETWORK)
|
|
698
779
|
default:
|
|
699
780
|
throw err
|
|
700
781
|
}
|