-
-
Save alejandro/2992331 to your computer and use it in GitHub Desktop.
mookmike sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Dependencies | |
var http = require('http') | |
, express = require('express') | |
, stylus = require('stylus') | |
, nib = require('nib') | |
, app = express() | |
; | |
//Use Nib on CSS | |
function compile(str, path) { | |
return stylus(str) | |
.set('filename', path) | |
.use(nib()) | |
} | |
//Views and Middleware | |
app.set('views', __dirname + '/views') | |
app.set('view engine', 'jade') | |
app.use(express.logger('dev')) | |
app.use(stylus.middleware( | |
{ src: __dirname + '/public' | |
, compile: compile | |
} | |
)) | |
app.use(express.static(__dirname + '/public')) | |
//Variable for if there is a current drawer, default to false. | |
drawer = false | |
// If there is no current drawer the app goes to the drawing page I | |
// If there is a drawer then user is sent to the view page | |
app.get('/', function (req, res) { | |
if(!drawer){ | |
drawer = true | |
res.render('drawer', | |
{ title : 'Drawer', | |
script : '/scripts/draw.js' | |
} | |
) | |
} | |
else{ | |
res.render('viewer', | |
{ title : 'viewer', | |
script : '/scripts/view.js' | |
} | |
) | |
} | |
}) | |
//Socket traffic | |
var server = http.createServer(app).listen(process.env['app_port'], function(){ | |
var io = require('socket.io').listen(server) | |
io.sockets.on('connection', function(socket){ | |
//Establish New Connection | |
socket.emit('user_connect') | |
/* | |
* When a user connects the socket gets labeled for whether it is a drawer or a viewer | |
* and alerts the console a user has connected as such | |
*/ | |
socket.on('new_drawer', function(){ | |
drawer = true | |
socket.set('task', 'drawer') | |
console.log('New Drawer!') | |
}) | |
socket.on('set_viewer', function(){ | |
socket.set('task', 'viewer') | |
console.log('New Viewer') | |
}) | |
//These lines take in the data emitted from the draw page and broadcast them out to all viewers | |
socket.on('down', function(data){ | |
socket.broadcast.emit('mouseDown', data) | |
}) | |
socket.on('move', function(data){ | |
socket.broadcast.emit('mouseMove', data); | |
}) | |
//When a user disconnects | |
socket.on('disconnect', function(){ | |
socket.get('task', function(err, task){ | |
if (err){ | |
console.log('Error deciding users task'); | |
} | |
else if (task == 'drawer'){ | |
drawer = false; //When drawer disconnects users.drawer is reset to false to allow a new drawer. Even though the value changes, the routing doesn't reset. | |
console.log('Drawer Disconnected'); | |
} | |
else if (task == 'viewer'){ | |
console.log('Viewer Disconnected'); | |
} | |
}) | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment