/**
* BlinkTradeJS SDK
* (c) 2016-present BlinkTrade, Inc.
*
* This file is part of BlinkTradeJS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* @flow
*/
import WS from 'ws';
import nodeify from 'nodeify';
import EventEmitter from 'eventemitter2';
import { getMac } from '../util/macaddress';
import { getFingerPrint } from '../util/fingerPrint';
import { getStun, closeStun } from '../util/stun';
import { MsgActionRes } from '../constants/messages';
import BROKERS from '../constants/brokers';
import Transport, { IS_NODE, IS_BROWSER } from './transport';
import type {
Stun,
Message,
ResolveReject,
PromiseEmitter,
BlinkTradeWSParams,
} from '../types';
import {
getRequest,
setRequest,
deleteRequest,
} from '../listener';
const RECONNECT_INTERVAL = 5000;
class WebSocketTransport extends Transport {
/*
* WebSocket Instance
*/
socket: WebSocket;
/*
* FingerPrint
*/
fingerPrint: string;
/*
* Stun object
*/
stun: Stun;
connection: ResolveReject;
lastPromise: ResolveReject;
lastMessageSent: Message;
/*
* Event emitter to dispatch websocket updates
*/
eventEmitter: EventEmitter;
headers: ?Object;
autoReconnect: boolean;
reconnectInterval: number;
constructor(params?: BlinkTradeWSParams = {}) {
super(params, params.brokerId === BROKERS.BITCAMBIO ? 'wsBitcambio' : 'ws');
this.stun = { local: null, public: [] };
this.getFingerPrint(params.fingerPrint);
this.headers = params.headers;
this.autoReconnect = params.reconnect || false;
this.reconnectInterval = params.reconnectInterval || RECONNECT_INTERVAL;
this.eventEmitter = new EventEmitter({ wildcard: true, delimiter: ':' });
}
connect(callback?: Function): Promise