UNPKG

@exchanges/binance

Version:

Exchange provider for Binance API

46 lines 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.socketCall = void 0; const html5_websocket_1 = __importDefault(require("html5-websocket")); const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket")); function socketCall(options, callback) { if (!options) throw new TypeError('socketCall options is empty'); if (typeof options !== 'object') throw new TypeError('socketCall options is not object'); if (!callback) throw new TypeError('socketCall callback is empty'); if (typeof callback !== 'function') throw new TypeError('socketCall callback is not function'); if (!options.host) throw new TypeError('socketCall options.host is empty'); if (!options.path) throw new TypeError('socketCall options.path is empty'); const queryPath = [options.host, options.path].filter(Boolean).join('/'); const socket = new reconnecting_websocket_1.default(queryPath, undefined, { connectionTimeout: 4 * 1e3, WebSocket: typeof window !== 'undefined' ? WebSocket : html5_websocket_1.default, debug: false, maxReconnectionDelay: 10 * 1e3, minReconnectionDelay: 4 * 1e3, maxRetries: Infinity, }); socket.onmessage = (e) => { const data = e && e.data; if (!data) return callback(void 0, void 0); try { const msg = JSON.parse(data); return callback(msg, void 0); } catch (err) { return callback(void 0, err); } }; return () => socket.close(1000, 'Close handle was called'); } exports.socketCall = socketCall; //# sourceMappingURL=socketCall.js.map