localstorage and disconnect
This commit is contained in:
parent
6f1ce9fcad
commit
586cb1738c
@ -18,7 +18,6 @@
|
|||||||
██████╔╝██║ ██║██║██║ ██║ ██║ ██║██╗╚██████╔╝███████║
|
██████╔╝██║ ██║██║██║ ██║ ██║ ██║██╗╚██████╔╝███████║
|
||||||
╚═════╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚══════╝
|
╚═════╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚══════╝
|
||||||
</pre>
|
</pre>
|
||||||
<a>root@baipyr.us:~# </a>
|
|
||||||
<a id="current"></a>
|
<a id="current"></a>
|
||||||
<input type="text" id="input">
|
<input type="text" id="input">
|
||||||
</div>
|
</div>
|
||||||
|
@ -152,7 +152,7 @@ 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!";
|
||||||
if (user.connected)
|
if (!!window.localStorage.getItem("connected"))
|
||||||
sendNickname(input[0]);
|
sendNickname(input[0]);
|
||||||
else
|
else
|
||||||
connect(input[0]);
|
connect(input[0]);
|
||||||
@ -163,25 +163,23 @@ function cmd_nick(input) {
|
|||||||
function cmd_msg(input) {
|
function cmd_msg(input) {
|
||||||
if (input === undefined)
|
if (input === undefined)
|
||||||
return "No recipient was given!";
|
return "No recipient was given!";
|
||||||
if (!user.connected)
|
if (!window.localStorage.getItem("connected"))
|
||||||
return "You are not connected! Use the 'nick' command to connect using your username.";
|
return "You are not connected! Use the 'nick' command to connect using your username.";
|
||||||
if (user.name === "")
|
if (window.localStorage.getItem("name") === "")
|
||||||
return "You do not have a name!";
|
return "You do not have a name!";
|
||||||
// Get recipient username without spaces and pretext
|
// Get recipient username without spaces and pretext
|
||||||
const username = input[0];
|
const username = input[0];
|
||||||
pretext.current = `Chat (${username})> `;
|
pretext.current = `Chat (${username})> `;
|
||||||
// Set chat mode to direct messages and start pulling
|
// Set chat mode to direct messages and start pulling
|
||||||
chatMode = `msg ${username}`;
|
chatMode = `msg ${username}`;
|
||||||
user.chatPull = setInterval(getChatMessages, 500);
|
userData.chatPull = setInterval(getChatMessages, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit current level (example: chat -> main)
|
// Exit current level (example: chat -> main)
|
||||||
function cmd_exit(error) {
|
function cmd_exit(error) {
|
||||||
const level = chatMode.split(' ')[0];
|
const level = chatMode.split(' ')[0];
|
||||||
if (level === "default") {
|
if (level === "default")
|
||||||
console.log("Test!");
|
|
||||||
return "Already at top-level!";
|
return "Already at top-level!";
|
||||||
}
|
|
||||||
|
|
||||||
// Set mode to default and reset pretext
|
// Set mode to default and reset pretext
|
||||||
chatMode = "default";
|
chatMode = "default";
|
||||||
@ -191,7 +189,7 @@ function cmd_exit(error) {
|
|||||||
// case "chat":
|
// case "chat":
|
||||||
case "msg":
|
case "msg":
|
||||||
// Stop chat pulling
|
// Stop chat pulling
|
||||||
clearInterval(user.chatPull);
|
clearInterval(userData.chatPull);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +200,7 @@ function cmd_exit(error) {
|
|||||||
|
|
||||||
// List all users to be able to chat with
|
// List all users to be able to chat with
|
||||||
function cmd_ls() {
|
function cmd_ls() {
|
||||||
if (!user.connected)
|
if (!window.localStorage.getItem("connected"))
|
||||||
return "You are not connected! Use the 'nick' command to connect using your username.";
|
return "You are not connected! Use the 'nick' command to connect using your username.";
|
||||||
requestUsernames();
|
requestUsernames();
|
||||||
return null;
|
return null;
|
||||||
@ -210,6 +208,15 @@ function cmd_ls() {
|
|||||||
|
|
||||||
// Ping host for two-way-delay
|
// Ping host for two-way-delay
|
||||||
function cmd_ping() {
|
function cmd_ping() {
|
||||||
|
if (!window.localStorage.getItem("connected"))
|
||||||
|
return "You are not connected! Use the 'nick' command to connect using your username.";
|
||||||
requestPing();
|
requestPing();
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// User wants to log out
|
||||||
|
function cmd_logout() {
|
||||||
|
if (!window.localStorage.getItem("connected"))
|
||||||
|
return "You are not even connected yet!";
|
||||||
|
disconnect();
|
||||||
}
|
}
|
11
js/main.js
11
js/main.js
@ -1,8 +1,3 @@
|
|||||||
let pretext = {
|
|
||||||
original: "root@baipyr.us:~# ",
|
|
||||||
current: "root@baipyr.us:~# "
|
|
||||||
};
|
|
||||||
|
|
||||||
// Initiating variables
|
// Initiating variables
|
||||||
const length = pretext.current.length+1;
|
const length = pretext.current.length+1;
|
||||||
let cursorPosition = 0;
|
let cursorPosition = 0;
|
||||||
@ -14,6 +9,10 @@ const tbDiv = document.getElementById("textbox");
|
|||||||
const textIn = document.getElementById("input");
|
const textIn = document.getElementById("input");
|
||||||
const textCur = document.getElementById("current");
|
const textCur = document.getElementById("current");
|
||||||
const cursor = document.getElementById("cursor");
|
const cursor = document.getElementById("cursor");
|
||||||
|
// Set initial pretext
|
||||||
|
const pretextElement = document.createElement('a');
|
||||||
|
pretextElement.innerHTML = pretext.original;
|
||||||
|
tbDiv.insertBefore(pretextElement, tbDiv.children[1]);
|
||||||
// Initial cursor offset
|
// Initial cursor offset
|
||||||
cursor.style.transform = `translate(${length-1}ch,${2.222*7-0.4}ch)`;
|
cursor.style.transform = `translate(${length-1}ch,${2.222*7-0.4}ch)`;
|
||||||
|
|
||||||
@ -188,7 +187,7 @@ function createText(str, safe=true) {
|
|||||||
// Rename last-posted pretext to own username
|
// Rename last-posted pretext to own username
|
||||||
function renameToSelf() {
|
function renameToSelf() {
|
||||||
const tbc = tbDiv.children;
|
const tbc = tbDiv.children;
|
||||||
tbc[tbc.length - 3].innerHTML = `${user.name}#${user.id}: `;
|
tbc[tbc.length - 3].innerHTML = `${window.localStorage.getItem("name")}#${window.localStorage.getItem("id")}: `;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cursor position based on coordinates
|
// Update cursor position based on coordinates
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
// Global networking variables
|
// Global networking variables
|
||||||
const user = { id: "", name: "", history: [], connected: false, chatPull: null };
|
let pretext = {
|
||||||
|
original: "root@baipyr.us:~# ",
|
||||||
|
current: "root@baipyr.us:~# "
|
||||||
|
};
|
||||||
|
|
||||||
|
const userData = { history: [], chatPull: null };
|
||||||
let chatMode = "default";
|
let chatMode = "default";
|
||||||
|
|
||||||
|
if (window.localStorage.getItem("id") === null)
|
||||||
|
window.localStorage.setItem("id", "");
|
||||||
|
if (window.localStorage.getItem("name") === null)
|
||||||
|
window.localStorage.setItem("name", "");
|
||||||
|
if (window.localStorage.getItem("connected") === null)
|
||||||
|
window.localStorage.setItem("connected", "");
|
||||||
|
else if (!!window.localStorage.getItem("connected"))
|
||||||
|
pretext.original = pretext.current = `${window.localStorage.getItem("name")}@baipyr.us:~# `;
|
||||||
|
|
||||||
|
|
||||||
// Tell server to disconnect
|
// Tell server to disconnect
|
||||||
document.body.onunload = document.body.onbeforeunload = () => {
|
function disconnect() {
|
||||||
if (user.connected)
|
if (!!window.localStorage.getItem("connected")) {
|
||||||
fetch('/disconnect', {
|
fetch('/disconnect', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -13,15 +27,20 @@ document.body.onunload = document.body.onbeforeunload = () => {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: user.name,
|
name: window.localStorage.getItem("name"),
|
||||||
id: user.id
|
id: window.localStorage.getItem("id")
|
||||||
})
|
})
|
||||||
}).then(res => {});
|
}).then(res => {});
|
||||||
};
|
window.localStorage.setItem("id", "");
|
||||||
|
window.localStorage.setItem("name", "");
|
||||||
|
window.localStorage.setItem("connected", "");
|
||||||
|
pretext.original = pretext.current = "root@baipyr.us:~# ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Connect to server
|
// Connect to server
|
||||||
function connect(name) {
|
function connect(name) {
|
||||||
user.name = name;
|
window.localStorage.setItem("name", name);
|
||||||
fetch('/connect', {
|
fetch('/connect', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -31,7 +50,7 @@ function connect(name) {
|
|||||||
body: JSON.stringify({ name })
|
body: JSON.stringify({ name })
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
user.connected = true;
|
window.localStorage.setItem("connected", "1");
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
// No valid status code was sent
|
// No valid status code was sent
|
||||||
@ -41,8 +60,8 @@ function connect(name) {
|
|||||||
return;
|
return;
|
||||||
// Save provided id
|
// Save provided id
|
||||||
const { id } = res;
|
const { id } = res;
|
||||||
user.id = id;
|
window.localStorage.setItem("id", id);
|
||||||
pretext.original = pretext.current = `${user.name}@baipyr.us:~# `;
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
||||||
outputText({output: `Connected as '${name}#${id}'.`});
|
outputText({output: `Connected as '${name}#${id}'.`});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -50,7 +69,8 @@ function connect(name) {
|
|||||||
|
|
||||||
// Send nickname to server, receive verification
|
// Send nickname to server, receive verification
|
||||||
function sendNickname(name) {
|
function sendNickname(name) {
|
||||||
user.name = name;
|
const oldName = window.localStorage.getItem("name");
|
||||||
|
window.localStorage.setItem("name", name);
|
||||||
fetch('/nickname', {
|
fetch('/nickname', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -58,8 +78,8 @@ function sendNickname(name) {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
oldName: user.name,
|
id: window.localStorage.getItem("id"),
|
||||||
id: user.id,
|
oldName,
|
||||||
name
|
name
|
||||||
})
|
})
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
@ -71,8 +91,8 @@ function sendNickname(name) {
|
|||||||
return;
|
return;
|
||||||
// Receive name and save it
|
// Receive name and save it
|
||||||
const { id } = res;
|
const { id } = res;
|
||||||
user.id = id;
|
window.localStorage.setItem("id", id);
|
||||||
pretext.original = pretext.current = `${user.name}@baipyr.us:~# `;
|
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
|
||||||
// Reply whether name is taken or not
|
// Reply whether name is taken or not
|
||||||
outputText({output: `Applied name '${name}#${id}'.`});
|
outputText({output: `Applied name '${name}#${id}'.`});
|
||||||
});
|
});
|
||||||
@ -87,8 +107,8 @@ function getChatMessages() {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
name: user.name,
|
name: window.localStorage.getItem("name"),
|
||||||
id: user.id
|
id: window.localStorage.getItem("id")
|
||||||
})
|
})
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
// Incoming messages
|
// Incoming messages
|
||||||
@ -122,8 +142,8 @@ function directMessage(name, message) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
recipient: name,
|
recipient: name,
|
||||||
name: user.name,
|
name: window.localStorage.getItem("name"),
|
||||||
id: user.id,
|
id: window.localStorage.getItem("id"),
|
||||||
message
|
message
|
||||||
})
|
})
|
||||||
}).then(res => {});
|
}).then(res => {});
|
||||||
|
Loading…
Reference in New Issue
Block a user