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
| import { Socket, SocketMessage } from "./socket"; import { req } from "./dom";
export function socketLog( debug: boolean, parentEl: HTMLElement, terminal: boolean, url: string, extraHandlers: Array<(m: SocketMessage) => void> ) { const o = () => { if (debug) { console.log("[socket]: open"); } };
const newRow = () => { if (terminal) { const row = document.createElement("tr"); const numTH = document.createElement("th"); numTH.innerText = parentEl.children.length.toString(); const textTD = document.createElement("td"); row.append(numTH, textTD); parentEl.append(row); return textTD; } const div = document.createElement("div"); parentEl.append(div); return div; };
let currRow: HTMLElement | null = null; const r = (m: SocketMessage) => { if (m.cmd !== "output") { if (extraHandlers.length === 0) { console.log("unknown command [" + m.cmd + "] received"); } else { for (const h of extraHandlers) { h(m); } } return; } if (m.param.html === undefined) { console.log("no [html] key in message param: " + JSON.stringify(m, null, 2)); } const html = m.param.html as string[];
let content = ""; for (const x of html) { if (!currRow) { currRow = newRow(); } if (x === "\n") { if (content === "") { content = " "; } currRow!.innerHTML += content; content = ""; currRow = null; } else { content += x; } } if (currRow) { currRow.innerHTML += content; } const c = req("#content"); c.scrollTo(0, c.scrollHeight); };
const e = (svc: string, err: string) => { console.error("socket error", svc, err); };
return new Socket(false, o, r, e, url); }
|