Created
January 18, 2013 16:13
-
-
Save dirkk0/4565681 to your computer and use it in GitHub Desktop.
drag a box on multiple browsers in realtime with websockets. Most of the code is from this thread:
http://stackoverflow.com/questions/9865167/socket-io-and-complex-json-with-node-js-jquery (I changed the mousemove to drag, though)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-1.8.3.js"></script> | |
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
$(document).ready(function () { | |
var socket = io.connect(); | |
socket.on('update_position', function (data) { | |
var x = data.x; | |
var y = data.y; | |
// jquery code to move div here | |
$("#left").val(x); | |
$("#top").val(y); | |
$("#mydiv").css({ | |
left: x + "px", | |
top: y + "px" | |
}); | |
}); | |
$("#mydiv").draggable({ | |
drag: function (event, ui) { | |
var coord = $(this).position(); | |
$("#left").val(coord.left); | |
$("#top").val(coord.top); | |
socket.emit('receive_position', { | |
x: coord.left, | |
y: coord.top | |
}); | |
} | |
}); | |
}); | |
</script> | |
<style> | |
.dstyle | |
{ | |
position: absolute; | |
width: 50px; height: 50px; | |
background: #ffb; padding: 5px; | |
border: 2px solid #999; | |
} | |
</style> | |
</head> | |
<body> | |
X: <input type="text" id="left"/> | |
Y: <input type="text" id="top"/> | |
<div id="mydiv" class="dstyle">drag me</div> | |
</body> |
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 app = require('http').createServer(handler) | |
, io = require('socket.io').listen(app) | |
, fs = require('fs') | |
app.listen(8080); | |
function handler (req, res) { | |
fs.readFile(__dirname + '/index.html', | |
function (err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
var lastPosition = { x: 0, y: 0 }; // whatever default data | |
io.sockets.on('connection', function (socket) { | |
socket.emit('update_position', lastPosition); | |
socket.on('receive_position', function (data) { | |
lastPosition = data; | |
socket.broadcast.emit('update_position', data); // send `data` to all other clients | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, I am trying to do the same but have some difficulties. The element being dragged is generated dynamically. So far when I generate the element, it is emitted to all the clients properly. Its the dragging and dropping where I am having problems.
Here is my code:
index.html
indexPublic.js
app.js
Any help will be appreciated.