sharing text with Jquery & php
I am try to create an html5 whiteboard. In the white board, the content is in svgformat(as for just text), using svgcanavas js library. Am updating the whiteboard content across all other who have joined the session like this
function change() {
var svg = $('#svgcanvas').html();
//$('body').html(hi);
$.post("php/update.php", { svg: svg } );
}
setInterval("change()",100);
Php is like this
<?php
$svg = $_POST['svg'];
$myFile = "svg.tmp";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $svg;
fwrite($fh, $stringData);
fclose($fh);
?>
And the client side js
function get() {
$.get('php/get.php', function(data) {
$('#svgcanv开发者_JS百科as').html(data);
});
}
setInterval("get()",100);
And php
<?php
$file = "svg.tmp";
$fh = fopen($file, 'r');
$theData = fread($fh, filesize($file));
echo $theData;
fclose($fh);
?>
There will be only maximum 20 people at a time using this whiteboard. Is there a better way to do the sync. And Is there a way to detect only the lines that have changed and update that alone? because file size keep increases and takes longer time going forward.
So I want to know:
- Is this the best way I can do it?
- Is there way to detect only the changed line in svg and update that alone?
- Will this work when 20 people are accessing the same file,
svg.tmp
?
1: Yes, this is about the only way until more browsers start supporting HTML5 WebSockets. Just don't poll the server so much, about every 5 sec should be good.
2: No, not really
3: Yes, it should.
Hope that helps!
精彩评论