UNPKG

shrimpy-node

Version:
133 lines 5.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var WebSocket = require("ws"); var ShrimpyWsClient = /** @class */ (function () { function ShrimpyWsClient(errorCallback, token) { if (token === void 0) { token = ""; } this._baseUrl = 'wss://ws-feed.shrimpy.io'; this._token = ""; this._websocket = undefined; this._subscriptionCallbacks = {}; var url = this._baseUrl; if (token) { this._token = token; url = this._baseUrl + "?token=" + this._token; } this._websocketErrorCallback = errorCallback; this._websocket = new WebSocket(url); } ShrimpyWsClient.prototype.connect = function () { var _this = this; if (this._websocket == undefined) { return; } this._websocket.on('open', function open() { // Open the connection }); this._websocket.on('error', function (error) { var wsError = { 'type': 'WebsocketClientError', 'code': 2404, 'message': error.message }; _this._websocketErrorCallback(wsError); }); this._websocket.on('message', function (message) { var parsedMessage = JSON.parse(message.toString()); var topic = _this._getTopic(parsedMessage); if (topic === 'ping') { // Handle Ping _this._pong(parsedMessage); return; } if (topic === 'error') { _this._websocketErrorCallback(parsedMessage); return; } var successCallback = _this._subscriptionCallbacks[topic]; if (successCallback !== undefined) { successCallback(parsedMessage); } }); this._websocket.on('close', function () { // Connection has been closed, delete all callbacks _this._subscriptionCallbacks = {}; }); }; ShrimpyWsClient.prototype.disconnect = function () { if (this._websocket !== undefined) { this._websocket.close(); } }; ShrimpyWsClient.prototype.forceDisconnect = function () { if (this._websocket !== undefined) { this._websocket.terminate(); } }; ShrimpyWsClient.prototype.reconnect = function (token) { if (token === void 0) { token = ""; } var url = this._baseUrl; if (token) { this._token = token; url = this._baseUrl + "?token=" + this._token; } this.forceDisconnect(); this._websocket = new WebSocket(url); this.connect(); }; ShrimpyWsClient.prototype.subscribe = function (subscriptionRequest, successCallback) { if (this._websocket == undefined) { return; } if (this._websocket.OPEN == this._websocket.readyState) { var topic = this._getTopic(subscriptionRequest); this._subscriptionCallbacks[topic] = successCallback; this._websocket.send(JSON.stringify(subscriptionRequest)); } }; ShrimpyWsClient.prototype.unsubscribe = function (unsubscriptionRequest) { var topic = this._getTopic(unsubscriptionRequest); delete this._subscriptionCallbacks[topic]; if (this._websocket == undefined) { return; } if (this._websocket.OPEN == this._websocket.readyState) { this._websocket.send(JSON.stringify(unsubscriptionRequest)); } }; ShrimpyWsClient.prototype.getReadyState = function () { if (this._websocket == undefined) { return WebSocket.CLOSED; } return this._websocket.readyState; }; ShrimpyWsClient.prototype._getTopic = function (message) { if (message.hasOwnProperty('type')) { var messageType = message['type']; if (messageType.indexOf('subscribe') === -1) { return messageType.toLowerCase(); } } var exchange = message['exchange']; var pair = message['pair']; var channel = message['channel']; var rawKeys = [exchange.toLowerCase(), pair.toLowerCase(), channel.toLowerCase()]; var nonNullKeys = rawKeys.filter(function (k) { return k !== undefined; }); return nonNullKeys.join('-'); }; ShrimpyWsClient.prototype._pong = function (parsedData) { if (this._websocket == undefined) { return; } var pong = { 'type': 'pong', 'data': parsedData.data }; this._websocket.send(JSON.stringify(pong)); }; return ShrimpyWsClient; }()); exports.ShrimpyWsClient = ShrimpyWsClient; //# sourceMappingURL=shrimpy-ws-client.js.map