-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (51 loc) · 1.79 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
69
70
71
72
'use strict'
//Einbinden der benötigten Bibliotheken
const https = require('https');
const express = require('express');
const socketio = require('socket.io');
const fs = require('fs');
const path = require('path');
//eigene Klassen einbinden
const DataController = require('./datacontroller');
//Array in dem alle Spieler abgelegt sind
let lobby = [];
//Server generieren
let app = express();
//Client-App an anfragenden Client ausliefern
app.use(express.static(__dirname + '/client'));
const httpsOptions = {
cert: fs.readFileSync(path.join(__dirname, 'ssl', 'RSA-cert.pem')),
key: fs.readFileSync(path.join(__dirname, 'ssl', 'RSA-privkey.pem'))
};
let server = https.createServer(httpsOptions, app).listen(8080, function(){
showReady();
});
let io = socketio(server);
//Auf Verbindungen von Clients reagieren
io.on('connection', connected);
//Datacontroller
let datacontroller;
function showReady() {
console.log('Server connected!');
//Datacontroller initialisieren
datacontroller = new DataController();
}
//Highscore aus datenback lesen und an aufrufer senden
let singleHighscore = (function(sock, data){
sock.emit('highscore', data);
});
//Highscore aus datenback lesen und an alle senden mit richtigem gridsize
let newHighscoreList = (function(data){
io.emit('highscore', data);
});
function connected(sock) {
//Event-Handler für erstmaliges senden der HighscoreListe
sock.on('highscoreListe', (data) => datacontroller.getHighscore(sock, data, singleHighscore));
//Event-Handler für das hinzufügen eines Highscore eintrags
sock.on('addhighscore', (data) => datacontroller.addHighscoreObject(data, newHighscoreList));
}
//Fehlerbehandlung
server.on('error', serverError);
function serverError(err) {
console.error('Server error: ', err);
}