systemquery/src/lib/agent/subsystems/http/ServerSentEventStream.mjs
Starbeamrainbowlabs 3819f8c61a
It works! Wooo!
...almost. We still need to ask ourselves for a table too haha
2022-02-21 03:10:49 +00:00

34 lines
752 B
JavaScript

"use strict";
import log from '../../../io/NamespacedLog.mjs'; const l = log("sse");
class ServerSentEventStream {
constructor(response) {
this.response = response;
this.next_id = 0;
this.response.writeHead(200, {
"content-type": "text/event-stream",
"cache-control": "no-store"
});
this.response.flushHeaders();
}
write(event_name, data) {
// l.log(`event #${this.next_id} ${event_name}: ${data}`);
this.response.write(`id: ${this.next_id++}\n`);
this.response.write(`event: ${event_name}\n`);
this.response.write(`data: ${data}\n\n`);
}
write_json(event_name, data_obj) {
return this.write(event_name, JSON.stringify(data_obj));
}
end() {
this.response.end();
}
}
export default ServerSentEventStream;