-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (22 loc) · 690 Bytes
/
index.js
File metadata and controls
26 lines (22 loc) · 690 Bytes
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var light = {state:false};
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
console.log('User connected: ' + socket.id);
socket.emit('light', light);
socket.on('disconnect', function(){
console.log('User disconnected: ' + socket.id);
});
socket.on('toggle', function(state) {
light.state = !light.state;
console.log('id: ' + socket.id + ' light: ' + light.state);
io.sockets.emit('light', light);
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});