From 0c120cb7a3a7796504e577a99452ea8909989cfc Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 29 May 2018 10:38:08 -0700 Subject: Assert that connection is opening before attempting to subscribe --- packages/connect/src/ws_orderbook_channel.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) (limited to 'packages/connect') diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts index f90d9ac30..45d1b35d7 100644 --- a/packages/connect/src/ws_orderbook_channel.ts +++ b/packages/connect/src/ws_orderbook_channel.ts @@ -51,6 +51,7 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { public subscribe(subscriptionOpts: OrderbookChannelSubscriptionOpts, handler: OrderbookChannelHandler): void { assert.isOrderbookChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); assert.isOrderbookChannelHandler('handler', handler); + assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); const newSubscription: Subscription = { subscriptionOpts, handler, @@ -62,7 +63,7 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { requestId: this._subscriptions.length - 1, payload: subscriptionOpts, }; - this._sendMessage(subscribeMessage); + this._client.send(JSON.stringify(subscribeMessage)); } /** * Close the websocket and stop receiving updates @@ -70,14 +71,6 @@ export class WebSocketOrderbookChannel implements OrderbookChannel { public close(): void { this._client.close(); } - /** - * Send a message to the client if it has been instantiated and it is open - */ - private _sendMessage(message: any): void { - if (this._client.readyState === WebSocket.w3cwebsocket.OPEN) { - this._client.send(JSON.stringify(message)); - } - } /** * For use in cases where we need to alert all handlers of an error */ -- cgit v1.2.3