-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (58 loc) · 1.67 KB
/
index.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
//////////////////////////////////////////////////////////////////////////
// Copyright (c) 2014, Max-Emanuel Maurer <[email protected]> //
//////////////////////////////////////////////////////////////////////////
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/static'));
app.get('/', function(req, res){
res.sendfile('templates/index.html');
});
app.get('/report', function(req, res){
res.sendfile('templates/report.html');
});
app.get('/admin', function(req, res){
res.sendfile('templates/admin.html');
});
var voteData = {question:"Question",options:["Option 1","Option 2"],votes:{},showOptions:false};
io.on('connection', function(socket){
console.log('a user connected');
function update() {
io.emit('update', voteData);
}
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('update', function(){
update();
});
socket.on('reset', function(){
voteData.votes = {};
update();
});
socket.on('setQuestion', function(data){
voteData.question = data.question;
voteData.options = data.options;
voteData.showOptions=false;
voteData.votes = {};
update();
});
socket.on('showOptions', function(){
voteData.showOptions = !voteData.showOptions;
update();
});
socket.on('vote', function(option){
console.log("voted "+option);
if (!voteData.votes[option]) {
voteData.votes[option]=1;
} else {
voteData.votes[option]++;
}
update();
});
});
var port = process.env.PORT || 3000;
http.listen(port, function(){
console.log('listening on *:3000');
});