mirror of
https://github.com/therootcompany/golib.git
synced 2026-02-10 05:48:06 +00:00
f: add cmd/ssechat as ServerSentEvents demo
This commit is contained in:
parent
c1ba5b4744
commit
b6d8c2217a
@ -99,17 +99,17 @@
|
|||||||
// Connect to SSE
|
// Connect to SSE
|
||||||
const evtSource = new EventSource("/api/events");
|
const evtSource = new EventSource("/api/events");
|
||||||
|
|
||||||
evtSource.onmessage = (event) => {
|
evtSource.onmessage = function (ev) {
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(ev.data);
|
||||||
appendMessage(`${data.time} • ${data.nick}: ${data.text}`);
|
appendMessage(`${data.time} • ${data.nick}: ${data.text}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
evtSource.addEventListener("system", (event) => {
|
evtSource.addEventListener("system", function (ev) {
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(ev.data);
|
||||||
appendMessage(`${data.time} • ${data.text}`, "system");
|
appendMessage(`${data.time} • ${data.text}`, "system");
|
||||||
});
|
});
|
||||||
|
|
||||||
evtSource.onerror = () => {
|
evtSource.onerror = function () {
|
||||||
appendMessage("[Connection lost — reconnecting...]", "system");
|
appendMessage("[Connection lost — reconnecting...]", "system");
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -122,8 +122,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send message on form submit
|
// Send message on form submit
|
||||||
form.addEventListener("submit", async (e) => {
|
form.addEventListener("submit", async function (ev) {
|
||||||
e.preventDefault();
|
ev.preventDefault();
|
||||||
const text = msgInput.value.trim();
|
const text = msgInput.value.trim();
|
||||||
if (!text) return;
|
if (!text) return;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user