atcheck/atcheck-websocket/server.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-01-13 17:17:41 +01:00
const WebSocket = require('ws');
var http = require('http');
const wss = new WebSocket.Server({ port: 8888 });
var lookup = [];
wss.on('connection', function connection(ws) {
console.log(ws.protocol);
lookup[ws.protocol] = ws;
});
var server = http.createServer ( 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 .");
}
});
/*wss.on('connection', function connection(ws) {
console.log(ws.protocol);
lookup[ws.protocol] = ws;
});*/
server.listen(8889);
console.log("Server running on port 8889");