libshv-js 3.3.5 → 3.3.6
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/package.json +1 -1
- package/src/ws-client.ts +17 -1
package/package.json
CHANGED
package/src/ws-client.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessage, type RpcResponse, Meth
|
|
|
4
4
|
import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap} from './rpcvalue';
|
|
5
5
|
|
|
6
6
|
const DEFAULT_TIMEOUT = 5000;
|
|
7
|
+
const DEFAULT_PING_INTERVAL = 30 * 1000;
|
|
7
8
|
|
|
8
9
|
const dataToRpcValue = (buff: ArrayBuffer) => {
|
|
9
10
|
const rd: ChainPackReader | CponReader = new ChainPackReader(buff);
|
|
@@ -39,6 +40,7 @@ type WsClientOptions = {
|
|
|
39
40
|
password: string;
|
|
40
41
|
loginType?: 'PLAIN' | 'AZURE';
|
|
41
42
|
timeout?: number;
|
|
43
|
+
pingInterval?: number;
|
|
42
44
|
wsUri: string;
|
|
43
45
|
onConnected: () => void;
|
|
44
46
|
onConnectionFailure: (error: Error) => void;
|
|
@@ -74,6 +76,8 @@ type DirResult = Array<IMap<{
|
|
|
74
76
|
|
|
75
77
|
class WsClient {
|
|
76
78
|
requestId = 1;
|
|
79
|
+
pingTimerId = -1;
|
|
80
|
+
|
|
77
81
|
rpcHandlers: Array<{
|
|
78
82
|
resolve: RpcResponseResolver;
|
|
79
83
|
timeout_handle: number;
|
|
@@ -92,6 +96,7 @@ class WsClient {
|
|
|
92
96
|
onDisconnected: WsClientOptions['onDisconnected'];
|
|
93
97
|
onRequest: WsClientOptions['onRequest'];
|
|
94
98
|
timeout: WsClientOptions['timeout'];
|
|
99
|
+
pingInterval: WsClientOptions['pingInterval'];
|
|
95
100
|
|
|
96
101
|
constructor(options: WsClientOptions) {
|
|
97
102
|
if (typeof options !== 'object') {
|
|
@@ -108,6 +113,7 @@ class WsClient {
|
|
|
108
113
|
this.websocket = new WebSocket(options.wsUri);
|
|
109
114
|
this.websocket.binaryType = 'arraybuffer';
|
|
110
115
|
|
|
116
|
+
this.pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
|
|
111
117
|
this.onConnected = options.onConnected ?? (() => {/* nothing */});
|
|
112
118
|
this.onConnectionFailure = options.onConnectionFailure ?? (() => {/* nothing */});
|
|
113
119
|
this.onDisconnected = options.onDisconnected ?? (() => {/* nothing */});
|
|
@@ -144,6 +150,9 @@ class WsClient {
|
|
|
144
150
|
}
|
|
145
151
|
this.logDebug('SUCCESS: connected to shv broker');
|
|
146
152
|
this.onConnected();
|
|
153
|
+
this.pingTimerId = window.setInterval(() => {
|
|
154
|
+
this.sendPing();
|
|
155
|
+
}, this.pingInterval);
|
|
147
156
|
}).catch(() => {
|
|
148
157
|
this.logDebug('FAILURE: couldn\' connected to shv broker');
|
|
149
158
|
});
|
|
@@ -153,6 +162,7 @@ class WsClient {
|
|
|
153
162
|
this.logDebug('DISCONNECTED');
|
|
154
163
|
this.subscriptions.length = 0;
|
|
155
164
|
this.onDisconnected();
|
|
165
|
+
window.clearInterval(this.pingTimerId);
|
|
156
166
|
});
|
|
157
167
|
|
|
158
168
|
this.websocket.addEventListener('message', evt => {
|
|
@@ -223,7 +233,7 @@ class WsClient {
|
|
|
223
233
|
}
|
|
224
234
|
|
|
225
235
|
sendRpcMessage(rpc_msg: RpcMessage) {
|
|
226
|
-
if (this.websocket && this.websocket.readyState ===
|
|
236
|
+
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
|
|
227
237
|
this.logDebug('sending rpc message:', rpc_msg.toCpon());
|
|
228
238
|
const msg_data = new Uint8Array(rpc_msg.toChainPack());
|
|
229
239
|
|
|
@@ -284,6 +294,12 @@ class WsClient {
|
|
|
284
294
|
});
|
|
285
295
|
}
|
|
286
296
|
|
|
297
|
+
sendPing() {
|
|
298
|
+
this.callRpcMethod('.broker/app', 'ping').catch((error: unknown) => {
|
|
299
|
+
console.log('Failed to send ping:', error);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
287
303
|
accessGrantForMethodCall(path: string, method: string) {
|
|
288
304
|
return this.callRpcMethod('.broker/currentClient', 'accessGrantForMethodCall', [path, method]);
|
|
289
305
|
}
|