-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.html
More file actions
678 lines (639 loc) · 24.5 KB
/
Copy pathindex.html
File metadata and controls
678 lines (639 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<!doctype html>
<!--
rclnodejs/web demo. Open via http://localhost:8080/ once both
`node runtime.cjs` (rclnodejs/web runtime) and `node static.cjs`
(page server) are running — see README.md.
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<title>rclnodejs/web — zero-build ROS 2 in a single HTML page</title>
<style>
:root {
color-scheme: light dark;
}
body {
font-family: system-ui, sans-serif;
max-width: 880px;
margin: 2em auto;
padding: 0 1em;
}
h1 {
margin-bottom: 0;
}
h2 {
margin-top: 2em;
}
.endpoint {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.9em;
}
input,
button {
font-size: 1em;
padding: 0.3em 0.5em;
}
input[type='text'],
input[type='number'] {
width: 8em;
}
.panel {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1em;
align-items: start;
}
.controls {
padding: 0.25em 0;
}
.controls .row {
margin-bottom: 0.5em;
}
.log {
background: #1e1e1e;
color: #d4d4d4;
padding: 0.6em;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.85em;
max-height: 12em;
overflow: auto;
white-space: pre-wrap;
}
/* Don't render the dark log box until something is logged. */
.log:empty {
display: none;
}
.log .ok {
color: #6ec06e;
}
.log .err {
color: #f08080;
}
pre.code {
margin: 0;
background: #1e1e1e;
color: #e6e6e6;
padding: 0.75em;
border-radius: 4px;
font-size: 0.85em;
line-height: 1.4;
overflow: auto;
}
.code .kw {
color: #569cd6;
}
.code .str {
color: #ce9178;
}
.code .com {
color: #6a9955;
font-style: italic;
}
.status {
display: inline-block;
font-size: 0.85em;
padding: 0.15em 0.6em;
border-radius: 999px;
background: #fde68a;
color: #92400e;
}
.status.ok {
background: #bbf7d0;
color: #166534;
}
.status.err {
background: #fecaca;
color: #991b1b;
}
.badge {
display: inline-block;
font-size: 0.7em;
padding: 0.1em 0.4em;
border-radius: 4px;
background: #f7df1e;
color: #000;
vertical-align: middle;
margin-left: 0.5em;
}
.transport {
margin: 1em 0 0.5em;
padding: 0.6em 0.8em;
background: #f5f5f4;
border-radius: 4px;
font-size: 0.9em;
}
.transport label {
margin-right: 1em;
cursor: pointer;
}
@media (prefers-color-scheme: dark) {
.transport {
background: #2a2a2a;
}
}
@media (max-width: 720px) {
.panel {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<h1>
Zero-build ROS 2 in a single HTML page
<span class="badge">JavaScript</span>
</h1>
<p>
A single static HTML page that talks to a real ROS 2 graph — one Node
process exposes a typed, capability-allow-listed gateway, the browser
drives it with three verbs.
</p>
<p>
Endpoint:
<span class="endpoint" id="endpoint">ws://localhost:9000/capability</span>
<span class="status" id="status">connecting…</span>
</p>
<div class="transport">
<strong>Transport:</strong>
<label
><input type="radio" name="transport" value="ws" checked /> WebSocket
(call+publish+subscribe)</label
>
<label
><input type="radio" name="transport" value="http" /> HTTP
(call+publish; subscribe falls through to WS sibling)</label
>
<div style="margin-top: 0.4em; font-size: 0.85em; color: #666">
Same registry, same allow-list — the SDK picks the transport from the
URL scheme.
</div>
</div>
<h2>1. Service call — <code>/add_two_ints</code></h2>
<div class="panel">
<div class="controls">
<div class="row">
<input id="addA" type="number" value="2" />
<span>+</span>
<input id="addB" type="number" value="40" />
<button id="callBtn">call</button>
</div>
<div class="log" id="callLog"></div>
</div>
<pre
class="code"
><span class="kw">import</span> { connect } <span class="kw">from</span> <span class="str">'/sdk/index.js'</span>;
<span class="kw">const</span> ros = <span class="kw">await</span> connect(<span class="str">'ws://localhost:9000/capability'</span>);
<span class="kw">const</span> reply = <span class="kw">await</span> ros.<span class="kw">call</span>(<span class="str">'/add_two_ints'</span>, {
a: <span class="str">'2n'</span>, b: <span class="str">'40n'</span>, <span class="com">// int64 → "Nn" string on the wire</span>
});
console.log(reply.sum); <span class="com">// '42n'</span></pre>
</div>
<h2>2. Topic subscription — <code>/web_demo_chatter</code></h2>
<div class="panel">
<div class="controls">
<div class="row">
<button id="subBtn">subscribe</button>
<button id="unsubBtn" disabled>unsubscribe</button>
</div>
<div class="log" id="subLog"></div>
</div>
<pre
class="code"
><span class="kw">import</span> { connect } <span class="kw">from</span> <span class="str">'/sdk/index.js'</span>;
<span class="kw">const</span> ros = <span class="kw">await</span> connect(<span class="str">'ws://localhost:9000/capability'</span>);
<span class="com">// /web_demo_chatter is the shared demo topic — anything panel 3</span>
<span class="com">// (or curl) publishes to it lands here, since it's the same topic.</span>
<span class="kw">const</span> sub = <span class="kw">await</span> ros.<span class="kw">subscribe</span>(<span class="str">'/web_demo_chatter'</span>, (msg) => {
console.log(<span class="str">'recv:'</span>, msg.data);
});
<span class="com">// later — stop receiving:</span>
<span class="kw">await</span> sub.close();</pre>
</div>
<h2>3. Topic publish — <code>/web_demo_chatter</code></h2>
<div class="panel">
<div class="controls">
<div class="row">
<input
id="chatMsg"
type="text"
value="hello from the browser"
style="width: 18em"
/>
<button id="pubBtn">publish</button>
</div>
<div class="log" id="chatLog"></div>
</div>
<pre
class="code"
><span class="kw">import</span> { connect } <span class="kw">from</span> <span class="str">'/sdk/index.js'</span>;
<span class="kw">const</span> ros = <span class="kw">await</span> connect(<span class="str">'ws://localhost:9000/capability'</span>);
<span class="com">// Subscribe so we can see the round-trip:</span>
<span class="kw">await</span> ros.<span class="kw">subscribe</span>(<span class="str">'/web_demo_chatter'</span>, (m) =>
console.log(<span class="str">'recv:'</span>, m.data),
);
<span class="kw">await</span> ros.<span class="kw">publish</span>(<span class="str">'/web_demo_chatter'</span>, { data: <span class="str">'hello'</span> });</pre>
</div>
<h2>4. Capability allow-list in action</h2>
<div class="panel">
<div class="controls">
<div class="row">
<button id="badCallBtn">call <code>/dangerous</code></button>
</div>
<div class="log" id="badLog"></div>
</div>
<pre
class="code"
><span class="kw">import</span> { connect } <span class="kw">from</span> <span class="str">'/sdk/index.js'</span>;
<span class="kw">const</span> ros = <span class="kw">await</span> connect(<span class="str">'ws://localhost:9000/capability'</span>);
<span class="com">// /dangerous is not in the server's allow-list — the runtime</span>
<span class="com">// rejects the call before it reaches ROS 2.</span>
<span class="kw">try</span> {
<span class="kw">await</span> ros.<span class="kw">call</span>(<span class="str">'/dangerous'</span>, {});
} <span class="kw">catch</span> (e) {
console.log(e.code); <span class="com">// 'not_exposed'</span>
}</pre>
</div>
<h2>5. Same capability, no SDK — just <code>curl</code></h2>
<p style="font-size: 0.9em; color: #555">
The HTTP transport is what makes <code>rclnodejs/web</code>
<em>actually</em> web-native: every <code>call</code> and
<code>publish</code> in your allow-list is reachable from any HTTP client
— curl, Postman, an AI agent… no JavaScript required. With
<code>sse: true</code> on the runtime (set in this demo's
<code>runtime.mjs</code>), <code>subscribe</code> is also reachable as a
Server-Sent Events stream — handy for clients that can't hold a
WebSocket open. Browser apps still prefer the WebSocket transport, which
multiplexes many topics on one connection.
</p>
<pre
class="code"
><span class="com"># service call — returns 200 + JSON</span>
curl -sS -X POST http://localhost:9001/capability/call/add_two_ints \
-H <span class="str">'content-type: application/json'</span> \
-d <span class="str">'{"a":"7n","b":"35n"}'</span>
<span class="com"># => {"sum":"42n"}</span>
<span class="com"># publish — returns 204 No Content on success</span>
curl -sS -X POST http://localhost:9001/capability/publish/web_demo_chatter \
-H <span class="str">'content-type: application/json'</span> \
-d <span class="str">'{"data":"hi from curl"}'</span>
<span class="com"># subscribe over SSE — streams text/event-stream until you ^C</span>
<span class="com"># (-N disables curl's buffering so events print as they arrive)</span>
curl -N http://localhost:9001/capability/subscribe/web_demo_chatter
<span class="com"># event: ready</span>
<span class="com"># data: {"capability":"/web_demo_chatter","subId":"sse"}</span>
<span class="com">#</span>
<span class="com"># event: message</span>
<span class="com"># data: {"data":"hi from curl"}</span>
<span class="com"># …one `message` event per published sample…</span>
<span class="com"># allow-list rejection — returns 404 + structured error body</span>
curl -sS -X POST http://localhost:9001/capability/call/dangerous \
-H <span class="str">'content-type: application/json'</span> -d <span class="str">'{}'</span>
<span class="com"># => {"ok":false,"error":"capability not exposed: call /dangerous","code":"not_exposed"}</span></pre>
<h2>
6. Native <code>EventSource</code> — SSE subscribe over HTTP
<span class="badge">no SDK</span>
</h2>
<p style="font-size: 0.9em; color: #555">
The same SSE stream the <code>curl</code> example above reads is a
first-class browser API: <code>EventSource</code>. No SDK, no WebSocket —
just a plain HTTP GET the browser keeps open and auto-reconnects. This
works cross-origin because the runtime sends CORS headers
(<code>new HttpTransport({ sse: true, cors: true })</code>). For
multiplexing many topics on one connection, the WebSocket transport in
section 2 is still the better fit.
</p>
<p style="font-size: 0.9em; color: #555">
The topic box defaults to <code>/web_demo_chatter</code> — the same
topic panels 2 and 3 use, so a publish from panel 3 streams in here at
the same time it reaches the WebSocket subscriber. Point the box at
<code>/topic</code> instead to subscribe to an external ROS 2 publisher
(see the README's <em>publisher example</em> pairing).
</p>
<div class="panel">
<div class="controls">
<div class="row">
<input
id="sseTopic"
type="text"
value="/web_demo_chatter"
spellcheck="false"
aria-label="topic to subscribe to"
/>
<button id="sseBtn">open EventSource</button>
<button id="sseCloseBtn" disabled>close</button>
</div>
<div class="log" id="sseLog"></div>
</div>
<pre
class="code"
><span class="com">// No import — EventSource is built into every browser.</span>
<span class="com">// '/web_demo_chatter' is the shared demo topic; use '/topic' to pair with publisher-example.mjs.</span>
<span class="kw">const</span> es = <span class="kw">new</span> EventSource(
<span class="str">'http://localhost:9001/capability/subscribe/web_demo_chatter'</span>,
);
es.addEventListener(<span class="str">'ready'</span>, (e) =>
console.log(<span class="str">'subscribed:'</span>, JSON.parse(e.data)),
);
es.addEventListener(<span class="str">'message'</span>, (e) => {
<span class="kw">const</span> msg = JSON.parse(e.data);
console.log(<span class="str">'recv:'</span>, msg.data);
});
<span class="com">// later — stop receiving:</span>
es.close();</pre>
</div>
<h2>
7. Native <code>fetch()</code> — call & publish over HTTP
<span class="badge">no SDK</span>
</h2>
<p style="font-size: 0.9em; color: #555">
The <code>curl</code> commands in section 5 translate one-to-one to the
browser's <code>fetch()</code> API — same method, URL, headers and JSON
body. Because the runtime sends CORS headers
(<code>new HttpTransport({ cors: true })</code>), these cross-origin
requests succeed straight from the page (served on <code>:8080</code>,
runtime on <code>:9001</code>). <code>call</code> returns
<code>200</code> + a JSON reply; <code>publish</code> returns
<code>204 No Content</code>. The publish lands on the same
<code>/web_demo_chatter</code> topic as panels 2 and 3, so it also shows
up in their logs.
</p>
<div class="panel">
<div class="controls">
<div class="row">
<button id="fetchCallBtn">fetch call <code>/add_two_ints</code></button>
<button id="fetchPubBtn">
fetch publish <code>/web_demo_chatter</code>
</button>
</div>
<div class="log" id="fetchLog"></div>
</div>
<pre
class="code"
><span class="com">// No import — fetch() is built into every browser.</span>
<span class="com">// Service call → 200 + JSON reply:</span>
<span class="kw">const</span> res = <span class="kw">await</span> fetch(
<span class="str">'http://localhost:9001/capability/call/add_two_ints'</span>,
{
method: <span class="str">'POST'</span>,
headers: { <span class="str">'content-type'</span>: <span class="str">'application/json'</span> },
body: JSON.stringify({ a: <span class="str">'7n'</span>, b: <span class="str">'35n'</span> }),
},
);
<span class="kw">const</span> reply = <span class="kw">await</span> res.json();
console.log(reply.sum); <span class="com">// '42n'</span>
<span class="com">// Topic publish → 204 No Content (no body):</span>
<span class="kw">await</span> fetch(
<span class="str">'http://localhost:9001/capability/publish/web_demo_chatter'</span>,
{
method: <span class="str">'POST'</span>,
headers: { <span class="str">'content-type'</span>: <span class="str">'application/json'</span> },
body: JSON.stringify({ data: <span class="str">'hello from fetch()'</span> }),
},
);</pre>
</div>
<script type="module">
import { connect } from '/sdk/index.js';
const host = location.hostname || 'localhost';
const ENDPOINTS = {
ws: `ws://${host}:9000/capability`,
// HTTP base for call/publish.
http: `http://${host}:9001`,
};
// Pass Form C ({http, ws}) when the user picks HTTP so subscribe
// still reaches the WS runtime on :9000. The SDK's auto-derived
// sibling would land on :9001 instead and fail — this dev layout
// splits the two transports across separate ports.
const connectArg = (mode) =>
mode === 'http'
? { http: ENDPOINTS.http, ws: ENDPOINTS.ws }
: ENDPOINTS.ws;
const endpointEl = document.getElementById('endpoint');
const statusEl = document.getElementById('status');
const setStatus = (text, cls) => {
statusEl.textContent = text;
statusEl.className = `status ${cls || ''}`;
};
const setEndpoint = (mode) => {
endpointEl.textContent =
mode === 'http'
? `${ENDPOINTS.http} (subscribe routed to ${ENDPOINTS.ws})`
: ENDPOINTS.ws;
};
const log = (id, text, cls) => {
const el = document.getElementById(id);
const line = document.createElement('div');
line.textContent = `${new Date().toLocaleTimeString()} ${text}`;
if (cls) line.className = cls;
el.appendChild(line);
el.scrollTop = el.scrollHeight;
};
let ros;
let chatterSub;
async function teardown() {
if (chatterSub) {
try {
await chatterSub.close();
} catch (_) {}
chatterSub = null;
document.getElementById('subBtn').disabled = false;
document.getElementById('unsubBtn').disabled = true;
}
if (ros) {
try {
await ros.close();
} catch (_) {}
ros = null;
}
}
async function reconnect(mode) {
await teardown();
setEndpoint(mode);
setStatus(`connecting (${mode})…`);
try {
ros = await connect(connectArg(mode));
setStatus(`connected (${mode})`, 'ok');
} catch (e) {
setStatus(`failed: ${e.message || e}`, 'err');
return;
}
// Bind the always-on /web_demo_chatter subscription on every
// reconnect. Over HTTP the explicit { ws } in connectArg() makes
// this work — see comment above.
try {
await ros.subscribe('/web_demo_chatter', (msg) =>
log('chatLog', `<- ${msg.data}`)
);
} catch (e) {
log('chatLog', `subscribe failed: ${e.message} (${e.code})`, 'err');
}
}
async function init() {
// Wire button handlers once. They use the live `ros` ref so
// they work across reconnect()s.
document.getElementById('callBtn').onclick = async () => {
if (!ros) return;
const a = document.getElementById('addA').value;
const b = document.getElementById('addB').value;
try {
const reply = await ros.call('/add_two_ints', {
a: `${a}n`,
b: `${b}n`,
});
log('callLog', `${a} + ${b} = ${reply.sum}`, 'ok');
} catch (e) {
log('callLog', `error: ${e.message} (${e.code})`, 'err');
}
};
const subBtn = document.getElementById('subBtn');
const unsubBtn = document.getElementById('unsubBtn');
subBtn.onclick = async () => {
if (!ros) return;
try {
chatterSub = await ros.subscribe('/web_demo_chatter', (msg) =>
log('subLog', msg.data)
);
subBtn.disabled = true;
unsubBtn.disabled = false;
log('subLog', `subscribed (subId=${chatterSub.subId})`, 'ok');
} catch (e) {
log('subLog', `error: ${e.message} (${e.code})`, 'err');
}
};
unsubBtn.onclick = async () => {
if (!chatterSub) return;
await chatterSub.close();
chatterSub = null;
unsubBtn.disabled = true;
subBtn.disabled = false;
log('subLog', 'unsubscribed', 'ok');
};
document.getElementById('pubBtn').onclick = async () => {
if (!ros) return;
const data = document.getElementById('chatMsg').value;
try {
await ros.publish('/web_demo_chatter', { data });
log('chatLog', `-> ${data}`, 'ok');
} catch (e) {
log('chatLog', `error: ${e.message} (${e.code})`, 'err');
}
};
document.getElementById('badCallBtn').onclick = async () => {
if (!ros) return;
try {
await ros.call('/dangerous', {});
log(
'badLog',
'unexpected success — registry should have rejected',
'err'
);
} catch (e) {
log('badLog', `rejected: ${e.message} (${e.code})`, 'ok');
}
};
// Native EventSource — independent of the WS/HTTP transport
// toggle above. It always talks to the HTTP transport's SSE
// endpoint directly, which is exactly the point: no SDK, no
// WebSocket, just a browser primitive over plain HTTP (CORS is
// enabled on the runtime so this works cross-origin).
const sseBtn = document.getElementById('sseBtn');
const sseCloseBtn = document.getElementById('sseCloseBtn');
const sseTopic = document.getElementById('sseTopic');
let es;
const closeEs = () => {
if (es) {
es.close();
es = null;
}
sseBtn.disabled = false;
sseCloseBtn.disabled = true;
sseTopic.disabled = false;
};
sseBtn.onclick = () => {
closeEs();
// Accept '/topic' or 'topic'; the URL path carries the bare name.
const name = (sseTopic.value || '/web_demo_chatter')
.trim()
.replace(/^\//, '');
const url = `${ENDPOINTS.http}/capability/subscribe/${encodeURIComponent(
name
)}`;
es = new EventSource(url);
sseBtn.disabled = true;
sseCloseBtn.disabled = false;
sseTopic.disabled = true;
log('sseLog', `EventSource → ${url}`, 'ok');
es.addEventListener('ready', (e) => {
const info = JSON.parse(e.data);
log('sseLog', `ready (subId=${info.subId})`, 'ok');
});
es.addEventListener('message', (e) => {
log('sseLog', JSON.parse(e.data).data);
});
es.addEventListener('error', (e) => {
// SSE `error` events carry no payload for transport drops;
// our runtime only sends a data-bearing `error` event for a
// terminal failure (e.g. capability removed).
if (e.data) {
const err = JSON.parse(e.data);
log('sseLog', `error: ${err.error} (${err.code})`, 'err');
} else if (es && es.readyState === EventSource.CONNECTING) {
log('sseLog', 'connection lost — auto-reconnecting…', 'err');
}
});
};
sseCloseBtn.onclick = () => {
closeEs();
log('sseLog', 'closed', 'ok');
};
// Native fetch() — call/publish straight to the HTTP transport,
// no SDK and no WebSocket. CORS on the runtime lets these
// cross-origin requests (page on :8080 → runtime on :9001)
// succeed. Independent of the WS/HTTP transport toggle above.
document.getElementById('fetchCallBtn').onclick = async () => {
try {
const res = await fetch(
`${ENDPOINTS.http}/capability/call/add_two_ints`,
{
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ a: '7n', b: '35n' }),
}
);
const reply = await res.json();
log('fetchLog', `7 + 35 = ${reply.sum} (HTTP ${res.status})`, 'ok');
} catch (e) {
log('fetchLog', `call error: ${e.message}`, 'err');
}
};
document.getElementById('fetchPubBtn').onclick = async () => {
try {
const res = await fetch(
`${ENDPOINTS.http}/capability/publish/web_demo_chatter`,
{
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ data: 'hello from fetch()' }),
}
);
// publish replies 204 No Content on success.
log(
'fetchLog',
`published 'hello from fetch()' (HTTP ${res.status})`,
res.ok ? 'ok' : 'err'
);
} catch (e) {
log('fetchLog', `publish error: ${e.message}`, 'err');
}
};
for (const radio of document.querySelectorAll(
'input[name="transport"]'
)) {
radio.addEventListener('change', (e) => reconnect(e.target.value));
}
await reconnect('ws');
}
init();
</script>
</body>
</html>