webdojo: connect to slog stream for printfs
Connects to the SSE stream on /~/slog to receive printf output from the
runtime, and pretends those are incoming %txt effects.
Depends on which introduces the endpoint used here.
@@ -14,6 +14,36 @@ export default class Subscription {
} else {
console.error('~~~ ERROR: Must set api.ship before operation ~~~');
}
+ this.setupSlog();
+ }
+
+ setupSlog() {
+ const slog = new EventSource('/~/slog', { withCredentials: true });
+ let available = false;
+
+ slog.onopen = e => {
+ console.log('slog: opened stream');
+ available = true;
+ }
+
+ slog.onmessage = e => {
+ this.handleEvent({ txt: e.data });
+ }
+
+ slog.onerror = e => {
+ console.error('slog: eventsource error:', e);
+ if (!available) {
+ this.handleEvent({ txt:
+ 'landscape: no printf stream. bad connection or old binary.'
+ });
+ } else {
+ window.setTimeout(() => {
+ if (slog.readyState !== EventSource.CLOSED) return;
+ console.log('slog: reconnecting...');
+ this.setupSlog();
+ }, 10000);
+ }
+ }
}
delete() {
