Skip to content

Commit

Permalink
Add client wsError handler
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed May 8, 2022
1 parent f6fa9cd commit 9fe47bd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 12 deletions.
54 changes: 50 additions & 4 deletions dist/nkn.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Client {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: []
};
this.sigChainBlockHash = null;
Expand Down Expand Up @@ -231,6 +232,15 @@ class Client {
this.eventListeners.connectFailed.push(func);
}

/**
* Add event listener function that will be called when client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/
onWsError(func) {
this.eventListeners.wsError.push(func);
}

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand Down Expand Up @@ -755,8 +765,18 @@ class Client {
}
};

ws.onerror = err => {
console.log(err.message);
ws.onerror = event => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
};
}

Expand Down Expand Up @@ -10317,6 +10337,7 @@ class MultiClient {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: [],
session: []
};
Expand Down Expand Up @@ -10454,6 +10475,22 @@ class MultiClient {
console.log('All clients connect failed');
}
});

Object.keys(this.clients).map(clientID => {
this.clients[clientID].onWsError(event => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
});
});
}
/**
* Get the secret seed of the client.
Expand Down Expand Up @@ -10658,6 +10695,17 @@ class MultiClient {
onConnectFailed(func) {
this.eventListeners.connectFailed.push(func);
}
/**
* Add event listener function that will be called when any client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/


onWsError(func) {
this.eventListeners.wsError.push(func);
}

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand All @@ -10670,8 +10718,6 @@ class MultiClient {
* msg received will be sent back. Receiving reply or ACK will not trigger
* the event listener.
*/


onMessage(func) {
this.eventListeners.message.push(func);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/nkn.min.js

Large diffs are not rendered by default.

24 changes: 22 additions & 2 deletions lib/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Client {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: []
};
this.sigChainBlockHash = null;
Expand Down Expand Up @@ -230,6 +231,15 @@ class Client {
this.eventListeners.connectFailed.push(func);
}

/**
* Add event listener function that will be called when client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/
onWsError(func) {
this.eventListeners.wsError.push(func);
}

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand Down Expand Up @@ -754,8 +764,18 @@ class Client {
}
};

ws.onerror = err => {
console.log(err.message);
ws.onerror = event => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
};
}

Expand Down
30 changes: 28 additions & 2 deletions lib/multiclient/multiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class MultiClient {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: [],
session: []
};
Expand Down Expand Up @@ -285,6 +286,22 @@ class MultiClient {
console.log('All clients connect failed');
}
});

Object.keys(this.clients).map(clientID => {
this.clients[clientID].onWsError(event => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
});
});
}
/**
* Get the secret seed of the client.
Expand Down Expand Up @@ -489,6 +506,17 @@ class MultiClient {
onConnectFailed(func) {
this.eventListeners.connectFailed.push(func);
}
/**
* Add event listener function that will be called when any client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/


onWsError(func) {
this.eventListeners.wsError.push(func);
}

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand All @@ -501,8 +529,6 @@ class MultiClient {
* msg received will be sent back. Receiving reply or ACK will not trigger
* the event listener.
*/


onMessage(func) {
this.eventListeners.message.push(func);
}
Expand Down
30 changes: 28 additions & 2 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class Client {
eventListeners: {
connect: Array<ConnectHandler>,
connectFailed: Array<ConnectFailedHandler>,
wsError: Array<WsErrorHandler>,
message: Array<MessageHandler>,
};
sigChainBlockHash: string | null;
Expand Down Expand Up @@ -101,6 +102,7 @@ export default class Client {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: [],
};
this.sigChainBlockHash = null;
Expand Down Expand Up @@ -210,6 +212,15 @@ export default class Client {
this.eventListeners.connectFailed.push(func);
};

/**
* Add event listener function that will be called when client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/
onWsError(func: WsErrorHandler) {
this.eventListeners.wsError.push(func);
};

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand Down Expand Up @@ -658,8 +669,18 @@ export default class Client {
}
};

ws.onerror = (err) => {
console.log(err.message);
ws.onerror = (event) => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
}
}

Expand Down Expand Up @@ -1059,6 +1080,11 @@ export type ConnectFailedHandler = () => void;
*/
export type MessageHandler = (Message) => ReplyData | false | void | Promise<ReplyData | false | void>;

/**
* Websocket error handler function type.
*/
export type WsErrorHandler = (Event) => void;

/**
* Send message options type.
* @property {number} [responseTimeout] - Message response timeout in ms. Zero disables timeout.
Expand Down
29 changes: 28 additions & 1 deletion src/multiclient/multiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as consts from './consts';
import * as message from '../client/message';
import * as util from './util';

import type { ConnectHandler, ConnectFailedHandler, MessageHandler, Destination, MessageData, ReplyData, SendOptions, PublishOptions } from '../client';
import type { ConnectHandler, ConnectFailedHandler, MessageHandler, WsErrorHandler, Destination, MessageData, ReplyData, SendOptions, PublishOptions } from '../client';
import type { CreateTransactionOptions, TransactionOptions, TxnOrHash } from '../wallet';

/**
Expand Down Expand Up @@ -63,6 +63,7 @@ export default class MultiClient {
eventListeners: {
connect: Array<ConnectHandler>,
connectFailed: Array<ConnectFailedHandler>,
wsError: Array<WsErrorHandler>,
message: Array<MessageHandler>,
session: Array<SessionHandler>,
};
Expand Down Expand Up @@ -140,6 +141,7 @@ export default class MultiClient {
this.eventListeners = {
connect: [],
connectFailed: [],
wsError: [],
message: [],
session: [],
};
Expand Down Expand Up @@ -256,6 +258,22 @@ export default class MultiClient {
console.log('All clients connect failed');
}
});

Object.keys(this.clients).map(clientID => {
this.clients[clientID].onWsError((event) => {
if (this.eventListeners.wsError.length > 0) {
this.eventListeners.wsError.forEach(async f => {
try {
await f(event);
} catch (e) {
console.log('WsError handler error:', e);
}
});
} else {
console.log(event.message);
}
});
});
}

/**
Expand Down Expand Up @@ -430,6 +448,15 @@ export default class MultiClient {
this.eventListeners.connectFailed.push(func);
}

/**
* Add event listener function that will be called when any client websocket
* connection throws an error. Multiple listeners will be called sequentially
* in the order of added.
*/
onWsError(func: WsErrorHandler) {
this.eventListeners.wsError.push(func);
};

/**
* Add event listener function that will be called when client receives a
* message. Multiple listeners will be called sequentially in the order of
Expand Down

0 comments on commit 9fe47bd

Please sign in to comment.