From 91ab64a49146b154852a46fa25a7f65a534a2c69 Mon Sep 17 00:00:00 2001 From: Holly Date: Fri, 19 Apr 2024 00:18:12 +0100 Subject: [PATCH] keep track of cursor position serverside --- client/Terminal-00-Multiuser.user.js | 17 ++++++++++++++--- server/index.ts | 22 ++++++++++++++-------- server/objects/User.ts | 2 ++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/client/Terminal-00-Multiuser.user.js b/client/Terminal-00-Multiuser.user.js index 503831c..4bebd69 100644 --- a/client/Terminal-00-Multiuser.user.js +++ b/client/Terminal-00-Multiuser.user.js @@ -141,6 +141,11 @@ if (!window.TE_ACTIVE) { this.targetY = y; } + rawSetPosInit(x, y) { + this.actualX = this.targetX = Math.round(x * window.innerWidth); + this.actualY = this.targetY = y; + } + updateCursor() { const x = Math.round(this.actualX); const y = Math.round(this.actualY); @@ -227,7 +232,7 @@ if (!window.TE_ACTIVE) { function doConnect() { const Buffer = getBufferClass(); - ws = new WebSocket("wss://ws.eusv.net/t00mp"); + ws = new WebSocket(window.location.href.includes("localhost") ? "ws://localhost:38195" : "wss://ws.eusv.net/t00mp"); let keepAliveInterval; ws.onopen = () => { const currentPage = window.location.href.split("/").slice(3).join("/"); @@ -246,7 +251,9 @@ if (!window.TE_ACTIVE) { for (let i = 0; i < clientCount; i++) { const clientId = reader.readUInt(); const clientName = reader.readShortString(); - remoteClients.set(clientId, new RemoteClient(clientName)); + const clientX = reader.readFloat(); + const clientY = reader.readInt(); + remoteClients.set(clientId, new RemoteClient(clientName)).rawSetPosInit(clientX, clientY); } ready = true; break; @@ -336,9 +343,13 @@ if (!window.TE_ACTIVE) { buttons.style.marginTop = "1rem"; dialog.appendChild(buttons); const submitButton = document.createElement("button"); - submitButton.innerText = "Connect"; + submitButton.innerText = localStorage["t00mp_username"] !== "" ? "Connect" : "Change Username"; submitButton.onclick = () => submitFunction(null); buttons.appendChild(submitButton); + if (localStorage["t00mp_username"] !== "") { + const disconnectButton = document.createElement("button"); + disconnectButton.innerText = "Disconnect"; + } const doNotButton = document.createElement("button"); doNotButton.innerText = "Close"; doNotButton.onclick = () => bg.remove(); diff --git a/server/index.ts b/server/index.ts index aa622ea..3daf3a1 100644 --- a/server/index.ts +++ b/server/index.ts @@ -19,6 +19,14 @@ function sendToAllButSelf(user:User, data:Buffer) { }); } +function sendToAll(user:User, data:Buffer) { + users.forEach(otherUser => { + if (otherUser.currentURL === user.currentURL) { + otherUser.send(data); + } + }); +} + server.on("connection", (socket) => { const myUUID = crypto.randomUUID(); let user:User; @@ -54,9 +62,9 @@ server.on("connection", (socket) => { await users.forEach(otherUser => { lengthOfUsernames += otherUser.username.length + 1; // + 1 for length byte }); - const usersToSend = createWriter(Endian.LE, 3 + (users.length * 4) + lengthOfUsernames).writeByte(MessageType.Clients).writeUShort(users.length); + const usersToSend = createWriter(Endian.LE, 3 + (users.length * 12) + lengthOfUsernames).writeByte(MessageType.Clients).writeUShort(users.length); await users.forEach(otherUser => { - usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username); + usersToSend.writeUInt(otherUser.id).writeShortString(otherUser.username).writeFloat(otherUser.cursorX).writeInt(otherUser.cursorY); }); user = users.set(myUUID, new User(socket, username, page)); sendToAllButSelf(user, createWriter(Endian.LE, 6 + username.length).writeByte(MessageType.ClientJoined).writeUInt(user.id).writeShortString(username).toBuffer()); @@ -67,9 +75,9 @@ server.on("connection", (socket) => { if (user === undefined) { return; } - const cursorX = reader.readFloat(); - const cursorY = reader.readInt(); - sendToAllButSelf(user, createWriter(Endian.LE, 13).writeByte(MessageType.CursorPos).writeUInt(user.id).writeFloat(cursorX).writeInt(cursorY).toBuffer()); + user.cursorX = reader.readFloat(); + user.cursorY = reader.readInt(); + sendToAllButSelf(user, createWriter(Endian.LE, 13).writeByte(MessageType.CursorPos).writeUInt(user.id).writeFloat(user.cursorX).writeInt(user.cursorY).toBuffer()); break; } case MessageType.Ping: @@ -77,9 +85,7 @@ server.on("connection", (socket) => { const cursorX = reader.readFloat(); const cursorY = reader.readInt(); const packet = createWriter(Endian.LE, 9).writeByte(MessageType.Ping).writeFloat(cursorX).writeInt(cursorY).toBuffer(); - users.forEach(otherUser => { - otherUser.send(packet); - }); + sendToAll(user, packet); break; } } diff --git a/server/objects/User.ts b/server/objects/User.ts index 2d09009..49132d9 100644 --- a/server/objects/User.ts +++ b/server/objects/User.ts @@ -7,6 +7,8 @@ export default class User { public readonly id:number; public readonly username:string; public readonly currentURL:string; + public cursorX:number = 0; + public cursorY:number = 0; constructor(socket:WebSocket, username:string, currentURL:string) { this.socket = socket;