xoh-xapi
Version:
X Open Hub API
291 lines (290 loc) • 11.6 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ws_1 = __importDefault(require("ws"));
const periods_1 = require("./constants/periods");
class Streamer {
constructor(args) {
this.init = () => {
return (new Promise((resolved, rejected) => {
if (this.socketOpen === false) {
this.socket.onopen = () => {
this.socketOpen = true;
//ping after every 10 secs to keep connection alive
this.pingTimerId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
this.ping(); //can be set to stream version of ping
}), 10000);
resolved(true);
};
this.socket.onmessage = (e) => {
this.processDataStream(e.data);
};
this.socket.onerror = (error) => {
this.socketOpen = false;
rejected("WebSocket error");
};
this.socket.onclose = (e) => {
this.socketOpen = false;
rejected(new Error("Streaming connection closed"));
};
}
else {
resolved(true);
}
}));
};
this.processDataStream = (data) => {
let response = JSON.parse(data);
let command = response.command;
let symbol = response.data.symbol;
this.requests.forEach((item, index) => {
if (item.command === command && item.symbol === symbol) {
item.listener(data);
if (command === "balance") {
item.listener(response.data);
}
else if (command === "candle") {
item.listener(response.data);
}
else if (command === "keepAlive") {
item.listener(response.data);
}
else if (command === "news") {
item.listener(response.data);
}
else if (command === "profit") {
item.listener(response.data);
}
else if (command === "tickPrices") {
item.listener(response.data);
}
else if (command === "trade") {
item.listener(response.data);
}
else if (command === "tradeStatus") {
item.listener(response.data);
}
else {
//just to be safe, reply with unformated data
item.listener(data);
}
}
});
};
this.keepAlive = () => {
let requestData = {
command: "getKeepAlive",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.registerRequest = (request) => {
this.requests.push(request);
};
this.deleteRequest = (request) => {
//delete request here
let command = request.command;
let symbol = request.symbol;
this.requests.forEach((item, index) => {
if (item.command === command && item.symbol === symbol) {
this.requests.splice(index, 1);
}
});
};
this.ping = () => {
let requestData = {
command: "ping",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getBalance = (args) => {
this.registerRequest({
command: "balance",
listener: args.listener,
});
let requestData = {
command: "getBalance",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getNews = (args) => {
this.registerRequest({
command: "news",
listener: args.listener,
});
let requestData = {
command: "getNews",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getProfits = (args) => {
this.registerRequest({
command: "profit",
listener: args.listener,
});
let requestData = {
command: "getProfits",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getTrades = (args) => {
this.registerRequest({
command: "trade",
listener: args.listener,
});
let requestData = {
command: "getTrades",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getTradeStatus = (args) => {
this.registerRequest({
command: "tradeStatus",
listener: args.listener,
});
let requestData = {
command: "getTradeStatus",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getKeepAlive = (args) => {
this.registerRequest({
command: "keepAlive",
listener: args.listener,
});
let requestData = {
command: "getKeepAlive",
streamSessionId: this.streamSessionId,
};
this.socket.send(JSON.stringify(requestData));
};
this.getCandles = (args) => {
this.registerRequest({
command: "candle",
listener: args.listener,
symbol: args.symbol,
});
let requestData = {
command: "getCandles",
streamSessionId: this.streamSessionId,
symbol: args.symbol,
};
(() => __awaiter(this, void 0, void 0, function* () {
//runs the first trigger
let triggerData = yield this.candlesTrigger({
symbol: args.symbol,
start: ((new Date().getTime()) - (1000 * (60 * 60))),
period: ((args.period === undefined) ? periods_1.PERIOD_M1 : args.period),
});
//runs the second trigger
let triggerData2 = yield this.candlesTrigger2({
symbol: args.symbol,
start: ((new Date().getTime()) - (1000 * (60 * 60))),
end: (new Date().getTime()),
period: ((args.period === undefined) ? periods_1.PERIOD_M1 : args.period),
});
this.socket.send(JSON.stringify(requestData));
}))();
};
this.getTickPrices = (args) => {
this.registerRequest({
command: "tickPrices",
listener: args.listener,
symbol: args.symbol,
});
let requestData = {
command: "getTickPrices",
streamSessionId: this.streamSessionId,
symbol: args.symbol,
minArrivalTime: ((args.minArrivalTime === undefined) ? 0 : args.minArrivalTime),
maxLevel: ((args.maxLevel === undefined) ? 2 : args.maxLevel),
};
this.socket.send(JSON.stringify(requestData));
};
this.stopBalance = () => {
let requestData = {
command: "stopBalance",
};
this.socket.send(JSON.stringify(requestData));
};
this.stopCandles = (args) => {
let requestData = {
command: "stopCandles",
symbol: args.symbol,
};
this.socket.send(JSON.stringify(requestData));
};
this.stopTickPrices = (args) => {
let requestData = {
command: "stopTickPrices",
symbol: args.symbol,
};
this.socket.send(JSON.stringify(requestData));
};
this.stopKeepAlive = () => {
let requestData = {
command: "stopKeepAlive",
};
this.socket.send(JSON.stringify(requestData));
};
this.stopNews = () => {
let requestData = {
command: "stopNews",
};
this.socket.send(JSON.stringify(requestData));
};
this.stopTrades = () => {
let requestData = {
command: "stopTrades",
};
this.socket.send(JSON.stringify(requestData));
};
this.stopTradeStatus = () => {
let requestData = {
command: "stopTradeStatus",
};
this.socket.send(JSON.stringify(requestData));
};
this.stopProfits = () => {
let requestData = {
command: "stopProfits",
};
this.socket.send(JSON.stringify(requestData));
};
this.streamSessionId = args.streamSessionId;
this.host = args.host;
this.candlesTrigger = args.candlesTrigger;
this.candlesTrigger2 = args.candlesTrigger2;
this.socketOpen = false;
if (typeof process === 'object' &&
typeof process.versions === 'object' &&
typeof process.versions.node !== 'undefined') {
this.socket = new ws_1.default(this.host);
}
else {
this.socket = new WebSocket(this.host);
}
this.requests = [];
(() => __awaiter(this, void 0, void 0, function* () {
yield this.init();
}))();
}
}
exports.default = Streamer;