Created
January 12, 2013 14:54
-
-
Save yosiat/4518384 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
// When the document is ready to be manipulated.. | |
$(document).ready(function(){ | |
// connect to sokcet io | |
var socket = io.connect('http://localhost:1337'); | |
var $box = $("#box"); | |
$box.draggable({ | |
drag: function(){ | |
socket.emit("box_moved", { | |
offsetTop : this.offsetTop, | |
offsetLeft : this.offsetLeft | |
}); | |
} | |
}); | |
socket.on("box_moved", function(coordinates){ | |
console.log(coordinates); | |
$box.css({ | |
left: coordinates.offsetLeft, | |
top: coordinates.offsetTop || 0, | |
}); | |
}); | |
}); |
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
<html> | |
<head> | |
<title></title> | |
<style type="text/css"> | |
div#box { | |
width: 250px; | |
height: 250px; | |
background-color: green; | |
position: absolute; | |
left: 0; | |
top: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="box"></div> | |
<script src="js/jquery-1.8.3.min.js"></script> | |
<script src="js/jquery-ui-1.9.2.custom.min.js"></script> | |
<script src="http://localhost:1337/socket.io/socket.io.js"></script> | |
<script src="js/app.js"></script> | |
</body> | |
</html> |
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 io = require('socket.io').listen(1337); | |
io.sockets.on('connection', function (socket) { | |
socket.on('box_moved', function(coordinates){ | |
socket.broadcast.emit('box_moved', coordinates); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment