Skip to content

Instantly share code, notes, and snippets.

@gijs
Forked from jeffkreeftmeijer/mice.css
Created August 4, 2010 07:15
Show Gist options
  • Save gijs/507781 to your computer and use it in GitHub Desktop.
Save gijs/507781 to your computer and use it in GitHub Desktop.
.mouse{
position: absolute;
background-image: url('../images/cursor.png');
width: 15px;
height: 22px;
z-index: 100;
}
io.setPath('/js/socket/');
function ratelimit(fn, ms) {
var last = (new Date()).getTime();
return (function() {
var now = (new Date()).getTime();
if (now - last > ms) {
last = now;
fn.apply(null, arguments);
}
});
}
function move(mouse){
if(disabled == false){
if($('#mouse_'+mouse['id']).length == 0) {
$('body').append('<div class="mouse" id="mouse_'+mouse['id']+'"/>');
}
$('#mouse_'+mouse['id']).css({
'left' : (($(window).width() - mouse['w']) / 2 + mouse['x']) + 'px',
'top' : mouse['y'] + 'px'
})
}
}
$(document).ready(function(){
$('#mouse_toggle a').toggle(function(){
$('.mouse').hide();
disabled = true;
$(this).html('enable');
}, function(){
$('.mouse').show();
disabled = false;
$(this).html('disable');
})
})
$(document).mousemove(
ratelimit(function(e){
socket.send(JSON.stringify({
'action': 'move',
'x': e.pageX,
'y': e.pageY,
'w': $(window).width(),
'h': $(window).height()
}));
}, 40)
);
var disabled = false,
socket = new io.Socket('jeffkreeftmeijer.com', {port: 8000});
if(socket.connect()){
socket.on('message', function(data){
data = JSON.parse(data);
if(data['action'] == 'close'){
$('#mouse_'+data['id']).remove();
} else if(data['action'] == 'move'){
move(data);
};
});
};
var sys = require('sys'),
http = require('http'),
io = require('../'),
server = http.createServer(),
socket = io.listen(server),
json = JSON.stringify,
log = sys.puts;
server.listen(8000);
socket.on('connection', function(client){
client.on('message', function(message){
try {
request = JSON.parse(message);
} catch (SyntaxError) {
log('Invalid JSON:');
log(message);
return false;
}
if(request.action != 'close' && request.action != 'move') {
log('Ivalid request:' + "\n" + message);
return false;
}
request.id = client.sessionId
client.broadcast(json(request));
});
client.on('disconnect', function(){
client.broadcast(json({'id': client.sessionId, 'action': 'close'}));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment