1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/minchat_ynh.git synced 2024-09-03 19:36:29 +02:00

More simple, more efficient

This commit is contained in:
Chtixof 2015-04-11 19:32:50 +02:00
parent 8a216a6d8c
commit b4de152c2a
6 changed files with 33 additions and 187 deletions

View file

@ -1,9 +1,6 @@
; how often get data from serwer ; how often get data from serwer
interval = 2500 ; milliseconds interval = 2500 ; milliseconds
; extra time for cache to secure transmission delay
cache = 60000 ; milliseconds
; authorized user:room (comma separated) ; authorized user:room (comma separated)
; caution: names must be valid for file names ! ; caution: names must be valid for file names !
; :room with no user name is a room opened to any user name ; :room with no user name is a room opened to any user name

View file

@ -1,24 +1,4 @@
<?php <?php
function v($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>' . print_r($v, true) . '</pre>';
if ($czyscHtmlIExit) exit;
}
function vv($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>';
var_dump($v);
echo '</pre>';
if ($czyscHtmlIExit) exit;
}
function vvv($var, & $result = null, $is_view = true)
{
if (is_array($var) || is_object($var)) foreach ($var as $key=> $value) vvv($value, $result[$key], false);
else $result = $var;
if ($is_view) v($result);
}
function getarr($arr,$key,$default) { function getarr($arr,$key,$default) {
return isset($arr[$key]) ? $arr[$key] : $default; return isset($arr[$key]) ? $arr[$key] : $default;
} }
@ -32,11 +12,9 @@ function deleteOldHistory() {
} }
} }
//-------------------------
// init setup.ini parms // init setup.ini parms
$ini = parse_ini_file('conf/setup.ini'); $ini = parse_ini_file('conf/setup.ini');
$interval= getarr($ini,'interval',2500); $interval= getarr($ini,'interval',2500);
$delay= $interval+getarr($ini,'cache',60000);
$auth= explode(',',getarr($ini,'auth','')); $auth= explode(',',getarr($ini,'auth',''));
// read args // read args
@ -108,10 +86,11 @@ if ($name.$room=="") {
</div> </div>
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script> <script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
// jQuery Document
$(document).ready(function() { $(document).ready(function() {
var id = 'undefined'; var id = 'undefined';
//If user submits the form var pos = 0;
var lastdate = 0;
$("#submitmsg").click(function() { $("#submitmsg").click(function() {
var clientmsg = $("#usermsg").val(); var clientmsg = $("#usermsg").val();
$("#usermsg").val(''); $("#usermsg").val('');
@ -119,8 +98,7 @@ if ($name.$room=="") {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: 'post.php', url: 'post.php',
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>',delay:'<?php echo $delay; ?>'}, data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>'},
//cache: false,
async: false, async: false,
success: function(data) { success: function(data) {
loadLog(); loadLog();
@ -132,32 +110,30 @@ if ($name.$room=="") {
return false; return false;
}); });
//Load the file containing the chat log
function loadLog() { function loadLog() {
var oldscrollHeight = $("#chatbox")[0].scrollHeight; var oldscrollHeight = $("#chatbox")[0].scrollHeight;
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: 'server.php', url: 'server.php',
data: {id: id,room:'<?php echo $room; ?>'}, data: {room:'<?php echo $room; ?>', pos: pos, lastdate: lastdate},
dataType: 'json', dataType: 'json',
//cache: false,
async: false, async: false,
success: function(data) { success: function(data) {
id = data.id; pos = data.pos;
var html = ''; var html = '';
var date; var date;
for (var k in data.data.reverse()) { for (var k in data.data) {
date = new Date(parseInt(data.data[k][0])*1000); lastdate = data.data[k][0];
date = date.toLocaleTimeString(); date = new Date(parseInt(lastdate)*1000);
date = date.replace(/([\d]+\D+[\d]{2})\D+[\d]{2}(.*)/, '$1$2'); date = date.toLocaleTimeString().substr(0,5);
html = html html = html
+"<div class='msgln'>("+date+") <b>" +"("+date+") <b>"
+data.data[k][1]+"</b>: "+data.data[k][2]+"<br></div>"; +data.data[k][1]+"</b>: "+data.data[k][2]+"<br>";
} }
$("#chatbox").append(html); //Insert chat messages into the #chatbox div $("#chatbox").append(html);
var newscrollHeight = $("#chatbox")[0].scrollHeight; var newscrollHeight = $("#chatbox")[0].scrollHeight;
if (newscrollHeight > oldscrollHeight) { if (newscrollHeight > oldscrollHeight) {
$("#chatbox").scrollTop($("#chatbox")[0].scrollHeight); $("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
} }
}, },
}); });

View file

@ -1,42 +1,15 @@
<?php <?php
function v($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>' . print_r($v, true) . '</pre>';
if ($czyscHtmlIExit) exit;
}
function vv($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>';
var_dump($v);
echo '</pre>';
if ($czyscHtmlIExit) exit;
}
function vvv($var, & $result = null, $is_view = true)
{
if (is_array($var) || is_object($var)) foreach ($var as $key=> $value) vvv($value, $result[$key], false);
else $result = $var;
if ($is_view) v($result);
}
$text = isset($_POST['text']) ? $_POST['text'] : ''; $text = isset($_POST['text']) ? $_POST['text'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : ''; $name = isset($_POST['name']) ? $_POST['name'] : '';
$room = isset($_POST['room']) ? $_POST['room'] : ''; $room = isset($_POST['room']) ? $_POST['room'] : '';
$delay = isset($_POST['delay']) ? $_POST['delay'] : '';
if ($text === '' || $name === '' || $delay === '') return; if ($text === '' || $name === '' ) return;
$isApc = extension_loaded('apc');
$time = time(); $time = time();
$date = date('Y-m-d', $time); $date = date('Y-m-d', $time);
$uniqid = uniqid();
$id = $time.'-'.$uniqid;
$historyDir = './history/'; $historyDir = './history/';
$tmpFile = $historyDir.$room.'cache2100-01-01';
$historyFile = $historyDir.$room.$date; $historyFile = $historyDir.$room.$date;
$fh = @fopen($historyFile, 'a'); $fh = @fopen($historyFile, 'a');
@ -48,45 +21,9 @@ if ($fh === false) {
/* start semafore */ /* start semafore */
flock($fh, LOCK_EX); flock($fh, LOCK_EX);
// data $data = array($time, $name, stripslashes(htmlspecialchars($text)));
$data = array($id, $name, stripslashes(htmlspecialchars($text)));
// write history
fwrite($fh, implode('>', $data)."\n"); fwrite($fh, implode('>', $data)."\n");
// cache
if ($isApc) {
$cache = apc_fetch('chat');
if ($cache === false) {
$cache = array();
}
} else {
$cache = @file_get_contents($tmpFile);
if ($cache === false) {
$cache = array();
} else {
$cache = unserialize($cache);
}
}
array_unshift($cache, $data);
// delete expired cache
$expireTime = floor($time - $delay);
foreach (array_reverse($cache,true) as $k => $e) {
if ($e[0] < $expireTime) {
unset($cache[$k]);
} else {
break;
}
}
if ($isApc) {
apc_store('chat', $cache);
} else {
file_put_contents($tmpFile, serialize($cache));
}
/* end semafore */ /* end semafore */
flock($fh, LOCK_UN); flock($fh, LOCK_UN);
fclose($fh); fclose($fh);

View file

@ -1,79 +1,26 @@
<?php <?php
function v($v, $czyscHtmlIExit = false) { $pos = isset($_REQUEST['pos']) ? $_REQUEST['pos'] : '';
if ($czyscHtmlIExit) ob_end_clean(); $lastdate = isset($_REQUEST['lastdate']) ? $_REQUEST['lastdate'] : '';
echo '<pre>' . print_r($v, true) . '</pre>'; $room = isset($_REQUEST['room']) ? $_REQUEST['room'] : '';
if ($czyscHtmlIExit) exit; $date = date('Y-m-d');
} $file = './history/'.$room.$date;
function vv($v, $czyscHtmlIExit = false) {
if ($czyscHtmlIExit) ob_end_clean();
echo '<pre>';
var_dump($v);
echo '</pre>';
if ($czyscHtmlIExit) exit;
}
function vvv($var, & $result = null, $is_view = true)
{
if (is_array($var) || is_object($var)) foreach ($var as $key=> $value) vvv($value, $result[$key], false);
else $result = $var;
if ($is_view) v($result); if (!ctype_digit($pos) || !file_exists($file)) return;
}
$id = isset($_POST['id']) ? $_POST['id'] : ''; settype($pos, "integer");
$room = isset($_POST['room']) ? $_POST['room'] : ''; settype($lastdate, "integer");
if ($id === '' ) return; if ($date!=date('Y-m-d',$lastdate)) $pos=0;
$isApc = extension_loaded('apc');
$cache = $isApc ? apc_fetch('chat') : @unserialize(file_get_contents('./history/'.$room.'cache2100-01-01'));
$data = array(); $data = array();
if ($id === 'undefined') { $fh =fopen($file, "r");
$id = empty($cache) ? 0 : $cache[0][0];
// first refresh : loads 50 last msg from history if (fseek($fh, $pos)==0) {
$file = './history/'.$room.date('Y-m-d'); $i=0;
if (file_exists($file)) { while($lig=fgets($fh)){ $data[$i++] = explode('>', $lig); }
$history = file($file, FILE_IGNORE_NEW_LINES); $pos =ftell($fh);
$history = array_reverse($history); echo json_encode(array('pos' => $pos, 'data' => $data));
foreach ($history as & $ref) {
$ref = explode('>', $ref);
}
$data = array_slice($history, 0, 50);
}
} elseif ($id === '0') {
if (!empty($cache)) {
$id = $cache[0][0];
$data = $cache;
}
} else {
$end = end($cache);
// read data from cache
if ($id >= $end[0]) {
foreach ($cache as $k => $c) {
if ($c[0] === $id) break;
}
$data = array_slice($cache, 0, $k);
}
// read data from history (any problem witch Internet and are delays)
else {
$date = date('Y-m-d');
$history = array();
while (($history = array_merge(file('./history/'.$room.$date, FILE_IGNORE_NEW_LINES), $history)) && $history[0] > $id) {
$date = date('Y-m-d', strtotime('-1 day', strtotime($date)));
if (!file_exists('./history/'.$room.$date)) break;
}
// prepare history
$history = array_reverse($history);
foreach ($history as & $ref) {
$ref = explode('>', $ref);
}
// get data
foreach ($history as $k => $h) {
if ($h[0] === $id) break;
}
$data = array_slice($history, 0, $k);
}
$id = $cache[0][0];
} }
echo json_encode(array('id' => $id, 'data' => $data)); ?>

View file

@ -1,9 +0,0 @@
; how often get data from serwer
interval = 2500 ; in milliseconds
; extra time for cache when is problem with Internet
expire_cache = 60 ; in seconds
; time for history
expire_history = 1 ; in days

View file

@ -69,8 +69,6 @@ a:hover { text-decoration: underline; }
.logout { float: right; } .logout { float: right; }
.msgln { margin:0 0 2px 0; }
.tform { display: table; } .tform { display: table; }
.tform p { display: table-row; } .tform p { display: table-row; }
.tform label { display: table-cell; } .tform label { display: table-cell; }