-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathserver.js
121 lines (106 loc) · 3.63 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { Worker, isMainThread, threadId } = require('worker_threads');
const express = require('express');
const Ably = require('ably');
const envConfig = require('dotenv').config();
const serveStatic = require('serve-static');
const path = require('path');
const app = express();
const { ABLY_API_KEY } = envConfig.parsed;
const globalQuizChName = 'main-quiz-thread';
console.log(envConfig, ABLY_API_KEY);
let globalQuizChannel;
const activeQuizRooms = {};
let totalPlayersThroughout = 0;
const realtime = new Ably.Realtime({
key: ABLY_API_KEY,
echoMessages: false
});
app.use('/', serveStatic(path.join(__dirname, 'realtime-quiz/dist')));
app.get('/auth', (request, response) => {
const tokenParams = { clientId: uniqueId() };
realtime.auth.createTokenRequest(tokenParams, function (err, tokenRequest) {
if (err) {
response
.status(500)
.send('Error requesting token: ' + JSON.stringify(err));
} else {
response.setHeader('Content-Type', 'application/json');
response.setHeader('Access-Control-Allow-Origin', '*');
response.send(JSON.stringify(tokenRequest));
}
});
});
const uniqueId = function () {
return 'id-' + Math.random().toString(36).substr(2, 16);
};
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'realtime-quiz/dist/index.html'));
});
app.get('/play', function (req, res) {
let requestedRoomCode = req.query.quizCode;
if (activeQuizRooms[requestedRoomCode].didQuizStart === true) {
res.sendFile(path.join(__dirname, 'realtime-quiz/dist/index.html'));
} else {
res.sendFile(path.join(__dirname, 'realtime-quiz/dist/index.html'));
}
});
app.get('/checkRoomStatus', function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
let requestedRoomCode = req.query.quizCode;
res.send({
isRoomClosed: activeQuizRooms[requestedRoomCode]
? activeQuizRooms[requestedRoomCode].didQuizStart
: true
});
});
const listener = app.listen(process.env.PORT || 8082, () => {
console.log('Your app is listening on port ' + listener.address().port);
});
realtime.connection.once('connected', () => {
globalQuizChannel = realtime.channels.get(globalQuizChName);
globalQuizChannel.presence.subscribe('enter', (player) => {
console.log('new quiz host', player.clientId);
generateNewQuizRoom(
player.data.nickname,
player.data.roomCode,
player.clientId
);
});
});
function generateNewQuizRoom(hostNickname, hostRoomCode, hostClientId) {
if (isMainThread) {
const worker = new Worker('./quiz-room-server.js', {
workerData: {
hostNickname: hostNickname,
hostRoomCode: hostRoomCode,
hostClientId: hostClientId
}
});
console.log(`CREATING NEW THREAD WITH ID ${threadId}`);
worker.on('error', (error) => {
console.log(`WORKER EXITED DUE TO AN ERROR ${error}`);
});
worker.on('message', (msg) => {
if (msg.roomCode && !msg.killWorker) {
activeQuizRooms[msg.roomCode] = {
roomCode: msg.roomCode,
totalPlayers: msg.totalPlayers,
didQuizStart: msg.didQuizStart
};
totalPlayersThroughout += totalPlayersThroughout;
} else if (msg.roomCode && msg.killWorker) {
totalPlayersThroughout -= msg.totalPlayers;
delete activeQuizRooms[msg.roomCode];
} else {
activeQuizRooms[msg.roomCode].didQuizStart = msg.didQuizStart;
console.log('Main knows started');
}
});
worker.on('exit', (code) => {
console.log(`WORKER EXITED WITH THREAD ID ${threadId}`);
if (code !== 0) {
console.log(`WORKER EXITED DUE TO AN ERROR WITH CODE ${code}`);
}
});
}
}