-
Notifications
You must be signed in to change notification settings - Fork 9
/
app-server.js
94 lines (77 loc) · 2.54 KB
/
app-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
var express = require('express');
var app = express();
var _ = require('underscore')
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: ", member.name, audience.length);
} else if(this.id === speaker.id){
console.log("%s has left. '%s' presentation 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: 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 as ", 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: ", 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]++;
console.log("answer: %s = %j", payload.choice, results);
});
socket.emit('welcome', {
title: title,
audience: audience,
speaker : speaker.name,
questions: questions,
currentQuestion: currentQuestion
});
connections.push(socket);
console.log("connection: number of sockets connected", connections.length);
});
console.log("server running on local host 3000");