Skip to content

Commit

Permalink
Implement heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr committed Sep 25, 2024
1 parent e705177 commit 65b5dbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libshv-js",
"version": "3.3.5",
"version": "3.3.6",
"description": "Typescript implementation of libshv",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
18 changes: 17 additions & 1 deletion src/ws-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessage, type RpcResponse, Meth
import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap} from './rpcvalue';

const DEFAULT_TIMEOUT = 5000;
const DEFAULT_PING_INTERVAL = 30 * 1000;

const dataToRpcValue = (buff: ArrayBuffer) => {
const rd: ChainPackReader | CponReader = new ChainPackReader(buff);
Expand Down Expand Up @@ -39,6 +40,7 @@ type WsClientOptions = {
password: string;
loginType?: 'PLAIN' | 'AZURE';
timeout?: number;
pingInterval?: number;
wsUri: string;
onConnected: () => void;
onConnectionFailure: (error: Error) => void;
Expand Down Expand Up @@ -74,6 +76,8 @@ type DirResult = Array<IMap<{

class WsClient {
requestId = 1;
pingTimerId = -1;

rpcHandlers: Array<{
resolve: RpcResponseResolver;
timeout_handle: number;
Expand All @@ -92,6 +96,7 @@ class WsClient {
onDisconnected: WsClientOptions['onDisconnected'];
onRequest: WsClientOptions['onRequest'];
timeout: WsClientOptions['timeout'];
pingInterval: WsClientOptions['pingInterval'];

constructor(options: WsClientOptions) {
if (typeof options !== 'object') {
Expand All @@ -108,6 +113,7 @@ class WsClient {
this.websocket = new WebSocket(options.wsUri);
this.websocket.binaryType = 'arraybuffer';

this.pingInterval = options.pingInterval ?? DEFAULT_PING_INTERVAL;
this.onConnected = options.onConnected ?? (() => {/* nothing */});
this.onConnectionFailure = options.onConnectionFailure ?? (() => {/* nothing */});
this.onDisconnected = options.onDisconnected ?? (() => {/* nothing */});
Expand Down Expand Up @@ -144,6 +150,7 @@ class WsClient {
}
this.logDebug('SUCCESS: connected to shv broker');
this.onConnected();
this.pingTimerId = window.setInterval(() => this.sendPing(), this.pingInterval);
}).catch(() => {
this.logDebug('FAILURE: couldn\' connected to shv broker');
});
Expand All @@ -153,6 +160,7 @@ class WsClient {
this.logDebug('DISCONNECTED');
this.subscriptions.length = 0;
this.onDisconnected();
window.clearInterval(this.pingTimerId);
});

this.websocket.addEventListener('message', evt => {
Expand Down Expand Up @@ -223,7 +231,7 @@ class WsClient {
}

sendRpcMessage(rpc_msg: RpcMessage) {
if (this.websocket && this.websocket.readyState === 1) {
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
this.logDebug('sending rpc message:', rpc_msg.toCpon());
const msg_data = new Uint8Array(rpc_msg.toChainPack());

Expand Down Expand Up @@ -284,6 +292,14 @@ class WsClient {
});
}

sendPing() {
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
this.callRpcMethod('.broker/app', 'ping').catch((error: unknown) => {
console.log('Failed to send ping:', error);
});
}
}

accessGrantForMethodCall(path: string, method: string) {
return this.callRpcMethod('.broker/currentClient', 'accessGrantForMethodCall', [path, method]);
}
Expand Down

0 comments on commit 65b5dbe

Please sign in to comment.