implemented EventSource instead of regular update requests and minor clean up

This commit is contained in:
Baipyrus 2023-04-22 20:23:38 +02:00
parent 1ee17099e2
commit 369152494d
3 changed files with 22 additions and 46 deletions

View File

@ -82,7 +82,8 @@ function cmd_help() {
" -msg Open a direct chat to the provided user/-s by name<br>" +
" -ls List all connected users by name and id<br>" +
" -ping Ping the host to request two-way-delay<br>" +
" -chat Open a group chat to the provided chat/-s by name";
" -chat Open a group chat to the provided chat/-s by name<br>" +
" -logout Disconnect from the host. This deletes your user";
}
// Display 'about' message
@ -201,7 +202,19 @@ function messagingIniti(mode, name) {
pretext.current = `Chat (${name})> `;
// Set chat mode to direct messages and start pulling
chatMode = `${mode} ${name}`;
userData.chatPull = setInterval(getChatMessages, 500);
const ownName = window.localStorage.getItem("name");
const id = window.localStorage.getItem("id");
const encode = encodeURIComponent(`${ownName}#${id}`);
userData.chat = new EventSource(`/getChat?full=${encode}`);
userData.chat.onmessage = (event) => {
const data = JSON.parse(event.data);
outputText({
preNext: `${data.from}: `,
output: data.message
});
}
}
// Initialize direct chat
@ -222,10 +235,11 @@ function cmd_exit(error) {
pretext.current = pretext.original;
// Do individual resets
switch (level) {
// case "chat":
case "chat":
case "msg":
// Stop chat pulling
clearInterval(userData.chatPull);
userData.chat.close();
userData.chat = null;
break;
}

View File

@ -4,7 +4,7 @@ const pretext = {
current: "root@baipyr.us:~# "
};
const userData = { history: [], chatPull: null };
const userData = { history: [], chat: null };
let chatMode = "default", activityNotify;
@ -155,41 +155,6 @@ function sendNickname(name) {
});
}
// Pull incoming chat messages
function getChatMessages() {
fetch('/getChat', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
from: chatMode.split(' ')[1].split(','),
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id")
})
}).then(res => {
// Incoming messages
if (res.status === 200)
return res.json();
// No new messages
else if (res.status === 204)
return;
cmd_exit("Error occurred while fetching messages!");
}).then(res => {
// No messages or invalid status code
if (res === undefined)
return;
// Output all messages by user
for (const user in res)
for (const msg of res[user])
outputText({
preNext: `${user}: `,
output: msg
});
});
}
// Send a direct message, do not look at response
function directMessage(names, message) {
fetch('/message', {
@ -248,17 +213,14 @@ function requestUsernames() {
function requestPing() {
const startTime = new Date();
fetch('/ping', {
method: 'GET',
headers: {
'Accept': 'application/json',
}
method: 'GET'
}).then(res => {
if (res.status !== 200)
return;
// Output measured ping time
const diff = new Date() - startTime;
const output = `Host responded after ${diff}ms.`;
outputText({output})
outputText({output});
});
}