implemented EventSource instead of regular update requests and minor clean up
This commit is contained in:
parent
1ee17099e2
commit
369152494d
@ -82,7 +82,8 @@ function cmd_help() {
|
|||||||
" -msg Open a direct chat to the provided user/-s by name<br>" +
|
" -msg Open a direct chat to the provided user/-s by name<br>" +
|
||||||
" -ls List all connected users by name and id<br>" +
|
" -ls List all connected users by name and id<br>" +
|
||||||
" -ping Ping the host to request two-way-delay<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
|
// Display 'about' message
|
||||||
@ -201,7 +202,19 @@ function messagingIniti(mode, name) {
|
|||||||
pretext.current = `Chat (${name})> `;
|
pretext.current = `Chat (${name})> `;
|
||||||
// Set chat mode to direct messages and start pulling
|
// Set chat mode to direct messages and start pulling
|
||||||
chatMode = `${mode} ${name}`;
|
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
|
// Initialize direct chat
|
||||||
@ -222,10 +235,11 @@ function cmd_exit(error) {
|
|||||||
pretext.current = pretext.original;
|
pretext.current = pretext.original;
|
||||||
// Do individual resets
|
// Do individual resets
|
||||||
switch (level) {
|
switch (level) {
|
||||||
// case "chat":
|
case "chat":
|
||||||
case "msg":
|
case "msg":
|
||||||
// Stop chat pulling
|
// Stop chat pulling
|
||||||
clearInterval(userData.chatPull);
|
userData.chat.close();
|
||||||
|
userData.chat = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ textIn.oninput = () => {
|
|||||||
textIn.value = "";
|
textIn.value = "";
|
||||||
updateCursor();
|
updateCursor();
|
||||||
};
|
};
|
||||||
// Auto focus on start
|
// Autofocus on start
|
||||||
textIn.focus();
|
textIn.focus();
|
||||||
|
|
||||||
// Key Listener
|
// Key Listener
|
||||||
|
@ -4,7 +4,7 @@ const pretext = {
|
|||||||
current: "root@baipyr.us:~# "
|
current: "root@baipyr.us:~# "
|
||||||
};
|
};
|
||||||
|
|
||||||
const userData = { history: [], chatPull: null };
|
const userData = { history: [], chat: null };
|
||||||
let chatMode = "default", activityNotify;
|
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
|
// Send a direct message, do not look at response
|
||||||
function directMessage(names, message) {
|
function directMessage(names, message) {
|
||||||
fetch('/message', {
|
fetch('/message', {
|
||||||
@ -248,17 +213,14 @@ function requestUsernames() {
|
|||||||
function requestPing() {
|
function requestPing() {
|
||||||
const startTime = new Date();
|
const startTime = new Date();
|
||||||
fetch('/ping', {
|
fetch('/ping', {
|
||||||
method: 'GET',
|
method: 'GET'
|
||||||
headers: {
|
|
||||||
'Accept': 'application/json',
|
|
||||||
}
|
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res.status !== 200)
|
if (res.status !== 200)
|
||||||
return;
|
return;
|
||||||
// Output measured ping time
|
// Output measured ping time
|
||||||
const diff = new Date() - startTime;
|
const diff = new Date() - startTime;
|
||||||
const output = `Host responded after ${diff}ms.`;
|
const output = `Host responded after ${diff}ms.`;
|
||||||
outputText({output})
|
outputText({output});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user