CliSite/index.ts

105 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-06-11 20:49:24 +02:00
//start ws server with socket.io
var http = require('http');
var express = require('express');
import { Request, Response } from 'express';
var app = express();
var server = http.createServer(app);
var port = process.env.PORT || 8480;
const { Server } = require("socket.io");
import { Socket } from 'socket.io';
const io = new Server(server);
//get resolvecommand from commands.ts
import { resolveCommand } from './commands/commands';
2022-06-11 20:49:24 +02:00
app.get('/', (req:Request, res:Response) => {
res.sendFile(__dirname + '/index.html');
});
//initialize buffer
//var buffer = [];
//add type for typescript
var buffer: { [id: string]: string } = {};
//var filesystem = [];
var filesystem: any = {};
//var curdir = [];
var curdir: any = {};
//var sessions = [];
var sessions: any = {};
//var curdirx = [];
var curdirx: any = {};
//on connection
function sendbuffer(socket:Socket, bufferx: { [id: string]: string }) {
var bufx = bufferx[socket.id];
//add placeholder character(reserved unicode) for blinking cursor in client;
bufx += "\u2588";
socket.emit('buffer', bufx);
}
io.on('connection', (socket:Socket) => {
console.log('a user connected');
buffer[socket.id] = "";
filesystem[socket.id] = {};
curdir[socket.id] = filesystem[socket.id];
curdirx[socket.id] = [];
//welcome message
buffer[socket.id] = "Welcome to the console!\n";
buffer[socket.id] += "Type 'help' to see the available commands.\n";
buffer[socket.id] += "#";
//send buffer
sendbuffer(socket, buffer);
socket.on('keypress', (data) => {
console.log('a key was pressed');
//get key
//convert data to string
var key = String.fromCharCode(data);
//convert key to lowercase
key = key.toLowerCase();
console.log(data);
//check if key is printable character or digit or . or space etc
//check if key is enter
if (data == 8){
//backspace
//if line is not empty remove last character if it is not the first character
var lines = buffer[socket.id].split("\n");
if (lines[lines.length - 1] != "#") {
lines[lines.length - 1] = lines[lines.length - 1].slice(0, -1);
buffer[socket.id] = lines.join("\n");
}
}
else if(data == 13) {
//execute command
let lines = buffer[socket.id].split('\n');
let command = lines[lines.length-1];
//remove # from command
command = command.substring(1);
//remove \n and everything after
command = command.substring(command.indexOf('\n'), command.length);
let args = command.split(' ');
let commandName = args[0];
let commandArgs = args.slice(1);
//remove trailing spaces
commandArgs = commandArgs.map(function(arg) {
return arg.trim();
});
//add \n to buffer
buffer[socket.id] += "\n";
//remove # in beginning of commandname
console.log("Executing command: \"" + commandName + "\" with arguments: \"" + commandArgs + "\"");
resolveCommand(commandName, socket, commandArgs, buffer, sessions, filesystem, curdir, curdirx);
2022-06-11 20:49:24 +02:00
}
else{
//add key to buffer
buffer[socket.id] += key;
}
//send buffer to client
sendbuffer(socket, buffer);
});
});
io.on('disconnect', (socket:Socket) => {
console.log('user disconnected');
buffer[socket.id] = "";
});
server.listen(port, function() {
console.log('Server listening at port %d', port);
});