-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (28 loc) · 849 Bytes
/
index.js
File metadata and controls
35 lines (28 loc) · 849 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
26
27
28
29
30
31
32
33
34
35
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.sendfile('public/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
var line_history = [];
io.on('connection', function(socket){
// first send the history to the new client
for (var i in line_history) {
socket.emit('draw_line', { line: line_history[i] } );
}
// add handler for message type "draw_line".
socket.on('draw_line', function (data) {
// add received line to history
line_history.push(data.line);
// send line to all clients
io.emit('draw_line', { line: data.line });
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});