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:
parent
8a216a6d8c
commit
b4de152c2a
6 changed files with 33 additions and 187 deletions
|
@ -1,9 +1,6 @@
|
|||
; how often get data from serwer
|
||||
interval = 2500 ; milliseconds
|
||||
|
||||
; extra time for cache to secure transmission delay
|
||||
cache = 60000 ; milliseconds
|
||||
|
||||
; authorized user:room (comma separated)
|
||||
; caution: names must be valid for file names !
|
||||
; :room with no user name is a room opened to any user name
|
||||
|
|
|
@ -1,24 +1,4 @@
|
|||
<?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) {
|
||||
return isset($arr[$key]) ? $arr[$key] : $default;
|
||||
}
|
||||
|
@ -32,11 +12,9 @@ function deleteOldHistory() {
|
|||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
// init setup.ini parms
|
||||
$ini = parse_ini_file('conf/setup.ini');
|
||||
$interval= getarr($ini,'interval',2500);
|
||||
$delay= $interval+getarr($ini,'cache',60000);
|
||||
$auth= explode(',',getarr($ini,'auth',''));
|
||||
|
||||
// read args
|
||||
|
@ -108,10 +86,11 @@ if ($name.$room=="") {
|
|||
</div>
|
||||
<script type="text/javascript" src="lib/jquery-2.1.3.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
// jQuery Document
|
||||
$(document).ready(function() {
|
||||
var id = 'undefined';
|
||||
//If user submits the form
|
||||
var pos = 0;
|
||||
var lastdate = 0;
|
||||
|
||||
$("#submitmsg").click(function() {
|
||||
var clientmsg = $("#usermsg").val();
|
||||
$("#usermsg").val('');
|
||||
|
@ -119,8 +98,7 @@ if ($name.$room=="") {
|
|||
$.ajax({
|
||||
type: 'POST',
|
||||
url: 'post.php',
|
||||
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>',delay:'<?php echo $delay; ?>'},
|
||||
//cache: false,
|
||||
data: {text: clientmsg,name:'<?php echo $name; ?>',room:'<?php echo $room; ?>'},
|
||||
async: false,
|
||||
success: function(data) {
|
||||
loadLog();
|
||||
|
@ -132,32 +110,30 @@ if ($name.$room=="") {
|
|||
return false;
|
||||
});
|
||||
|
||||
//Load the file containing the chat log
|
||||
function loadLog() {
|
||||
var oldscrollHeight = $("#chatbox")[0].scrollHeight;
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: 'server.php',
|
||||
data: {id: id,room:'<?php echo $room; ?>'},
|
||||
data: {room:'<?php echo $room; ?>', pos: pos, lastdate: lastdate},
|
||||
dataType: 'json',
|
||||
//cache: false,
|
||||
async: false,
|
||||
success: function(data) {
|
||||
id = data.id;
|
||||
pos = data.pos;
|
||||
var html = '';
|
||||
var date;
|
||||
for (var k in data.data.reverse()) {
|
||||
date = new Date(parseInt(data.data[k][0])*1000);
|
||||
date = date.toLocaleTimeString();
|
||||
date = date.replace(/([\d]+\D+[\d]{2})\D+[\d]{2}(.*)/, '$1$2');
|
||||
for (var k in data.data) {
|
||||
lastdate = data.data[k][0];
|
||||
date = new Date(parseInt(lastdate)*1000);
|
||||
date = date.toLocaleTimeString().substr(0,5);
|
||||
html = html
|
||||
+"<div class='msgln'>("+date+") <b>"
|
||||
+data.data[k][1]+"</b>: "+data.data[k][2]+"<br></div>";
|
||||
+"("+date+") <b>"
|
||||
+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;
|
||||
if (newscrollHeight > oldscrollHeight) {
|
||||
$("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
|
||||
$("#chatbox").scrollTop($("#chatbox")[0].scrollHeight);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
@ -1,42 +1,15 @@
|
|||
<?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'] : '';
|
||||
$name = isset($_POST['name']) ? $_POST['name'] : '';
|
||||
$room = isset($_POST['room']) ? $_POST['room'] : '';
|
||||
$delay = isset($_POST['delay']) ? $_POST['delay'] : '';
|
||||
|
||||
if ($text === '' || $name === '' || $delay === '') return;
|
||||
|
||||
$isApc = extension_loaded('apc');
|
||||
if ($text === '' || $name === '' ) return;
|
||||
|
||||
$time = time();
|
||||
$date = date('Y-m-d', $time);
|
||||
$uniqid = uniqid();
|
||||
$id = $time.'-'.$uniqid;
|
||||
|
||||
$historyDir = './history/';
|
||||
|
||||
$tmpFile = $historyDir.$room.'cache2100-01-01';
|
||||
$historyFile = $historyDir.$room.$date;
|
||||
|
||||
$fh = @fopen($historyFile, 'a');
|
||||
|
@ -48,45 +21,9 @@ if ($fh === false) {
|
|||
/* start semafore */
|
||||
flock($fh, LOCK_EX);
|
||||
|
||||
// data
|
||||
$data = array($id, $name, stripslashes(htmlspecialchars($text)));
|
||||
|
||||
// write history
|
||||
$data = array($time, $name, stripslashes(htmlspecialchars($text)));
|
||||
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 */
|
||||
flock($fh, LOCK_UN);
|
||||
fclose($fh);
|
||||
|
|
|
@ -1,79 +1,26 @@
|
|||
<?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;
|
||||
$pos = isset($_REQUEST['pos']) ? $_REQUEST['pos'] : '';
|
||||
$lastdate = isset($_REQUEST['lastdate']) ? $_REQUEST['lastdate'] : '';
|
||||
$room = isset($_REQUEST['room']) ? $_REQUEST['room'] : '';
|
||||
$date = date('Y-m-d');
|
||||
$file = './history/'.$room.$date;
|
||||
|
||||
if ($is_view) v($result);
|
||||
}
|
||||
if (!ctype_digit($pos) || !file_exists($file)) return;
|
||||
|
||||
$id = isset($_POST['id']) ? $_POST['id'] : '';
|
||||
$room = isset($_POST['room']) ? $_POST['room'] : '';
|
||||
settype($pos, "integer");
|
||||
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();
|
||||
if ($id === 'undefined') {
|
||||
$id = empty($cache) ? 0 : $cache[0][0];
|
||||
// first refresh : loads 50 last msg from history
|
||||
$file = './history/'.$room.date('Y-m-d');
|
||||
if (file_exists($file)) {
|
||||
$history = file($file, FILE_IGNORE_NEW_LINES);
|
||||
$history = array_reverse($history);
|
||||
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];
|
||||
$fh =fopen($file, "r");
|
||||
|
||||
if (fseek($fh, $pos)==0) {
|
||||
$i=0;
|
||||
while($lig=fgets($fh)){ $data[$i++] = explode('>', $lig); }
|
||||
$pos =ftell($fh);
|
||||
echo json_encode(array('pos' => $pos, 'data' => $data));
|
||||
}
|
||||
|
||||
echo json_encode(array('id' => $id, 'data' => $data));
|
||||
?>
|
|
@ -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
|
|
@ -69,8 +69,6 @@ a:hover { text-decoration: underline; }
|
|||
|
||||
.logout { float: right; }
|
||||
|
||||
.msgln { margin:0 0 2px 0; }
|
||||
|
||||
.tform { display: table; }
|
||||
.tform p { display: table-row; }
|
||||
.tform label { display: table-cell; }
|
||||
|
|
Loading…
Reference in a new issue