2023-01-19 11:57:43 +00:00
|
|
|
// Global networking variables
|
|
|
|
const user = { id: "", name: "", history: [], connected: false, chatPull: null };
|
|
|
|
let chatMode = "default";
|
|
|
|
|
|
|
|
|
|
|
|
// Tell server to disconnect
|
2023-01-18 14:19:22 +00:00
|
|
|
document.body.onunload = document.body.onbeforeunload = () => {
|
|
|
|
if (user.connected)
|
|
|
|
fetch('/disconnect', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-01-20 07:13:35 +00:00
|
|
|
name: user.name,
|
2023-01-18 14:19:22 +00:00
|
|
|
id: user.id
|
|
|
|
})
|
|
|
|
}).then(res => {});
|
|
|
|
};
|
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
// Connect to server
|
2023-01-20 07:13:35 +00:00
|
|
|
function connect(name) {
|
|
|
|
user.name = name;
|
|
|
|
fetch('/connect', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ name })
|
|
|
|
}).then(res => {
|
2023-01-19 14:31:13 +00:00
|
|
|
if (res.status === 200) {
|
|
|
|
user.connected = true;
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
// No valid status code was sent
|
|
|
|
console.error("Could not connect to server!");
|
|
|
|
}).then(res => {
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
|
|
|
// Save provided id
|
2023-01-20 07:13:35 +00:00
|
|
|
const { id } = res;
|
2023-01-19 14:31:13 +00:00
|
|
|
user.id = id;
|
2023-01-20 07:13:35 +00:00
|
|
|
outputText({output: `Connected as '${name}#${id}'.`});
|
2023-01-19 14:31:13 +00:00
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:19:22 +00:00
|
|
|
|
2023-01-19 11:57:43 +00:00
|
|
|
|
|
|
|
// Send nickname to server, receive verification
|
2023-01-19 14:31:13 +00:00
|
|
|
function sendNickname(name) {
|
2023-01-19 11:57:43 +00:00
|
|
|
fetch('/nickname', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-01-20 07:13:35 +00:00
|
|
|
oldName: user.name,
|
2023-01-19 14:31:13 +00:00
|
|
|
id: user.id,
|
|
|
|
name
|
2023-01-19 11:57:43 +00:00
|
|
|
})
|
|
|
|
}).then(res => {
|
|
|
|
if (res.status === 200)
|
|
|
|
return res.json();
|
|
|
|
}).then(res => {
|
|
|
|
// No valid status code
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-01-19 14:31:13 +00:00
|
|
|
// Receive name and save it
|
2023-01-20 07:13:35 +00:00
|
|
|
const { id } = res;
|
2023-01-19 14:31:13 +00:00
|
|
|
user.name = name;
|
2023-01-20 07:13:35 +00:00
|
|
|
user.id = id;
|
2023-01-19 11:57:43 +00:00
|
|
|
// Reply whether name is taken or not
|
2023-01-20 07:13:35 +00:00
|
|
|
outputText({output: `Applied name '${name}#${id}'.`});
|
2023-01-19 11:57:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull incoming chat messages
|
|
|
|
function getChatMessages() {
|
|
|
|
fetch('/getChat', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-01-20 07:13:35 +00:00
|
|
|
name: user.name,
|
2023-01-19 11:57:43 +00:00
|
|
|
id: user.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(name, message) {
|
|
|
|
fetch('/message', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2023-01-20 07:13:35 +00:00
|
|
|
recipient: name,
|
|
|
|
name: user.name,
|
2023-01-19 11:57:43 +00:00
|
|
|
id: user.id,
|
2023-01-20 07:13:35 +00:00
|
|
|
message
|
2023-01-19 11:57:43 +00:00
|
|
|
})
|
|
|
|
}).then(res => {});
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:31:13 +00:00
|
|
|
// function globalMessage(message) {}
|
|
|
|
|
|
|
|
// Request all usernames and number of anonymous users
|
|
|
|
function requestUsernames() {
|
|
|
|
fetch('/getNames', {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
}).then(res => {
|
|
|
|
if (res.status === 200)
|
|
|
|
return res.json();
|
|
|
|
}).then(res => {
|
|
|
|
if (res === undefined)
|
|
|
|
return;
|
2023-01-20 07:13:35 +00:00
|
|
|
let output = "";
|
2023-01-19 14:31:13 +00:00
|
|
|
for (const u of res)
|
2023-01-20 07:13:35 +00:00
|
|
|
output += `${u.name}#${u.id} `;
|
|
|
|
outputText({ output });
|
2023-01-19 14:31:13 +00:00
|
|
|
});
|
|
|
|
}
|