better debug

This commit is contained in:
Holly Stubbs 2024-10-08 22:55:01 +01:00
parent 57ca5acc7d
commit 5b8e8f9037
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
3 changed files with 51 additions and 16 deletions

View file

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name MultiProbe // @name MultiProbe
// @namespace https://*.angusnicneven.com/* // @namespace https://*.angusnicneven.com/*
// @version 20241008.1 // @version 20241008.2
// @description Probe with friends! // @description Probe with friends!
// @author tgpholly // @author tgpholly
// @match https://*.angusnicneven.com/* // @match https://*.angusnicneven.com/*
@ -55,7 +55,7 @@ console.log("[MP] MultiProbe init");
'use strict'; 'use strict';
// Make sure to change the userscript version too!!!!!!!!!! // Make sure to change the userscript version too!!!!!!!!!!
const USERSCRIPT_VERSION_RAW = "20241008.1"; const USERSCRIPT_VERSION_RAW = "20241008.2";
const USERSCRIPT_VERSION = parseInt(USERSCRIPT_VERSION_RAW.replace(".", "")); const USERSCRIPT_VERSION = parseInt(USERSCRIPT_VERSION_RAW.replace(".", ""));
if (!continueRunningScript) { if (!continueRunningScript) {
@ -510,19 +510,29 @@ mp_button {
}, 1000); }, 1000);
const debugMessageContainer = document.createElement("mp_container"); const debugMessageContainer = document.createElement("mp_container");
debugMessageContainer.style = "position:fixed;top:0;left:0;padding:4px;pointer-events:none;text-shadow: 0px 0px 4px black"; debugMessageContainer.style = "position:fixed;top:0;left:0;padding:8px;margin:4px;pointer-events:none;text-shadow: 0px 0px 4px black;z-index:999999999999;background-color:rgba(0,0,0,0.5);outline:1px solid white";
document.documentElement.appendChild(debugMessageContainer); if (!window.multiprobe_debug) {
debugMessageContainer.style.display = "none";
}
if (window.multiprobe_debug) {
const debugText = document.createElement("mp_text"); const debugText = document.createElement("mp_text");
debugText.innerText = "RUNNING IN DEBUG"; debugText.innerText = "MultiProbe DEBUG";
debugText.style.color = "rgb(255, 200, 200)";
const debugConnectedTo = document.createElement("mp_text");
debugConnectedTo.innerText = "Disconnected";
const debugAuthedAs = document.createElement("mp_text");
debugAuthedAs.innerText = "Not Authenticated";
const debugPing = document.createElement("mp_text");
debugPing.innerText = "Ping: Calculating...";
debugMessageContainer.appendChild(debugText); debugMessageContainer.appendChild(debugText);
} debugMessageContainer.appendChild(debugConnectedTo);
if (window.multiprobe_connectLocal) { debugMessageContainer.appendChild(debugAuthedAs);
const debugConnectLocal = document.createElement("mp_text"); debugMessageContainer.appendChild(debugPing);
debugConnectLocal.innerText = "CONNECTED TO DEV SERVER"; document.documentElement.appendChild(debugMessageContainer);
debugMessageContainer.appendChild(debugConnectLocal);
}
let needsToUpdate = false; let needsToUpdate = false;
console.log("[MP] Checking for new versions..."); console.log("[MP] Checking for new versions...");
@ -712,8 +722,16 @@ mp_button {
window.onkeydown = (e) => { window.onkeydown = (e) => {
if (!windowContainer && e.key === "F1") { if (!windowContainer && e.key === "F1") {
e.preventDefault();
localStorage["mpshowfirsttime"] = "true"; localStorage["mpshowfirsttime"] = "true";
createFirstTimeDialog(); createFirstTimeDialog();
return false;
}
if (e.key === "F3") {
e.preventDefault();
window.multiprobe_debug = !window.multiprobe_debug;
debugMessageContainer.style.display = window.multiprobe_debug ? "block" : "none";
return false;
} }
}; };
@ -770,27 +788,38 @@ mp_button {
console.log("[MP] Connecting to realtime server..."); console.log("[MP] Connecting to realtime server...");
ws = new WebSocket((windowLocation.includes("//localhost:") || window.multiprobe_connectLocal) ? "wss://ws.eusv.net/t00mpdev" : "wss://ws.eusv.net/t00mp"); ws = new WebSocket((windowLocation.includes("//localhost:") || window.multiprobe_connectLocal) ? "wss://ws.eusv.net/t00mpdev" : "wss://ws.eusv.net/t00mp");
let keepAliveInterval; let keepAliveInterval;
let pingStartTime = -1;
let lastPing = 0;
ws.onopen = () => { ws.onopen = () => {
console.log("[MP] Connected! Authenticating..."); console.log("[MP] Connected! Authenticating...");
debugConnectedTo.innerText = `Connected to ${ws.url}`;
otherCursors.innerHTML = ""; otherCursors.innerHTML = "";
selfCursor = new RemoteClient(localStorage["t00mp_username"], false, true); selfCursor = new RemoteClient(localStorage["t00mp_username"], false, true);
selfCursor.probeImage.style.visibility = "hidden"; selfCursor.probeImage.style.visibility = "hidden";
selfCursor.element.style.visibility = localStorage["t00mp_cursorStyle"] ?? "hidden"; selfCursor.element.style.visibility = localStorage["t00mp_cursorStyle"] ?? "hidden";
selfCursor.hasBeenMoved = true; selfCursor.hasBeenMoved = true;
const currentPage = windowLocation.split("/").slice(2).join("/"); const currentPage = windowLocation.split("/").slice(2).join("/");
ws.send(keepAlivePacket);
ws.send(createWriter(Endian.LE, 4 + apiKey.length + currentPage.length) ws.send(createWriter(Endian.LE, 4 + apiKey.length + currentPage.length)
.writeUByte(MessageType.ClientDetails) .writeUByte(MessageType.ClientDetails)
.writeShortString(apiKey) .writeShortString(apiKey)
.writeString(currentPage) .writeString(currentPage)
.toBuffer()); .toBuffer());
keepAliveInterval = setInterval(() => { keepAliveInterval = setInterval(() => {
pingStartTime = performance.now();
ws.send(keepAlivePacket); ws.send(keepAlivePacket);
}, 5000); }, 2000);
} }
function onMessage(buf) { function onMessage(buf) {
const reader = createReader(Endian.LE, Buffer.from(buf)); const reader = createReader(Endian.LE, Buffer.from(buf));
switch (reader.readByte()) { switch (reader.readByte()) {
case MessageType.KeepAlive:
{
lastPing = performance.now() - pingStartTime;
debugPing.innerText = `Ping: ${lastPing}ms`;
break;
}
case MessageType.Clients: case MessageType.Clients:
{ {
const clientCount = reader.readUShort(); const clientCount = reader.readUShort();
@ -813,6 +842,7 @@ mp_button {
if (!needsToUpdate) { if (!needsToUpdate) {
createFirstTimeDialog(); createFirstTimeDialog();
} }
debugAuthedAs.innerText = `Authenticated as ${localStorage["t00mp_username"]}`;
console.log("[MP] Authenticated!"); console.log("[MP] Authenticated!");
break; break;
} }
@ -914,6 +944,8 @@ mp_button {
if (keepAliveInterval) { if (keepAliveInterval) {
clearInterval(keepAliveInterval); clearInterval(keepAliveInterval);
keepAliveInterval = undefined; keepAliveInterval = undefined;
debugConnectedTo.innerText = "Disconnected";
debugAuthedAs.innerText = "Not Authenticated";
} }
ws = undefined; ws = undefined;
ready = false; ready = false;

View file

@ -194,6 +194,8 @@ async function updateConnectionMetrics() {
onlineUsersUnique.Value = userCount; onlineUsersUnique.Value = userCount;
} }
const keepalivePacket = createWriter(Endian.LE, 1).writeByte(MessageType.KeepAlive).toBuffer();
websocketServer.on("connection", (socket) => { websocketServer.on("connection", (socket) => {
const myUUID = crypto.randomUUID(); const myUUID = crypto.randomUUID();
let user:RemoteUser; let user:RemoteUser;
@ -257,6 +259,7 @@ websocketServer.on("connection", (socket) => {
if (user !== undefined) { if (user !== undefined) {
user.lastKeepAliveTime = Date.now(); user.lastKeepAliveTime = Date.now();
} }
socket.send(keepalivePacket);
break; break;
} }
case MessageType.ClientDetails: case MessageType.ClientDetails:

@ -1 +1 @@
Subproject commit f5432ffac9a4f03ccee22bf31de1fe871040f780 Subproject commit f701cd6715b9b8e4ddaf8126c3d4cd58339f3954