-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp-server.js
More file actions
115 lines (96 loc) · 3.12 KB
/
app-server.js
File metadata and controls
115 lines (96 loc) · 3.12 KB
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
var express = require('express');
var path = require('path');
var webpack = require('webpack');
var webpackConfig = require('./webpack.config.js');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var _collection = require('lodash/collection'); // find
var _util = require('lodash/util'); // matches
var app = express();
// Hot reloading
var compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
noInfo: true, publicPath: webpackConfig.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
// End hot reloading
app.use(express.static('./public'));
app.use(express.static('./node_modules/bootstrap/dist'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
var server = app.listen(3000);
var connections = [];
var audience = [];
var title = 'Untitled Presentation';
var speaker = {};
var questions = require('./app-questions');
var currentQuestion = false;
var results = {
a: 0,
b: 0,
c: 0,
d: 0
};
var io = require('socket.io')(server);
io.on('connection', (client) => {
client.emit('welcome', {
title: title,
speaker: speaker,
audience: audience,
questions: questions,
currentQuestion: currentQuestion,
results: results
});
connections.push(client);
console.log('Connected: %s socket. Total: %s', client.id, connections.length);
client.on('join', (payload) => {
const newMember = {
id: client.id,
name: payload.name,
type: 'member'
};
client.emit('joined', newMember);
audience.push(newMember);
io.emit('audience', audience);
console.log('Audience joined: %s', payload.name);
});
client.on('start', (payload) => {
speaker.name = payload.name;
speaker.id = client.id;
speaker.type = 'speaker';
title = payload.title;
client.emit('joined', speaker);
io.emit('start', {title: title, speaker: speaker});
console.log('Presentation Started: "%s" by %s', title, speaker.name);
});
client.on('ask', (question) => {
currentQuestion = question;
results = { a: 0, b: 0, c: 0, d: 0 };
io.emit('ask', currentQuestion);
console.log('Question asked "%s"', question.q);
});
client.on('answer', (payload) => {
results[payload.choice]++;
io.emit('results', results);
console.log('Answer: "%s" - %j', payload.choice, results);
});
client.on('disconnect', () => {
var memberDisconnected = _collection.find(audience, _util.matches({ id: client.id }));
if (memberDisconnected) {
audience.splice(audience.indexOf(memberDisconnected), 1);
io.emit('audience', audience);
console.log('Left: %s (%s audience members)', memberDisconnected.name, audience.length);
} else if (client.id === speaker.id) {
console.log('%s has left. %s is over.', speaker.name, title);
speaker = {};
title = 'Untitled Presentation';
currentQuestion = false;
io.emit('end', { title: title, speaker: speaker, currentQuestion: currentQuestion });
}
connections.splice(connections.indexOf(client), 1);
client.disconnect();
console.log('Disconnected: %s sockets remaining.', connections.length);
});
});
console.log('Server running at http://localhost:3000');