开发者

sharing text with Jquery & php

开发者 https://www.devze.com 2023-03-27 13:12 出处:网络
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 joi

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:

  1. Is this the best way I can do it?
  2. Is there way to detect only the changed line in svg and update that alone?
  3. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号