-
-
Save rwaldron/491397 to your computer and use it in GitHub Desktop.
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
.mouse{ | |
position: absolute; | |
background-image: url('../images/cursor.png'); | |
width: 15px; | |
height: 22px; | |
z-index: 100; | |
} |
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
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).mousemove( | |
ratelimit(function(e){ | |
if (conn) { | |
conn.send(JSON.stringify({ | |
'action': 'move', | |
'x': e.pageX, | |
'y': e.pageY, | |
'w': $(window).width(), | |
'h': $(window).height() | |
})); | |
} | |
}, 40) | |
); | |
var disabled = false, | |
conn; | |
var connect = function() { | |
if (window["WebSocket"]) { | |
$('#mouse_toggle').show(); | |
$('#no_web_sockets').hide(); | |
conn = new WebSocket("ws://jeffkreeftmeijer.com:8000"); | |
conn.onmessage = function(evt) { | |
data = JSON.parse(evt.data); | |
if(data['action'] == 'close'){ | |
$('#mouse_'+data['id']).remove(); | |
} else if(data['action'] == 'move'){ | |
move(data); | |
}; | |
}; | |
} | |
}; | |
window.onload = connect; |
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
var ws = require(__dirname + '/lib/ws'), | |
server = ws.createServer(); | |
server.addListener("connection", function(conn){ | |
conn.addListener("message", function(message){ | |
message = JSON.parse(message); | |
message['id'] = conn.id | |
conn.broadcast(JSON.stringify(message)); | |
}); | |
}); | |
server.addListener("close", function(conn){ | |
conn.broadcast(JSON.stringify({'id': conn.id, 'action': 'close'})); | |
}); | |
server.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment