Files
marqdo/examples/marqdo-blog/public/live.js
cflmyandCursor da9fc75b1e Add marqdo-blog example with GFM-table style assembly
Introduce web_style ABI (样式装配) so themes are GFM style tables
(|选择器|属性|值|, optional |媒体| column for @media) assembled by
functions instead of hand-written CSS strings — 文档即代码. The new
examples/marqdo-blog shows a full blog: card grid, dynamic /post/{slug},
tag archive, WebSocket live widget, and session-gated admin CRUD, plus a
smoke test (15 checks) and README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-26 13:03:55 +08:00

41 lines
1.2 KiB
JavaScript

// 实时 WebSocket 小部件(连接 /live 端点)。
// 连接 ws://host/live,发送输入,展示服务器回显。
(function () {
var out = document.getElementById("live-out");
var input = document.getElementById("live-msg");
var btn = document.getElementById("live-send");
var status = document.getElementById("live-status");
if (!out || !input || !btn || !status) return;
var proto = location.protocol === "https:" ? "wss" : "ws";
var ws = new WebSocket(proto + "://" + location.host + "/live");
status.textContent = "connecting…";
ws.onopen = function () {
status.textContent = "connected";
btn.disabled = false;
};
ws.onmessage = function (ev) {
var p = document.createElement("p");
p.textContent = "echo: " + ev.data;
out.appendChild(p);
out.scrollTop = out.scrollHeight;
};
ws.onclose = function () {
status.textContent = "closed";
btn.disabled = true;
};
ws.onerror = function () {
status.textContent = "error";
btn.disabled = true;
};
btn.onclick = function () {
var m = input.value.trim();
if (m) ws.send(m);
};
input.addEventListener("keydown", function (e) {
if (e.key === "Enter") btn.onclick();
});
})();