-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (77 loc) · 2.25 KB
/
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
95
96
97
98
var express = require('express'),
path = require('path'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
events = require('./events.js'),
memcached = require('./drivers/memcached.js'),
shortid = require('shortid'),
port = process.env.PORT || 8099;
app.use(express.static(path.join(__dirname, '/public')));
app.use('/scripts', express.static(__dirname + '/node_modules'));
//Frontend routes
app.get('/adm', function(req, res) {
res.sendFile(__dirname + '/templates/admin.html');
});
app.get('/stat', function(req, res) {
res.sendFile(__dirname + '/templates/stat.html');
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/templates/index.html');
});
//Backend routes
app.post('/register', function (req, res) {
res.send(JSON.stringify({ user_hash : shortid.generate() }));
});
app.get('/votes', function (req, res) {
memcached.get('votes', function (err, data) {
if (data) {
res.send(JSON.stringify(data));
}
});
});
//Server
http.listen(port, function() {
console.log('server started on localhost:'+port);
var votes = { red : 0, blue : 0 };
memcached.set('votes', votes, 0, function (err) {
if (err) throw new err;
});
memcached.set('vote_status', true, 0, function (err) {
if (err) throw new err;
});
});
io.on('connection', function(socket) {
events.votes(function(data) {
io.emit('votes', data);
});
events.isActive(function(data) {
io.emit('isActive', data);
});
socket.on('vote', function(data) {
events.vote(data, function(data) {
io.emit('votes', data);
});
});
socket.on('reset', function(msg) {
events.reset();
io.emit('isActive', true);
events.votes(function(data) {
io.emit('votes', data);
});
});
socket.on('stop', function() {
events.stop();
io.emit('isActive', false);
events.votes(function(data) {
io.emit('votes', data);
});
});
socket.on('win', function() {
events.win(function(data) {
events.stop();
io.emit('isActive', false);
io.emit('win', data);
});
});
});