Generate bridge clientId once
This commit is contained in:
parent
ead57baf6f
commit
a1864e5880
1 changed files with 9 additions and 2 deletions
|
|
@ -10,6 +10,7 @@ let ws: WebSocket | null = null;
|
|||
let doc: Y.Doc | null = null;
|
||||
let text: Y.Text | null = null;
|
||||
let suppressLocal = 0;
|
||||
let clientId: number | null = null;
|
||||
|
||||
function send(msg: object) {
|
||||
console.log(JSON.stringify(msg));
|
||||
|
|
@ -32,6 +33,7 @@ function connect(roomName: string) {
|
|||
ws = new WebSocket(DAEMON_URL);
|
||||
|
||||
ws.onopen = () => {
|
||||
clientId = Math.floor(Math.random() * 1000000);
|
||||
ws?.send(JSON.stringify({ type: "join", room: roomName }));
|
||||
send({ type: "connected", room: roomName });
|
||||
};
|
||||
|
|
@ -161,12 +163,17 @@ for await (const chunk of Bun.stdin.stream()) {
|
|||
setContent(msg.text);
|
||||
break;
|
||||
case "cursor":
|
||||
if (ws && msg.line !== undefined && msg.col !== undefined) {
|
||||
if (
|
||||
ws &&
|
||||
clientId !== null &&
|
||||
msg.line !== undefined &&
|
||||
msg.col !== undefined
|
||||
) {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "awareness",
|
||||
data: {
|
||||
clientId: Math.floor(Math.random() * 1000000),
|
||||
clientId,
|
||||
cursor: { line: msg.line, col: msg.col },
|
||||
},
|
||||
}),
|
||||
|
|
|
|||
Loading…
Reference in a new issue