From 64a85dfb9cc60212dcf1c70ca7be4874936dd332 Mon Sep 17 00:00:00 2001 From: fragosti Date: Thu, 16 Aug 2018 17:03:11 -0700 Subject: Rename websocket files and types --- packages/connect/src/ws_orders_channel.ts | 101 ++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 packages/connect/src/ws_orders_channel.ts (limited to 'packages/connect/src/ws_orders_channel.ts') diff --git a/packages/connect/src/ws_orders_channel.ts b/packages/connect/src/ws_orders_channel.ts new file mode 100644 index 000000000..9d45b6570 --- /dev/null +++ b/packages/connect/src/ws_orders_channel.ts @@ -0,0 +1,101 @@ +import * as _ from 'lodash'; +import { v4 as uuid } from 'uuid'; +import * as WebSocket from 'websocket'; + +import { + OrdersChannel, + OrdersChannelHandler, + OrdersChannelMessageTypes, + OrdersChannelSubscriptionOpts, +} from './types'; +import { assert } from './utils/assert'; +import { ordersChannelMessageParser } from './utils/orderbook_channel_message_parser'; + +/** + * This class includes all the functionality related to interacting with a websocket endpoint + * that implements the standard relayer API v0 + */ +export class WebSocketOrdersChannel implements OrdersChannel { + private readonly _client: WebSocket.w3cwebsocket; + private readonly _handler: OrdersChannelHandler; + private readonly _subscriptionOptsList: OrdersChannelSubscriptionOpts[] = []; + /** + * Instantiates a new WebSocketOrdersChannel instance + * @param client A WebSocket client + * @param handler An OrdersChannelHandler instance that responds to various + * channel updates + * @return An instance of WebSocketOrdersChannel + */ + constructor(client: WebSocket.w3cwebsocket, handler: OrdersChannelHandler) { + assert.isOrdersChannelHandler('handler', handler); + // set private members + this._client = client; + this._handler = handler; + // attach client callbacks + this._client.onerror = err => { + this._handler.onError(this, err); + }; + this._client.onclose = () => { + this._handler.onClose(this); + }; + this._client.onmessage = message => { + this._handleWebSocketMessage(message); + }; + } + /** + * Subscribe to orderbook snapshots and updates from the websocket + * @param subscriptionOpts An OrdersChannelSubscriptionOpts instance describing which + * assetData pair to subscribe to + */ + public subscribe(subscriptionOpts: OrdersChannelSubscriptionOpts): void { + assert.isOrdersChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); + assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); + this._subscriptionOptsList.push(subscriptionOpts); + const subscribeMessage = { + type: 'subscribe', + channel: 'orders', + requestId: uuid(), + payload: subscriptionOpts, + }; + this._client.send(JSON.stringify(subscribeMessage)); + } + /** + * Close the websocket and stop receiving updates + */ + public close(): void { + this._client.close(); + } + private _handleWebSocketMessage(message: any): void { + if (_.isUndefined(message.data)) { + this._handler.onError(this, new Error(`Message does not contain data. Url: ${this._client.url}`)); + return; + } + try { + const data = message.data; + const parserResult = ordersChannelMessageParser.parse(data); + const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId]; + if (_.isUndefined(subscriptionOpts)) { + this._handler.onError( + this, + new Error(`Message has unknown requestId. Url: ${this._client.url} Message: ${data}`), + ); + return; + } + switch (parserResult.type) { + case OrdersChannelMessageTypes.Update: { + this._handler.onUpdate(this, subscriptionOpts, parserResult.payload); + break; + } + default: { + this._handler.onError( + this, + new Error(`Message has unknown type parameter. Url: ${this._client.url} Message: ${data}`), + subscriptionOpts, + ); + } + } + } catch (error) { + this._handler.onError(this, error); + } + } +} -- cgit v1.2.3 From 075e3a41c876797907e3ad98f20940e32e8d0762 Mon Sep 17 00:00:00 2001 From: fragosti Date: Mon, 20 Aug 2018 11:42:29 -0700 Subject: Update websocket for SRA v2 --- packages/connect/src/ws_orders_channel.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'packages/connect/src/ws_orders_channel.ts') diff --git a/packages/connect/src/ws_orders_channel.ts b/packages/connect/src/ws_orders_channel.ts index 9d45b6570..62960d23a 100644 --- a/packages/connect/src/ws_orders_channel.ts +++ b/packages/connect/src/ws_orders_channel.ts @@ -9,7 +9,11 @@ import { OrdersChannelSubscriptionOpts, } from './types'; import { assert } from './utils/assert'; -import { ordersChannelMessageParser } from './utils/orderbook_channel_message_parser'; +import { ordersChannelMessageParser } from './utils/orders_channel_message_parser'; + +export interface OrdersChannelSubscriptionOptsMap { + [key: string]: OrdersChannelSubscriptionOpts; +} /** * This class includes all the functionality related to interacting with a websocket endpoint @@ -18,7 +22,7 @@ import { ordersChannelMessageParser } from './utils/orderbook_channel_message_pa export class WebSocketOrdersChannel implements OrdersChannel { private readonly _client: WebSocket.w3cwebsocket; private readonly _handler: OrdersChannelHandler; - private readonly _subscriptionOptsList: OrdersChannelSubscriptionOpts[] = []; + private readonly _subscriptionOptsMap: OrdersChannelSubscriptionOptsMap = {}; /** * Instantiates a new WebSocketOrdersChannel instance * @param client A WebSocket client @@ -50,11 +54,12 @@ export class WebSocketOrdersChannel implements OrdersChannel { public subscribe(subscriptionOpts: OrdersChannelSubscriptionOpts): void { assert.isOrdersChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); - this._subscriptionOptsList.push(subscriptionOpts); + const requestId = uuid(); + this._subscriptionOptsMap[requestId] = subscriptionOpts; const subscribeMessage = { type: 'subscribe', channel: 'orders', - requestId: uuid(), + requestId, payload: subscriptionOpts, }; this._client.send(JSON.stringify(subscribeMessage)); @@ -73,7 +78,7 @@ export class WebSocketOrdersChannel implements OrdersChannel { try { const data = message.data; const parserResult = ordersChannelMessageParser.parse(data); - const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId]; + const subscriptionOpts = this._subscriptionOptsMap[parserResult.requestId]; if (_.isUndefined(subscriptionOpts)) { this._handler.onError( this, -- cgit v1.2.3 From 44cc5e45cc3a3ed7db2691a287500e5d61a2d0c1 Mon Sep 17 00:00:00 2001 From: fragosti Date: Mon, 20 Aug 2018 11:53:49 -0700 Subject: Run prettier --- packages/connect/src/ws_orders_channel.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'packages/connect/src/ws_orders_channel.ts') diff --git a/packages/connect/src/ws_orders_channel.ts b/packages/connect/src/ws_orders_channel.ts index 62960d23a..cde4acbc3 100644 --- a/packages/connect/src/ws_orders_channel.ts +++ b/packages/connect/src/ws_orders_channel.ts @@ -2,12 +2,7 @@ import * as _ from 'lodash'; import { v4 as uuid } from 'uuid'; import * as WebSocket from 'websocket'; -import { - OrdersChannel, - OrdersChannelHandler, - OrdersChannelMessageTypes, - OrdersChannelSubscriptionOpts, -} from './types'; +import { OrdersChannel, OrdersChannelHandler, OrdersChannelMessageTypes, OrdersChannelSubscriptionOpts } from './types'; import { assert } from './utils/assert'; import { ordersChannelMessageParser } from './utils/orders_channel_message_parser'; -- cgit v1.2.3