Skip to content

Commit

Permalink
fix: replace ws with isomorphic-ws.
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyoung-an committed Apr 10, 2024
1 parent e8d74dd commit 3eb7056
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 29 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test_ain_raw": "jest ain_raw.test.ts",
"test_he": "jest he.test.ts",
"test_em": "jest event_manager.test.ts",
"docs": "yarn build && typedoc --plugin @mxssfd/typedoc-theme --theme my-theme --out docs"
"docs": "yarn build && typedoc --plugin @mxssfd/typedoc-theme --theme my-theme --out docs",
"postinstall": "patch-package"
},
"engines": {
"node": ">=16"
Expand Down Expand Up @@ -59,9 +60,12 @@
"browserify-cipher": "^1.0.1",
"eventemitter3": "^4.0.0",
"hdkey": "^1.1.1",
"isomorphic-ws": "^5.0.0",
"lodash": "^4.17.20",
"node-seal": "^4.5.7",
"patch-package": "^8.0.0",
"pbkdf2": "^3.0.17",
"postinstall-postinstall": "^2.1.0",
"randombytes": "^2.1.0",
"scryptsy": "^2.1.0",
"semver": "^6.3.0",
Expand Down
15 changes: 15 additions & 0 deletions patches/isomorphic-ws+5.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/isomorphic-ws/index.d.ts b/node_modules/isomorphic-ws/index.d.ts
index de923a9..3b57081 100644
--- a/node_modules/isomorphic-ws/index.d.ts
+++ b/node_modules/isomorphic-ws/index.d.ts
@@ -3,6 +3,6 @@
// Fix for https://github.com/heineiuo/isomorphic-ws/issues/8
// If there is still something wrong, welcome issue.

-import WebSocket = require('ws')
+import { WebSocket } from 'ws';

-export = WebSocket
\ No newline at end of file
+export default WebSocket;
\ No newline at end of file
54 changes: 29 additions & 25 deletions src/event-manager/event-channel-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Ain from '../ain';
import { WebSocket } from 'ws';
import WebSocket from 'isomorphic-ws';
import {
EventChannelMessageTypes,
EventChannelMessage,
Expand All @@ -22,7 +22,7 @@ export default class EventChannelClient {
/** The event callback manager object. */
private readonly _eventCallbackManager: EventCallbackManager;
/** The web socket client. */
private _wsClient?: WebSocket;
private _ws?: WebSocket;
/** The blockchain endpoint URL. */
private _endpointUrl?: string;
/** Whether it's connected or not. */
Expand All @@ -38,7 +38,7 @@ export default class EventChannelClient {
constructor(ain: Ain, eventCallbackManager: EventCallbackManager) {
this._ain = ain;
this._eventCallbackManager = eventCallbackManager;
this._wsClient = undefined;
this._ws = undefined;
this._endpointUrl = undefined;
this._isConnected = false;
this._heartbeatTimeout = undefined;
Expand Down Expand Up @@ -85,40 +85,44 @@ export default class EventChannelClient {
}

this._endpointUrl = url;
this._wsClient = new WebSocket(url, [], { handshakeTimeout: connectionOption.handshakeTimeout || DEFAULT_HANDSHAKE_TIMEOUT_MS });
this._wsClient.on('message', (message: string) => {
this.handleMessage(message);
});
this._wsClient.on('error', (err) => {
console.error(err);
this._ws = new WebSocket(url, [], { handshakeTimeout: connectionOption.handshakeTimeout || DEFAULT_HANDSHAKE_TIMEOUT_MS });
this._ws.onmessage = (event: { data: unknown }) => {
if (typeof event.data !== 'string') {
return;
}
this.handleMessage(event.data);
};
this._ws.onerror = async (event: unknown) => {
console.error(event);
this.disconnect();
});
this._wsClient.on('open', () => {
};
this._ws.onopen = () => {
this._isConnected = true;
this.startHeartbeatTimer(connectionOption.heartbeatIntervalMs || DEFAULT_HEARTBEAT_INTERVAL_MS);
resolve(this);
});
this._wsClient.on('ping', () => {
if (this._heartbeatTimeout) {
clearTimeout(this._heartbeatTimeout);
}
this.startHeartbeatTimer(connectionOption.heartbeatIntervalMs || DEFAULT_HEARTBEAT_INTERVAL_MS);
});
this._wsClient.on('close', () => {
};
// NOTE(jiyoung): ping is not supported in browser api
// this._wsClient.on('ping', () => {
// if (this._heartbeatTimeout) {
// clearTimeout(this._heartbeatTimeout);
// }
// this.startHeartbeatTimer(connectionOption.heartbeatIntervalMs || DEFAULT_HEARTBEAT_INTERVAL_MS);
// });
this._ws.onclose = () => {
this.disconnect();
if (disconnectionCallback) {
disconnectionCallback(this._wsClient);
disconnectionCallback(this._ws);
}
});
})
};
});
}

/**
* Closes the current event channel.
*/
disconnect() {
this._isConnected = false;
this._wsClient!.terminate();
this._ws!.close();
if (this._heartbeatTimeout) {
clearTimeout(this._heartbeatTimeout);
this._heartbeatTimeout = null;
Expand All @@ -132,7 +136,7 @@ export default class EventChannelClient {
startHeartbeatTimer(timeoutMs: number) {
this._heartbeatTimeout = setTimeout(() => {
console.log(`Connection timeout! Terminate the connection. All event subscriptions are stopped.`);
this._wsClient!.terminate();
this._ws!.terminate();
}, timeoutMs);
}

Expand Down Expand Up @@ -231,7 +235,7 @@ export default class EventChannelClient {
if (!this._isConnected) {
throw Error(`Failed to send message. Event channel is not connected!`);
}
this._wsClient!.send(JSON.stringify(message));
this._ws!.send(JSON.stringify(message));
}

/**
Expand Down
Loading

0 comments on commit 3eb7056

Please sign in to comment.