atcheck/atcheck-websocket/server.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-01-13 17:17:41 +01:00
const WebSocket = require('ws');
2020-03-09 19:38:01 +01:00
var http = require('http');
2020-03-08 22:30:14 +01:00
const fs = require('fs');
2020-03-08 22:26:42 +01:00
var websocket_port = 8888;
const wss = new WebSocket.Server({port: websocket_port});
console.log("Websocket server running on port " + websocket_port);
2020-01-13 17:17:41 +01:00
var lookup = [];
wss.on('connection', function connection(ws) {
lookup[ws.protocol] = ws;
2020-01-13 17:17:41 +01:00
});
2020-03-09 19:56:20 +01:00
2020-03-09 19:38:01 +01:00
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text\plain"});
request.setEncoding('utf8');
2020-01-13 17:17:41 +01:00
if (request.method === "POST") {
var body = '';
2020-03-08 17:10:03 +01:00
request.on('data', function (chunk) {
body += chunk;
});
2020-03-09 19:43:37 +01:00
try {
request.on('end', function () {
2020-03-09 19:58:58 +01:00
body = JSON.parse(body);
2020-03-09 19:43:37 +01:00
var send_array = {
"type": "data",
2020-03-09 19:58:58 +01:00
"id": body.student_index,
"name": body.student_name,
"surname": body.student_surname,
"classes_code": body.classes_code
2020-03-09 19:43:37 +01:00
};
2020-03-09 19:58:58 +01:00
lookup[body.classes_code].send(JSON.stringify(send_array));
2020-03-09 20:01:53 +01:00
response.end("Added");
2020-03-09 19:43:37 +01:00
});
} catch (e) {console.log(e)}
}
else {
response.end("Undefined request .");
}
2020-01-13 17:17:41 +01:00
});
2020-03-09 19:38:01 +01:00
var http_port = 8889;
server.listen(http_port);
console.log("HTTP server running on port " + http_port);