client.js 786 B

123456789101112131415161718192021222324252627282930313233343536
  1. const http = require("http");
  2. const https = require("https");
  3. class PyPubSub {
  4. constructor(url) {
  5. this.url = url;
  6. this.getter = url.match(/^https/i) ? https : http;
  7. }
  8. attach(func) {
  9. this.getter.get(this.url, res => {
  10. res.setEncoding("utf8");
  11. res.on("data", data => {
  12. let payload = JSON.parse(data);
  13. func(payload);
  14. });
  15. });
  16. }
  17. }
  18. // Test
  19. function process(payload) {
  20. // ping-back?
  21. if (payload.stillalive) {
  22. console.log("Got a ping-back");
  23. // Actual payload? process it!
  24. } else {
  25. console.log("Got a payload from PyPubSub!");
  26. console.log(payload);
  27. }
  28. }
  29. const pps = new PyPubSub('http://localhost:2069/');
  30. pps.attach(process);