blob: 35b4487a9fdcc9e8325df550a736c1225e153ec7 [file] [log] [blame]
const http = require("http");
const https = require("https");
class PyPubSub {
constructor(url) {
this.url = url;
this.getter = url.match(/^https/i) ? https : http;
}
attach(func) {
this.getter.get(this.url, res => {
res.setEncoding("utf8");
let body = '';
res.on("data", data => {
body += data;
if (data.endsWith("\n")) {
let payload = JSON.parse(body);
body = '';
func(payload);
}
});
});
}
}
// Test
function process(payload) {
// ping-back?
if (payload.stillalive) {
console.log("Got a ping-back");
// Actual payload? process it!
} else {
console.log("Got a payload from PyPubSub!");
console.log(payload);
}
}
const pps = new PyPubSub('http://localhost:2069/');
pps.attach(process);