diff --git a/__tests__/event_manager.test.ts b/__tests__/event_manager.test.ts index e85256b..20f95d3 100644 --- a/__tests__/event_manager.test.ts +++ b/__tests__/event_manager.test.ts @@ -12,6 +12,7 @@ jest.setTimeout(180000); describe('Event Handler', function() { const ain = new Ain(test_node_3, test_event_handler_node); + const customClientId = 'myCustomClientId'; let eventFilterId: string; let connectionCount = 0; let disconnectionCount = 0; @@ -28,7 +29,7 @@ describe('Event Handler', function() { expect(connectionCount).toBe(0); expect(disconnectionCount).toBe(0); - await ain.em.connect(connectionCb, disconnectionCb); + await ain.em.connect(connectionCb, disconnectionCb, customClientId); expect(connectionCount).toBe(1); expect(disconnectionCount).toBe(0); @@ -59,6 +60,12 @@ describe('Event Handler', function() { }); }); + describe('Custom client id setting', () => { + it('getCustomClientId()', async () => { + expect(ain.em.getCustomClientId()).toBe(customClientId); + }); + }); + describe('BLOCK_FINALIZED', () => { it('Subscribe to BLOCK_FINALIZED', (done) => { eventFilterId = ain.em.subscribe('BLOCK_FINALIZED', { diff --git a/package.json b/package.json index e692962..b09ab46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ainblockchain/ain-js", - "version": "1.12.0", + "version": "1.13.0", "description": "", "main": "lib/ain.js", "scripts": { diff --git a/src/event-manager/event-channel-client.ts b/src/event-manager/event-channel-client.ts index c6534af..38f87ae 100644 --- a/src/event-manager/event-channel-client.ts +++ b/src/event-manager/event-channel-client.ts @@ -27,6 +27,8 @@ export default class EventChannelClient { private _ws?: WebSocket | WebSocketBE; /** Whether it's connected or not. */ private _isConnected: boolean; + /** The custom client id of the event channel. */ + private _customClientId: string; /** The handshake timeout object. */ private _handshakeTimeout?: ReturnType | null; /** The heartbeat timeout object. */ @@ -42,6 +44,7 @@ export default class EventChannelClient { this._eventCallbackManager = eventCallbackManager; this._ws = undefined; this._isConnected = false; + this._customClientId = ''; this._handshakeTimeout = undefined; this._heartbeatTimeout = undefined; } @@ -54,9 +57,10 @@ export default class EventChannelClient { * Opens a new event channel. * @param {ConnectionCallback} connectionCallback The connection callback function. * @param {DisconnectionCallback} disconnectionCallback The disconnection callback function. + * @param {string} customClientId The custom client id to set. * @returns {Promise} A promise for the connection success. */ - connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback): Promise { + connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string): Promise { return new Promise(async (resolve, reject) => { if (this.isConnected) { reject(new Error(`Can't connect multiple channels`)); @@ -139,6 +143,10 @@ export default class EventChannelClient { } // Heartbeat timeout this.startHeartbeatTimer(DEFAULT_HEARTBEAT_INTERVAL_MS); + // Custom client id + if (customClientId) { + this.setCustomClientId(customClientId); + } // Connection callback if (connectionCallback) { connectionCallback(this._ws); @@ -291,30 +299,48 @@ export default class EventChannelClient { } /** - * Sends a register-event-filter messsage to the event channel. + * Sends a SET_CUSTOM_CLIENT_ID messsage to the event channel. + * @param {string} customClientId The custom client id to set. + */ + setCustomClientId(customClientId: string) { + this._customClientId = customClientId; + const data = { customClientId }; + const message = this.buildMessage(EventChannelMessageTypes.SET_CUSTOM_CLIENT_ID, data); + this.sendMessage(message); + } + + /** + * Returns the custom client id saved on the client side. + */ + getCustomClientId() { + return this._customClientId; + } + + /** + * Sends a REGISTER_FILTER messsage to the event channel. * @param {EventFilter} filter The event filter to register. */ registerFilter(filter: EventFilter) { - const filterObj = filter.toObject(); - const registerMessage = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, filterObj); - this.sendMessage(registerMessage); + const data = filter.toObject(); + const message = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, data); + this.sendMessage(message); } /** - * Sends a deregister-event-filter messsage to the event channel. + * Sends a DEREGISTER_FILTER messsage to the event channel. * @param {EventFilter} filter The event filter to deregister. */ deregisterFilter(filter: EventFilter) { - const filterObj = filter.toObject(); - const deregisterMessage = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, filterObj); - this.sendMessage(deregisterMessage); + const data = filter.toObject(); + const message = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, data); + this.sendMessage(message); } /** - * Sends a pong message. + * Sends a PONG message. */ sendPong() { - const pongMessage = this.buildMessage(EventChannelMessageTypes.PONG, {}); - this.sendMessage(pongMessage); + const message = this.buildMessage(EventChannelMessageTypes.PONG, {}); + this.sendMessage(message); } } diff --git a/src/event-manager/index.ts b/src/event-manager/index.ts index 4ca1363..3d85b8e 100644 --- a/src/event-manager/index.ts +++ b/src/event-manager/index.ts @@ -47,9 +47,10 @@ export default class EventManager { * Opens a new event channel. * @param {ConnectionCallback} ConnectionCallback The connection callback function. * @param {DisconnectionCallback} disconnectionCallback The disconnection callback function. + * @param {string} customClientId The custom client id to set. */ - async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback) { - await this._eventChannelClient.connect(connectionCallback, disconnectionCallback); + async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string) { + await this._eventChannelClient.connect(connectionCallback, disconnectionCallback, customClientId); } /** @@ -59,6 +60,13 @@ export default class EventManager { this._eventChannelClient.disconnect(); } + /** + * Returns the custom client id of the event channel saved on the client side. + */ + getCustomClientId() { + return this._eventChannelClient.getCustomClientId(); + } + subscribe( eventType: 'BLOCK_FINALIZED', config: BlockFinalizedEventConfig, diff --git a/src/types.ts b/src/types.ts index 8a3e7e5..4ccefc3 100755 --- a/src/types.ts +++ b/src/types.ts @@ -346,6 +346,7 @@ export enum BlockchainEventTypes { * Event channel message types for blockchain event handler. */ export enum EventChannelMessageTypes { + SET_CUSTOM_CLIENT_ID = 'SET_CUSTOM_CLIENT_ID', REGISTER_FILTER = 'REGISTER_FILTER', DEREGISTER_FILTER = 'DEREGISTER_FILTER', EMIT_EVENT = 'EMIT_EVENT', @@ -497,9 +498,9 @@ export type FilterDeletedEventCallback = (event: FilterDeletedEvent) => void; /** * A type for connection callback functions (blockchain event handler). */ -export type ConnectionCallback = (webSocket) => void; +export type ConnectionCallback = (webSocket: any) => void; /** * A type for disconnection callback functions (blockchain event handler). */ -export type DisconnectionCallback = (webSocket) => void; +export type DisconnectionCallback = (webSocket: any) => void;