From 6f1ce9fcad60ba0a441900eb07213fa9cdb136ab Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 30 Jan 2023 08:17:32 +0100 Subject: [PATCH] ping command for two-way-delay --- js/commands.js | 9 ++++++++- js/networking.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/js/commands.js b/js/commands.js index 5dcfc1d..4369916 100644 --- a/js/commands.js +++ b/js/commands.js @@ -67,7 +67,8 @@ function cmd_help() { " -exec Execute arbitrary math and logic equations
" + " -nick Choose your username. Do not use spaces in it
" + " -msg Open a direct chat to the provided user by name
" + - " -ls List all connected users by name and id"; + " -ls List all connected users by name and id
" + + " -ping Ping the host to request two-way-delay"; } // Display 'about' message @@ -205,4 +206,10 @@ function cmd_ls() { return "You are not connected! Use the 'nick' command to connect using your username."; requestUsernames(); return null; +} + +// Ping host for two-way-delay +function cmd_ping() { + requestPing(); + return null; } \ No newline at end of file diff --git a/js/networking.js b/js/networking.js index 3b8f4ff..6f89ac1 100644 --- a/js/networking.js +++ b/js/networking.js @@ -149,4 +149,16 @@ function requestUsernames() { output += `${u.name}#${u.id} `; outputText({ output }); }); +} + +// Ping host for two-way-delay +function requestPing() { + const startTime = new Date(); + fetch('/ping').then(res => { + if (res.status !== 200) + return; + const diff = new Date() - startTime; + const output = `Host responded after ${diff}ms.`; + outputText({output}) + }); } \ No newline at end of file