-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (65 loc) · 1.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const fs = require("fs/promises");
const WebSocketServer = require("ws").WebSocketServer;
const wss = new WebSocketServer({port:8082});
console.log("Server's up and running");
wss.on('connection',function(ws,req) {
if(!req.headers['sec-websocket-protocol']) {
ws.send("PROTOCOL ERR");
ws.close();
return;
}
if(req.headers['sec-websocket-protocol'].toLowerCase().split(" ").indexOf("lba2lvls") == -1) {
ws.send("PROTOCOL ERR");
ws.close();
return;
}
console.log("Connection: " + req.socket.remoteAddress);
ws.on('message',async function(d) {
var data = d.toString().trim();
//console.log(data);
switch(data.split(" ")[0]) {
case "GET":
try {
ws.send("GET OK\n"+await fs.readFile(".\\lvls\\"+data.split(" ")[1]+".lvl"));
disconnect(ws,req);
} catch(e) {
console.log(e);
ws.send("GET ERR");
disconnect(ws,req);
}
break;
case "POST":
try {
let id = concatrandom(8);
fs.writeFile(".\\lvls\\"+id+".lvl",data.split(" ")[1]);
ws.send("POST OK\n"+id);
disconnect(ws,req);
} catch(e) {
ws.send("POST ERR");
disconnect(ws,req);
}
break;
case "LIST":
try {
var list = "";
for(var file of await fs.readdir(".\\lvls\\")) {
list += file.substring(0,file.length-4)+";";
}
ws.send("LIST OK\n"+list.substring(0,list.length-1));
disconnect(ws,req);
} catch(e) {
ws.send("LIST ERR");
disconnect(ws,req);
}
break;
}
});
})
function concatrandom(count) {
let b = "";
for(let i = 0; i < count; i++) {
b += Math.floor(Math.random() * 10).toString();
}
return b;
}
function disconnect(ws,req) { console.log("Disconnection: " + req.socket.remoteAddress); ws.close(); }