Generate bridge clientId once

This commit is contained in:
Jared Miller 2026-01-27 20:49:54 -05:00
parent ead57baf6f
commit a1864e5880
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C

View file

@ -10,6 +10,7 @@ let ws: WebSocket | null = null;
let doc: Y.Doc | null = null; let doc: Y.Doc | null = null;
let text: Y.Text | null = null; let text: Y.Text | null = null;
let suppressLocal = 0; let suppressLocal = 0;
let clientId: number | null = null;
function send(msg: object) { function send(msg: object) {
console.log(JSON.stringify(msg)); console.log(JSON.stringify(msg));
@ -32,6 +33,7 @@ function connect(roomName: string) {
ws = new WebSocket(DAEMON_URL); ws = new WebSocket(DAEMON_URL);
ws.onopen = () => { ws.onopen = () => {
clientId = Math.floor(Math.random() * 1000000);
ws?.send(JSON.stringify({ type: "join", room: roomName })); ws?.send(JSON.stringify({ type: "join", room: roomName }));
send({ type: "connected", room: roomName }); send({ type: "connected", room: roomName });
}; };
@ -161,12 +163,17 @@ for await (const chunk of Bun.stdin.stream()) {
setContent(msg.text); setContent(msg.text);
break; break;
case "cursor": case "cursor":
if (ws && msg.line !== undefined && msg.col !== undefined) { if (
ws &&
clientId !== null &&
msg.line !== undefined &&
msg.col !== undefined
) {
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
type: "awareness", type: "awareness",
data: { data: {
clientId: Math.floor(Math.random() * 1000000), clientId,
cursor: { line: msg.line, col: msg.col }, cursor: { line: msg.line, col: msg.col },
}, },
}), }),