forex-quotes
Version:
Realtime forex quote API client
159 lines (158 loc) • 5.68 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.SocketClient = exports.IOEvents = exports.OutgoingEvents = exports.IncomingEvents = void 0;
// tslint:disable-next-line
var WebSocket = require('ws');
var url = 'wss://sockets.1forge.com/socket';
var IncomingEvents;
(function (IncomingEvents) {
IncomingEvents["MESSAGE"] = "message";
IncomingEvents["FORCE_CLOSE"] = "force_close";
IncomingEvents["HEART"] = "heart";
IncomingEvents["LOGIN"] = "login";
IncomingEvents["POST_LOGIN_SUCCESS"] = "post_login_success";
IncomingEvents["UPDATE"] = "update";
})(IncomingEvents = exports.IncomingEvents || (exports.IncomingEvents = {}));
var OutgoingEvents;
(function (OutgoingEvents) {
OutgoingEvents["LOGIN"] = "login";
OutgoingEvents["SUBSCRIBE_TO"] = "subscribe_to";
OutgoingEvents["UNSUBSCRIBE_FROM"] = "unsubscribe_from";
OutgoingEvents["SUBSCRIBE_TO_ALL"] = "subscribe_to_all";
OutgoingEvents["UNSUBSCRIBE_FROM_ALL"] = "unsubscribe_from_all";
})(OutgoingEvents = exports.OutgoingEvents || (exports.OutgoingEvents = {}));
var IOEvents;
(function (IOEvents) {
IOEvents["DISCONNECT"] = "close";
IOEvents["CONNECTION"] = "open";
})(IOEvents = exports.IOEvents || (exports.IOEvents = {}));
var SocketClient = /** @class */ (function () {
function SocketClient(apiKey) {
var _this = this;
this.apiKey = apiKey;
this.handleLoginRequest = function () {
_this.emit(OutgoingEvents.LOGIN, _this.apiKey);
};
this.handleOpen = function () {
_this.emit(OutgoingEvents.LOGIN, _this.apiKey);
};
this.handlePostLoginSuccess = function () {
if (!_this.onConnectionCallback) {
return;
}
_this.onConnectionCallback(_this);
};
this.handleMessage = function (message) {
var action = message.split('|')[0];
var body = message.split('|').slice(1).join('|');
switch (action) {
case IncomingEvents.LOGIN:
_this.handleLoginRequest();
return;
case IncomingEvents.POST_LOGIN_SUCCESS:
_this.handlePostLoginSuccess();
return;
case IncomingEvents.UPDATE:
_this.handleUpdate(JSON.parse(body));
return;
case IncomingEvents.FORCE_CLOSE:
_this.handleDisconnect();
return;
case IncomingEvents.HEART:
_this.handleHeart();
return;
}
if (!_this.onMessageCallback) {
return;
}
_this.onMessageCallback(body);
};
this.handleUpdate = function (data) {
if (!_this.onUpdateCallback) {
return;
}
_this.onUpdateCallback(data.s, data);
};
this.handleHeart = function () {
_this.emit('beat');
};
this.handleDisconnect = function () {
if (!_this.onDisconnectCallback) {
return;
}
_this.onDisconnectCallback();
};
this.emit = function (action, message) {
if (message == null) {
_this.socket.send(action);
}
else {
_this.socket.send(action + "|" + message);
}
};
}
SocketClient.prototype.onMessage = function (onMessage) {
this.onMessageCallback = onMessage;
return this;
};
SocketClient.prototype.onUpdate = function (onUpdate) {
this.onUpdateCallback = onUpdate;
return this;
};
SocketClient.prototype.onConnect = function (onConnect) {
this.onConnectionCallback = onConnect;
return this;
};
SocketClient.prototype.onDisconnect = function (onDisconnect) {
this.onDisconnectCallback = onDisconnect;
return this;
};
SocketClient.prototype.subscribeTo = function (symbols) {
var _this = this;
if (Array.isArray(symbols)) {
symbols.forEach(function (symbol) {
return _this.subscribeTo(symbol);
});
return this;
}
this.emit(OutgoingEvents.SUBSCRIBE_TO, symbols);
return this;
};
SocketClient.prototype.subscribeToAll = function () {
this.emit(OutgoingEvents.SUBSCRIBE_TO_ALL);
return this;
};
SocketClient.prototype.unsubscribeFrom = function (symbols) {
var _this = this;
if (Array.isArray(symbols)) {
symbols.forEach(function (symbol) {
return _this.unsubscribeFrom(symbol);
});
}
this.emit(OutgoingEvents.UNSUBSCRIBE_FROM, symbols);
return this;
};
SocketClient.prototype.unsubscribeFromAll = function () {
this.emit(OutgoingEvents.UNSUBSCRIBE_FROM_ALL);
return this;
};
SocketClient.prototype.disconnect = function () {
if (this.socket) {
this.socket.close();
this.socket = undefined;
}
return this;
};
SocketClient.prototype.connect = function () {
this.initializeSocketClient();
};
SocketClient.prototype.initializeSocketClient = function () {
this.socket = new WebSocket(url);
this.socket.on('close', this.handleDisconnect);
this.socket.on('error', this.disconnect);
this.socket.on('message', this.handleMessage);
this.socket.on('open', this.handleOpen);
};
return SocketClient;
}());
exports.SocketClient = SocketClient;