-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (35 loc) · 1.02 KB
/
index.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var numConnected = 0;
var BUFFER_MAX = 500;
var drawingBuffer = [];
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 3000;
function addToBuffer(line) {
drawingBuffer.push(line);
if (drawingBuffer.length > BUFFER_MAX) {
drawingBuffer.shift();
}
}
app.use(express.static('public'));
io.on('connection', function(socket) {
numConnected++;
console.log('connected: ' + numConnected);
io.emit('user count', numConnected);
socket.emit('buffered lines', drawingBuffer);
socket.on('disconnect', function() {
numConnected--;
console.log('connected: ' + numConnected);
io.emit('user count', numConnected);
});
socket.on('draw line', function(line) {
addToBuffer(line);
socket.broadcast.emit('draw line', line);
});
});
http.listen(port, function() {
console.log('listening on *:' + port);
});