start: function (privateChannel) {
if (this.status !== NOT_STARTED) {
return;
}
console.info("ProfitRobots: Starting alert listener");
this.status = STARTED;
this.privateChannel = privateChannel;
const lastEventId = window.localStorage.getItem("last_event_id") || "";
const url = "https://pushstream.tradingview.com"
+ "/message-pipe-es/public/"
+ "private_" + this.privateChannel
+ "?_=" + Math.floor(Date.now() / 1000)
+ "&tag="
+ "&time="
+ "&eventid=" + lastEventId
this.eventSource = new EventSource(url)
this.eventSource.onerror = (e) => {
console.info("ProfitRobots: Error", e);
if (e.target.readyState === EventSource.CLOSED) {
this.status = NOT_STARTED;
}
}
this.eventSource.onmessage = (e) => {
try {
const data = JSON.parse(e.data);
if (typeof data !== "object" || data === null) {
return;
}
if (data.channel !== "private_" + this.privateChannel) {
return;
}
const response = JSON.parse(data.text.content);
if (typeof response !== "object" || response === null) {
return;
}
if (response.m !== "event") {
return;
}
window.localStorage.setItem("last_event_id", response.p.id);
console.info("ProfitRobots: Message detected", response.p.desc);
this.sendAlert(response.p.desc, response.p.sym, this.parseResolution(response.p.res));
}
catch(e) {
}
}
this.eventSource.onopen = () => {
}
},