diff --git a/js/commands.js b/js/commands.js
index 01bb058..4acd4be 100644
--- a/js/commands.js
+++ b/js/commands.js
@@ -57,8 +57,8 @@ function runCommand(input) {
return output;
}
+// Display 'help' message
function cmd_help() {
- // Display 'help' message
return "Commands list:
" +
" -about Information about this website
"+
" -clear Clear terminal screen
" +
@@ -68,19 +68,19 @@ function cmd_help() {
" -msg Open a direct chat to the provided user by name";
}
+// Display 'about' message
function cmd_about() {
- // Display 'about' message
return "This website is based on the general idea and design of a terminal.
" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
}
+// Clear terminal by reloading page
function cmd_clear() {
- // Clear terminal by reloading page
window.location.reload();
}
+// Display the command history line by line
function cmd_history() {
- // Display the command history line by line
let output = "";
const hl = history.list;
@@ -92,6 +92,7 @@ function cmd_history() {
return output;
}
+// Execute arbitrary math and logic equations
function cmd_exec(input) {
// No input was given
if (input === undefined)
@@ -128,8 +129,9 @@ function cmd_echo(input) {
function cmd_nick(input) {
if (input === undefined)
return "No nickname was given!";
- user.name = input[0];
- sendNickname();
+ if (!user.connected)
+ return "You are not connected!";
+ sendNickname(input[0]);
return null;
}
@@ -170,4 +172,18 @@ function cmd_exit(error) {
// Exit was called automatically. Print error.
if (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();
}
\ No newline at end of file
diff --git a/js/networking.js b/js/networking.js
index b301821..71bfb86 100644
--- a/js/networking.js
+++ b/js/networking.js
@@ -19,24 +19,27 @@ document.body.onunload = document.body.onbeforeunload = () => {
};
// Connect to server
-fetch('/connect').then(res => {
- 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
- const { id } = res;
- user.id = id;
-});
+function connect() {
+ fetch('/connect').then(res => {
+ 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
+ const {id} = res;
+ user.id = id;
+ });
+}
+connect();
// Send nickname to server, receive verification
-function sendNickname() {
+function sendNickname(name) {
fetch('/nickname', {
method: 'POST',
headers: {
@@ -44,8 +47,8 @@ function sendNickname() {
'Content-Type': 'application/json'
},
body: JSON.stringify({
- name: user.name,
- id: user.id
+ id: user.id,
+ name
})
}).then(res => {
if (res.status === 200)
@@ -54,13 +57,11 @@ function sendNickname() {
// No valid status code
if (res === undefined)
return;
+ // Receive name and save it
+ const { name } = res;
+ user.name = name;
// Reply whether name is taken or not
- if (res.success)
- outputText({output: `Applied name '${user.name}'.`});
- else {
- outputText({output: `Name '${user.name}' is already taken!`});
- user.name = "";
- }
+ outputText({output: `Applied name '${name}'.`});
});
}
@@ -99,7 +100,6 @@ function getChatMessages() {
// Send a direct message, do not look at response
function directMessage(name, message) {
- // Direct message
fetch('/message', {
method: 'POST',
headers: {
@@ -114,4 +114,25 @@ function directMessage(name, message) {
}).then(res => {});
}
-// function globalMessage(message) {}
\ No newline at end of file
+// 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
+ });
+ });
+}
\ No newline at end of file