diff options
Diffstat (limited to 'packages/connect/src')
-rw-r--r-- | packages/connect/src/browser_ws_orderbook_channel.ts | 49 | ||||
-rw-r--r-- | packages/connect/src/index.ts | 1 | ||||
-rw-r--r-- | packages/connect/src/orderbook_channel_factory.ts | 33 |
3 files changed, 53 insertions, 30 deletions
diff --git a/packages/connect/src/browser_ws_orderbook_channel.ts b/packages/connect/src/browser_ws_orderbook_channel.ts index b97a82ec9..599b4f0be 100644 --- a/packages/connect/src/browser_ws_orderbook_channel.ts +++ b/packages/connect/src/browser_ws_orderbook_channel.ts @@ -1,5 +1,4 @@ import * as _ from 'lodash'; -import * as WebSocket from 'websocket'; import { OrderbookChannel, @@ -22,17 +21,16 @@ interface Subscription { * that implements the standard relayer API v0 in a browser environment */ export class BrowserWebSocketOrderbookChannel implements OrderbookChannel { - private _apiEndpointUrl: string; - private _clientIfExists?: WebSocket.w3cwebsocket; + private _client: WebSocket; private _subscriptions: Subscription[] = []; /** * Instantiates a new WebSocketOrderbookChannel instance * @param url The relayer API base WS url you would like to interact with * @return An instance of WebSocketOrderbookChannel */ - constructor(url: string) { - assert.isUri('url', url); - this._apiEndpointUrl = url; + constructor(client: WebSocket) { + // assert.isUri('url', url); + this._client = client; } /** * Subscribe to orderbook snapshots and updates from the websocket @@ -55,40 +53,31 @@ export class BrowserWebSocketOrderbookChannel implements OrderbookChannel { requestId: this._subscriptions.length - 1, payload: subscriptionOpts, }; - if (_.isUndefined(this._clientIfExists)) { - this._clientIfExists = new WebSocket.w3cwebsocket(this._apiEndpointUrl); - this._clientIfExists.onopen = () => { - this._sendMessage(subscribeMessage); - }; - this._clientIfExists.onerror = error => { - this._alertAllHandlersToError(error); - }; - this._clientIfExists.onclose = () => { - _.forEach(this._subscriptions, subscription => { - subscription.handler.onClose(this, subscription.subscriptionOpts); - }); - }; - this._clientIfExists.onmessage = message => { - this._handleWebSocketMessage(message); - }; - } else { - this._sendMessage(subscribeMessage); - } + this._client.onerror = () => { + this._alertAllHandlersToError(new Error('hello')); + }; + this._client.onclose = () => { + _.forEach(this._subscriptions, subscription => { + subscription.handler.onClose(this, subscription.subscriptionOpts); + }); + }; + this._client.onmessage = message => { + this._handleWebSocketMessage(message); + }; + this._sendMessage(subscribeMessage); } /** * Close the websocket and stop receiving updates */ public close(): void { - if (!_.isUndefined(this._clientIfExists)) { - this._clientIfExists.close(); - } + this._client.close(); } /** * Send a message to the client if it has been instantiated and it is open */ private _sendMessage(message: any): void { - if (!_.isUndefined(this._clientIfExists) && this._clientIfExists.readyState === WebSocket.w3cwebsocket.OPEN) { - this._clientIfExists.send(JSON.stringify(message)); + if (this._client.readyState === WebSocket.OPEN) { + this._client.send(JSON.stringify(message)); } } /** diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts index 88b09506c..30ce57aea 100644 --- a/packages/connect/src/index.ts +++ b/packages/connect/src/index.ts @@ -1,6 +1,7 @@ export { HttpClient } from './http_client'; export { BrowserWebSocketOrderbookChannel } from './browser_ws_orderbook_channel'; export { NodeWebSocketOrderbookChannel } from './node_ws_orderbook_channel'; +export { orderbookChannelFactory } from './orderbook_channel_factory'; export { Client, FeesRequest, diff --git a/packages/connect/src/orderbook_channel_factory.ts b/packages/connect/src/orderbook_channel_factory.ts new file mode 100644 index 000000000..cb00212e7 --- /dev/null +++ b/packages/connect/src/orderbook_channel_factory.ts @@ -0,0 +1,33 @@ +// import * as WebSocket from 'websocket'; + +import { BrowserWebSocketOrderbookChannel } from './browser_ws_orderbook_channel'; +import { NodeWebSocketOrderbookChannel } from './node_ws_orderbook_channel'; + +export const orderbookChannelFactory = { + async createBrowserOrderbookChannelAsync(url: string): Promise<BrowserWebSocketOrderbookChannel> { + return new Promise<BrowserWebSocketOrderbookChannel>((resolve, reject) => { + const client = new WebSocket(url); + console.log(client); + client.onopen = () => { + const orderbookChannel = new BrowserWebSocketOrderbookChannel(client); + console.log(orderbookChannel); + resolve(orderbookChannel); + }; + client.onerror = err => { + reject(err); + }; + }); + }, + // async createNodeOrderbookChannelAsync(url: string): Promise<NodeWebSocketOrderbookChannel> { + // return new Promise<BrowserWebSocketOrderbookChannel>((resolve, reject) => { + // const client = new WebSocket.w3cwebsocket(url); + // client.onopen = () => { + // const orderbookChannel = new BrowserWebSocketOrderbookChannel(client); + // resolve(orderbookChannel); + // }; + // client.onerror = err => { + // reject(err); + // }; + // }); + // }, +}; |