-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (63 loc) · 2.41 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
68
"use strict";
var http = require("http");
var webSocketServer = require('websocket').server;
var http = require('http');
var fs = require("fs");
var jsonfile = require("jsonfile");
var store_file = "./db/store.json";
var repl = require("repl");
try{
global.Student = require("./modules/students.js");
global.store = require("./modules/database.js");
global.Tournament = require("./modules/tournaments.js");
if(global.store.tournament == undefined){
global.store.tournament = new Tournament({title: "Sportfest", certificateTEX: "certificate.tex"});
}else{
global.store.tournament = new Tournament(global.store.tournament);
}
}catch(e){
console.log(e);
if(store == undefined){
console.log("This is a fatal error! Store is undefined!")
process.exit();
}
}
var control = require("./modules/control.js");
function startServer(){
var rs = repl.start({prompt: "Control>"});
rs.context.control = control;
//STARTS WEBSOCKET-SERVER
var server = http.createServer(function (req, res) {
});
var webSocketsServerPort = 1998;
server.listen(webSocketsServerPort, function() {
console.log("Server is listening on port " + webSocketsServerPort);
});
var wss = new webSocketServer({
httpServer: server
});
global.connections = [];
wss.on('request', function(request) {
//Accept request
var ws = request.accept(null, request.origin);
ws.conId = global.connections.push(ws);
ws.on('close', function() {
global.connections[ws.conId] = undefined; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! BAD SOLUTION (array will somewhen be too large)
clearInterval(ws.interval);
});
//Message received
ws.on('message', function(message) { //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOT FINISHED YET
try{
var msg = JSON.parse(message.utf8Data);
console.log(msg);
}catch(e){
console.log("A message through the websocket almost killed the server! Message: ");
console.log(message);
console.log("Error: ");
console.log(e);
ws.sendUTF(JSON.stringify({type: "notification", text: "The last message through the websocket almost killed the server. Please take a look at the console.", importance: 2}));
}
});
});
}
startServer();