fixed scrolling and buffer times- oh yeah also huge backend overhual
This commit is contained in:
parent
1eeb632ff2
commit
524969370d
3 changed files with 250 additions and 99 deletions
113
server/main.js
113
server/main.js
|
@ -20,7 +20,16 @@ const MIME_TYPES = {
|
|||
svg: "image/svg+xml",
|
||||
};
|
||||
|
||||
const motds = [
|
||||
const DATA_TYPES = {
|
||||
text: 0,
|
||||
colour: 1,
|
||||
buffer: 2,
|
||||
backspace: 3,
|
||||
backword: 4,
|
||||
arrow: 5,
|
||||
};
|
||||
|
||||
const MOTDS = [
|
||||
"hello, world!",
|
||||
"all your TTY are belong to us.",
|
||||
"TIP: got a linux system low on storage? try running `sudo rm -rf /`!",
|
||||
|
@ -46,7 +55,7 @@ let sockets = [];
|
|||
|
||||
let buffer = "";
|
||||
const MAX_BUFFER_SIZE = 10240;
|
||||
const MAX_MESSAGE_LENGTH = 64;
|
||||
const MAX_MESSAGE_LENGTH = 1024;
|
||||
|
||||
async function get_file(url) {
|
||||
const paths = [STATIC_PATH, url];
|
||||
|
@ -76,14 +85,31 @@ const server = https.createServer(config, async (req, res) => {
|
|||
|
||||
const wss = new Websocket.Server({ server });
|
||||
wss.on('connection', socket => {
|
||||
socket.send(`${banner}/* ${motds[Math.floor(Math.random() * motds.length)]} */\n\n`);
|
||||
socket.send(buffer);
|
||||
/*
|
||||
socket.colour = generate_colour();
|
||||
socket.send(JSON.stringify({
|
||||
type: DATA_TYPES.colour,
|
||||
colour: socket.colour,
|
||||
}));
|
||||
*/
|
||||
socket.send(JSON.stringify({
|
||||
type: DATA_TYPES.text,
|
||||
text: `${banner}/* ${MOTDS[Math.floor(Math.random() * MOTDS.length)]} */\n\n`,
|
||||
colour: false,
|
||||
sticky: true,
|
||||
}));
|
||||
if (buffer) {
|
||||
socket.send(JSON.stringify({
|
||||
type: DATA_TYPES.buffer,
|
||||
data: buffer,
|
||||
}));
|
||||
}
|
||||
|
||||
sockets.push(socket);
|
||||
|
||||
// console.log(`new connection.\n\tcurrent connections: ${sockets.length}`);
|
||||
|
||||
socket.on('message', handle_message);
|
||||
socket.on('message', event => { handle_message(JSON.parse(event), socket) });
|
||||
|
||||
socket.on('close', () => {
|
||||
sockets = sockets.filter(s => s !== socket);
|
||||
|
@ -91,38 +117,69 @@ wss.on('connection', socket => {
|
|||
});
|
||||
});
|
||||
|
||||
function handle_message(msg) {
|
||||
if (msg.length > MAX_MESSAGE_LENGTH) {
|
||||
return;
|
||||
}
|
||||
if (msg == '\b') {
|
||||
buffer = buffer.slice(0, buffer.length - 1);
|
||||
send_text('\b');
|
||||
return;
|
||||
} else if (buffer.length >= MAX_BUFFER_SIZE) {
|
||||
return;
|
||||
}
|
||||
if (msg == '\n') {
|
||||
buffer += '\n';
|
||||
send_text('\n');
|
||||
return;
|
||||
function handle_message(data, user) {
|
||||
switch (data.type) {
|
||||
case DATA_TYPES.backword:
|
||||
var break_point = buffer.lastIndexOf(" ");
|
||||
const last_newline = buffer.lastIndexOf("\n");
|
||||
if (last_newline > break_point) break_point = last_newline;
|
||||
buffer = buffer.substring(0, break_point);
|
||||
for (var i = 0; i < buffer.length - break_point; i++) {
|
||||
broadcast(JSON.stringify({
|
||||
type: DATA_TYPES.backspace,
|
||||
}));
|
||||
}
|
||||
case DATA_TYPES.backspace:
|
||||
buffer = buffer.substring(0, buffer.length - 1);
|
||||
broadcast(JSON.stringify({
|
||||
type: DATA_TYPES.backspace,
|
||||
}));
|
||||
return;
|
||||
case DATA_TYPES.text:
|
||||
if (buffer.length >= MAX_BUFFER_SIZE) {
|
||||
return;
|
||||
}
|
||||
if (data.text.length > MAX_MESSAGE_LENGTH) {
|
||||
return;
|
||||
}
|
||||
block = {
|
||||
type: DATA_TYPES.text,
|
||||
text: data.text,
|
||||
colour: user.colour,
|
||||
};
|
||||
buffer += data.text;
|
||||
broadcast(JSON.stringify(block));
|
||||
}
|
||||
|
||||
buffer += msg.toString();
|
||||
send_text(msg.toString());
|
||||
|
||||
/*
|
||||
if (buffer.length > MAX_BUFFER_SIZE) {
|
||||
buffer = buffer.slice(buffer.length - MAX_BUFFER_SIZE, buffer.length);
|
||||
send_as_server(`\n\nSERVER: This channel's maximum buffer length has been hit (${MAX_BUFFER_SIZE}).\n` +
|
||||
`You will need to make more room, or the server will have to be restarted.\n` +
|
||||
`Apologies for the inconvenience!`)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
function generate_colour() {
|
||||
let result = '#';
|
||||
let hexref = '0123456789abcdef';
|
||||
for (let i = 0; i < 6; i++) {
|
||||
result += hexref.charAt(Math.floor(Math.random() * hexref.length * .75) + 4);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`OpenTerminal is now LIVE on https://127.0.0.1:${PORT}!`);
|
||||
});
|
||||
|
||||
function send_text(text) {
|
||||
sockets.forEach(s => s.send(text));
|
||||
function send_as_server(message) {
|
||||
broadcast(JSON.stringify({
|
||||
type: DATA_TYPES.text,
|
||||
text: message,
|
||||
colour: "#ffffff",
|
||||
}));
|
||||
}
|
||||
|
||||
function broadcast(data) {
|
||||
sockets.forEach(s => s.send(data));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue