Permalink
Browse files

Fixes history not being cleared server-side

  • Loading branch information...
1 parent e350d60 commit 95378acceb0b81c9826700b920b1a045797af914 @sc0tt committed Mar 29, 2012
Showing with 32 additions and 31 deletions.
  1. +32 −31 server.js
View
@@ -1,27 +1,28 @@
var io = require('socket.io').listen(6969);
var Canvas = require('canvas'),
- canvas = new Canvas(1920,1080),
- context = canvas.getContext('2d'),
- fs = require('fs');
+canvas = new Canvas(1920,1080),
+context = canvas.getContext('2d'),
+fs = require('fs');
// Turn off socket.io debug messages
io.set('log level', 2);
var adminPW = "hello";
var history = [];
io.sockets.on('connection', function(socket) {
- for(var i = 0; i < history.length; i++) {
+ for(var i = 0; i < history.length; i++) {
socket.emit('add', history[i]);
- }
- socket.on('clear', function(pass) {
- if(pass == adminPW) {
+ }
+ socket.on('clear', function(pass) {
+ if(pass == adminPW) {
io.sockets.emit('clearUser');
- }
- });
- socket.on('add', function(data) {
- history.push(data);
- drawToCanvas(data);
- socket.broadcast.emit('add', data);
- });
- });
+ history = [];
+ }
+ });
+ socket.on('add', function(data) {
+ history.push(data);
+ drawToCanvas(data);
+ socket.broadcast.emit('add', data);
+ });
+});
var minutes = 5 * 60 * 1000;
function drawToCanvas(data)
@@ -44,22 +45,22 @@ function drawToCanvas(data)
}
setInterval(function() {
- if(history.length > 0) {
- fs.unlinkSync('./img/last.png');
- var out = fs.createWriteStream('./img/last.png'),
- stream = canvas.toDataURL().replace(/^data:image\/png;base64,/,""),
- buffer = new Buffer(stream, 'base64');
- fs.writeFile('./img/last.png', buffer, function(error) {
- if(error != null) console.log(error);
- });
- }
- context.save();
- context.setTransform(1, 0, 0, 1, 0, 0);
- context.clearRect(0, 0, canvas.width, canvas.height);
- context.restore();
- history = [];
- io.sockets.emit('clearUser');
- }, minutes);
+ if(history.length > 0) {
+ fs.unlinkSync('./img/last.png');
+ var out = fs.createWriteStream('./img/last.png'),
+ stream = canvas.toDataURL().replace(/^data:image\/png;base64,/,""),
+ buffer = new Buffer(stream, 'base64');
+ fs.writeFile('./img/last.png', buffer, function(error) {
+ if(error != null) console.log(error);
+ });
+ }
+ context.save();
+ context.setTransform(1, 0, 0, 1, 0, 0);
+ context.clearRect(0, 0, canvas.width, canvas.height);
+ context.restore();
+ history = [];
+ io.sockets.emit('clearUser');
+}, minutes);
//Temporary for now...
/*

0 comments on commit 95378ac

Please sign in to comment.