atcheck/atcheck-websocket/server.js
2020-03-08 22:26:42 +01:00

51 lines
1.5 KiB
JavaScript

const WebSocket = require('ws');
var https = require('https');
var websocket_port = 8888;
const wss = new WebSocket.Server({port: websocket_port});
console.log("Websocket server running on port " + websocket_port);
var lookup = [];
wss.on('connection', function connection(ws) {
console.log(ws);
lookup[ws.protocol] = ws;
});
var server = https.createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/atcheck.projektstudencki.pl/cert.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/atcheck.projektstudencki.pl/privkey.pem')
}, function (request, response) {
response.writeHead(200, {"Content-Type": "text\plain"});
request.setEncoding('utf8');
if (request.method === "POST") {
var body = '';
request.on('data', function (chunk) {
body += chunk;
});
request.on('end', function () {
body = JSON.parse(body);
console.log(body);
console.log(lookup);
var send_array = {
"type": "data",
"id": body.student_index,
"name": body.student_name,
"surname": body.student_surname,
"classes_code": body.classes_code
};
lookup[body.classes_code].send(JSON.stringify(send_array));
});
}
else {
response.end("Undefined request .");
}
});
var https_port = 8889;
server.listen(https_port);
console.log("HTTP server running on port " + https_port);