-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
38 lines (34 loc) · 1.22 KB
/
socket.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
var debug = require('debug')('Clack:socketio');
var State = require("./state");
module.exports = function(server) {
// Socket.io
var io = require('socket.io')(server);
var state = new State(io);
function myHandler(socket) {
debug("user connected");
socket.uniqueid = state.uniqueid();
// XXX: Note, this means that anyone could open or close votes. We need
// to only do this for presenter pages, but I don't feel like it yet.
if (state.pollingOpen) {
socket.emit('pollingOpen', {choicesCount: state.choicesCount});
socket.emit('voteUpdate', {votes: state.votes});
} else {
socket.emit('pollingClose');
}
socket.on('openVotes', function(msg) {
state.openPolls(4);
});
socket.on('closeVotes', function(msg) {
state.closePolls();
});
socket.on('vote', function(data) {
if (state.pollingOpen == false) {
socket.emit("softerror", "Polling is not open");
return;
}
state.recordVote(data.vote, socket.uniqueid);
debug(JSON.stringify(data));
});
}
io.on('connection', myHandler);
}