Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
✨ Add ws client with connect and disconnect interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Nov 11, 2020
1 parent fb7df00 commit 0725d24
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 14 deletions.
4 changes: 3 additions & 1 deletion elements/lisk-api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
"dependencies": {
"@liskhq/lisk-codec": "^0.1.0-alpha.0",
"@liskhq/lisk-cryptography": "^3.0.0-alpha.0",
"isomorphic-ws": "4.0.1",
"pm2-axon": "4.0.0",
"pm2-axon-rpc": "0.6.0"
"pm2-axon-rpc": "0.6.0",
"ws": "7.4.0"
},
"devDependencies": {
"@liskhq/lisk-chain": "^0.2.0-alpha.2",
Expand Down
26 changes: 26 additions & 0 deletions elements/lisk-api-client/src/create_ws_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2020 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/

import { WSClient } from './ws_client';

export const createWSClient = async (url: string, autoConnect = true): Promise<WSClient> => {
const client = new WSClient(url);

if (autoConnect) {
await client.connect();
}

return client;
};
3 changes: 2 additions & 1 deletion elements/lisk-api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
export { createWSClient } from './ws_client';

export { createClient } from './client';
export { createAPIClient } from './create_api_client';
export { createWSClient } from './create_ws_client';
74 changes: 62 additions & 12 deletions elements/lisk-api-client/src/ws_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,65 @@
*
*/

import { APIClient } from './api_client';
import { Channel } from './types';

export const createWSClient = async (url: string): Promise<APIClient> => {
// FIXME: requires real implementation
const channel = ({ url } as unknown) as Channel;
await channel.connect();
const client = new APIClient(channel);
await client.init();

return client;
};
import * as WebSocket from 'isomorphic-ws';

const CONNECTION_TIMEOUT = 2000;
const ACKNOWLEDGMENT_TIMEOUT = 2000;

const timeout = async (ms: number, message?: string): Promise<void> =>
new Promise((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(message ?? `Timed out in ${ms}ms.`);
}, ms);
});

export class WSClient {
public isAlive = false;
private readonly _url: string;
private _ws?: WebSocket;

public constructor(url: string) {
this._url = url;
}

public async connect(): Promise<void> {
this._ws = new WebSocket(this._url);

const connect = new Promise<void>(resolve => {
this._ws?.on('open', () => {
this.isAlive = true;
resolve();
});
});

this._ws.on('ping', () => {
this.isAlive = true;
});

return Promise.race([
connect,
timeout(CONNECTION_TIMEOUT, `Could not connect in ${CONNECTION_TIMEOUT}ms`),
]);
}

public async disconnect(): Promise<void> {
if (!this._ws) {
return Promise.resolve();
}

return new Promise<void>(resolve => {
this._ws?.on('close', () => {
this.isAlive = false;
this._ws = undefined;
resolve();
});
this._ws?.close();
});
}

// public async invoke<T>(actionName: string, params?: Record<string, unknown>): Promise<T> {
// }

// public subscribe(eventName: string, cb: EventCallback): void {}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6807,6 +6807,11 @@ isobject@^3.0.0, isobject@^3.0.1:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=

isomorphic-ws@4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/isomorphic-ws/-/isomorphic-ws-4.0.1.tgz#55fd4cd6c5e6491e76dc125938dd863f5cd4f2dc"
integrity sha512-BhBvN2MBpWTaSHdWRb/bwdZJ1WaehQ2L1KngkCkfLUGF0mAWAT1sQUQacEmQ0jXkFw/czDXPNQSL5u2/Krsz1w==

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -12258,6 +12263,11 @@ ws@7.3.1:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.1.tgz#d0547bf67f7ce4f12a72dfe31262c68d7dc551c8"
integrity sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==

ws@7.4.0:
version "7.4.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.0.tgz#a5dd76a24197940d4a8bb9e0e152bb4503764da7"
integrity sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ==

ws@^7.2.3:
version "7.3.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.0.tgz#4b2f7f219b3d3737bc1a2fbf145d825b94d38ffd"
Expand Down

0 comments on commit 0725d24

Please sign in to comment.