forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (33 loc) · 1000 Bytes
/
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
const static = require('node-static');
const http = require('http');
const bcrypt = require('bcrypt');
var file = new(static.Server)('./public');
var server = http.createServer(function (req, res) {
file.serve(req, res);
})
var io = require('socket.io')(server);
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
server.listen(port);
var passwordHash = '$2b$10$Z.k8FXVhVdEkyLq.hKOYeepN9YUwBmJ7qI7ofZMibMeYNwwC8bCle';
var currentState = null;
io.on('connection', function(socket) {
var master = false;
socket.on('authenticate', function(data){
var ok = bcrypt.compare(data.plaintext, passwordHash, function(err, result) {
master = result;
});
});
if (currentState) {
socket.broadcast.emit('change-state', {
state: currentState
});
}
socket.on('state-changed', function(data) {
if (master) {
currentState = data.state;
socket.broadcast.emit('change-state', {
state: data.state
});
}
});
});