connect and list usernames methods and more comments
This commit is contained in:
parent
71d8f13d08
commit
b5bb18fe7f
@ -57,8 +57,8 @@ function runCommand(input) {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cmd_help() {
|
|
||||||
// Display 'help' message
|
// Display 'help' message
|
||||||
|
function cmd_help() {
|
||||||
return "Commands list:<br>" +
|
return "Commands list:<br>" +
|
||||||
" -about Information about this website<br>"+
|
" -about Information about this website<br>"+
|
||||||
" -clear Clear terminal screen<br>" +
|
" -clear Clear terminal screen<br>" +
|
||||||
@ -68,19 +68,19 @@ function cmd_help() {
|
|||||||
" -msg Open a direct chat to the provided user by name";
|
" -msg Open a direct chat to the provided user by name";
|
||||||
}
|
}
|
||||||
|
|
||||||
function cmd_about() {
|
|
||||||
// Display 'about' message
|
// Display 'about' message
|
||||||
|
function cmd_about() {
|
||||||
return "This website is based on the general idea and design of a terminal.<br>" +
|
return "This website is based on the general idea and design of a terminal.<br>" +
|
||||||
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
|
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
|
||||||
}
|
}
|
||||||
|
|
||||||
function cmd_clear() {
|
|
||||||
// Clear terminal by reloading page
|
// Clear terminal by reloading page
|
||||||
|
function cmd_clear() {
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
function cmd_history() {
|
|
||||||
// Display the command history line by line
|
// Display the command history line by line
|
||||||
|
function cmd_history() {
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
const hl = history.list;
|
const hl = history.list;
|
||||||
@ -92,6 +92,7 @@ function cmd_history() {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute arbitrary math and logic equations
|
||||||
function cmd_exec(input) {
|
function cmd_exec(input) {
|
||||||
// No input was given
|
// No input was given
|
||||||
if (input === undefined)
|
if (input === undefined)
|
||||||
@ -128,8 +129,9 @@ function cmd_echo(input) {
|
|||||||
function cmd_nick(input) {
|
function cmd_nick(input) {
|
||||||
if (input === undefined)
|
if (input === undefined)
|
||||||
return "No nickname was given!";
|
return "No nickname was given!";
|
||||||
user.name = input[0];
|
if (!user.connected)
|
||||||
sendNickname();
|
return "You are not connected!";
|
||||||
|
sendNickname(input[0]);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,3 +173,17 @@ function cmd_exit(error) {
|
|||||||
if (error)
|
if (error)
|
||||||
outputText({output: error});
|
outputText({output: error});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List all users to be able to chat with
|
||||||
|
function cmd_ll() {
|
||||||
|
if (!user.connected)
|
||||||
|
return "You are not connected!";
|
||||||
|
requestUsernames();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to server
|
||||||
|
function cmd_connect() {
|
||||||
|
if (user.connected)
|
||||||
|
return "You are already connected!";
|
||||||
|
connect();
|
||||||
|
}
|
@ -19,6 +19,7 @@ document.body.onunload = document.body.onbeforeunload = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Connect to server
|
// Connect to server
|
||||||
|
function connect() {
|
||||||
fetch('/connect').then(res => {
|
fetch('/connect').then(res => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
user.connected = true;
|
user.connected = true;
|
||||||
@ -33,10 +34,12 @@ fetch('/connect').then(res => {
|
|||||||
const {id} = res;
|
const {id} = res;
|
||||||
user.id = id;
|
user.id = id;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
connect();
|
||||||
|
|
||||||
|
|
||||||
// Send nickname to server, receive verification
|
// Send nickname to server, receive verification
|
||||||
function sendNickname() {
|
function sendNickname(name) {
|
||||||
fetch('/nickname', {
|
fetch('/nickname', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -44,8 +47,8 @@ function sendNickname() {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: user.name,
|
id: user.id,
|
||||||
id: user.id
|
name
|
||||||
})
|
})
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res.status === 200)
|
if (res.status === 200)
|
||||||
@ -54,13 +57,11 @@ function sendNickname() {
|
|||||||
// No valid status code
|
// No valid status code
|
||||||
if (res === undefined)
|
if (res === undefined)
|
||||||
return;
|
return;
|
||||||
|
// Receive name and save it
|
||||||
|
const { name } = res;
|
||||||
|
user.name = name;
|
||||||
// Reply whether name is taken or not
|
// Reply whether name is taken or not
|
||||||
if (res.success)
|
outputText({output: `Applied name '${name}'.`});
|
||||||
outputText({output: `Applied name '${user.name}'.`});
|
|
||||||
else {
|
|
||||||
outputText({output: `Name '${user.name}' is already taken!`});
|
|
||||||
user.name = "";
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +100,6 @@ function getChatMessages() {
|
|||||||
|
|
||||||
// Send a direct message, do not look at response
|
// Send a direct message, do not look at response
|
||||||
function directMessage(name, message) {
|
function directMessage(name, message) {
|
||||||
// Direct message
|
|
||||||
fetch('/message', {
|
fetch('/message', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -115,3 +115,24 @@ function directMessage(name, message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// function globalMessage(message) {}
|
// 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;
|
||||||
|
for (const u of res)
|
||||||
|
outputText({
|
||||||
|
preNext: `${u.id}: `,
|
||||||
|
output: u.name
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user