-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgs.php
More file actions
53 lines (48 loc) · 1.19 KB
/
Copy pathmsgs.php
File metadata and controls
53 lines (48 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
function getMsgs() {
$lines = file("messages.json");
$strline = "";
foreach($lines as $line) {
$strline = $strline.$line;
}
return json_decode($strline);
}
function sendMsgs($ws) {
$arr = getMsgs();
// call push method of $ws with the length of $arr as a parameter
$ws->push(json_encode(array(
"channel" => "message count",
"data" => count($arr)
)));
foreach($arr as $message) {
$ws->push(json_encode(array("channel"=>"message", "data"=>json_encode($message))));
}
}
function storeMsg($data, $ws) {
echo "called storeMsg\n";
/*$objData = json_decode($data);
$msg = $objData->time." ".$objData->name.":".$objData->msg;*/
$objData = json_decode($data);
$msg = $objData;
if (strlen($objData->msg) > 400) {
return;
}
ob_start();
try {
readfile("messages.json");
} catch (Exception $e) {
echo $e."\n";
exit();
}
$fdata = ob_get_contents();
ob_end_clean();
$arr = json_decode($fdata);
if (count($arr) >= 400) {
array_shift($arr);
}
array_push($arr, $msg);
$sample2 = fopen("messages.json", "w");
fwrite($sample2, json_encode($arr));
fclose($sample2);
return json_encode(array("channel"=>"message", "data"=>$data));
}