aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/browser_ws_orderbook_channel.ts
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-05-25 08:19:27 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-07-12 01:17:45 +0800
commit47debf0134b5864046831321b8eeeeb9aaaaf0a8 (patch)
treed371b018939c7dad121edd0eae324cc560c95d74 /packages/connect/src/browser_ws_orderbook_channel.ts
parent16ddd1edfccdd7768447bfff9afec1f4a1ce014e (diff)
downloaddexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar.gz
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar.bz2
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar.lz
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar.xz
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.tar.zst
dexon-sol-tools-47debf0134b5864046831321b8eeeeb9aaaaf0a8.zip
Initial implementation of OrderbookChannelFactory
Diffstat (limited to 'packages/connect/src/browser_ws_orderbook_channel.ts')
-rw-r--r--packages/connect/src/browser_ws_orderbook_channel.ts49
1 files changed, 19 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));
}
}
/**