-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-server.js
More file actions
94 lines (79 loc) · 2.46 KB
/
app-server.js
File metadata and controls
94 lines (79 loc) · 2.46 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
var express = require('express');
var _ = require('underscore');
var app = express();
var connections = [];
var title = 'Untitled Presentation';
var audience = [];
var speaker = {};
var questions = require('./app-questions');
var currentQuestion = false;
var results = {
a: 0,
b: 0,
c: 0,
d: 0
};
app.use(express.static('./public'));
app.use(express.static('./node_modules/bootstrap/dist'));
var server = app.listen(3000);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
socket.once('disconnect', function() {
var member = _.findWhere(audience, { id: this.id });
if (member) {
audience.splice(audience.indexOf(member), 1);
io.sockets.emit('audience', audience);
console.log("Left: %s (%s audience members)", member.name, audience.length)
} else if (this.id === speaker.id) {
console.log("%s has left. '%s' is over.", speaker.name, title);
speaker = {};
title = "Untitled Presentation";
io.sockets.emit('end', { title: title, speaker: '' });
}
connections.splice(connections.indexOf(socket), 1);
socket.disconnect();
console.log("Disconnected: %s sockets remaining.", connections.length);
});
socket.on('join', function(payload) {
var newMember = {
id: this.id,
name: payload.name,
type: 'audience'
};
this.emit('joined', newMember);
audience.push(newMember);
io.sockets.emit('audience', audience);
console.log("Audience Joined: %s", payload.name);
});
socket.on('start', function(payload) {
speaker.name = payload.name
speaker.id = this.id;
speaker.type = 'speaker';
title = payload.title;
this.emit('joined', speaker);
io.sockets.emit('start', { title: title, speaker: speaker.name });
console.log("Presentation Started: '%s' by %s", title, speaker.name);
});
socket.on('ask', function(question) {
currentQuestion = question;
results = {a:0, b:0, c:0, d:0};
io.sockets.emit('ask', currentQuestion);
console.log("Question Asked: '%s'", question.q);
});
socket.on('answer', function(payload) {
results[payload.choice]++;
io.sockets.emit('results', results);
console.log("Answer: '%s' - %j", payload.choice, results);
});
socket.emit('welcome', {
title: title,
audience: audience,
speaker: speaker.name,
questions: questions,
currentQuestion: currentQuestion,
results: results
});
connections.push(socket);
console.log("Connected: %s sockets connected.", connections.length);
});
console.log("Polling server is running at 'http://localhost:3000'");