-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatusApp.php
More file actions
67 lines (54 loc) · 1.36 KB
/
StatusApp.php
File metadata and controls
67 lines (54 loc) · 1.36 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
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* @author David Stelter <david.stelter@gmail.com>
* @copyright Copyright (c) 2011, David Stelter
* @license http://www.opensource.org/licenses/MIT
* @link http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
*/
class StatusApp implements iWebSocketApp {
private $conn = null;
public function onMessage($msg) {
switch ($msg) {
case 'pid':
$this->conn->send('pid: ' . getmypid());
break;
case 'mem':
$mem = memory_get_usage(true);
$mb = $mem / pow(2,20);
$this->conn->send("{$mb}MB");
break;
case 'tail':
$this->doTail();
break;
default:
$this->conn->send("unknown command '$msg'");
break;
}
echo "StatusApp got message '$msg'\n";
}
public function doTail() {
if (! $f = fopen(dirname(__FILE__).'/foo.txt', 'r')) {
$this->conn->send("Failed to open file");
return false;
}
while (true) {
if (false !== ($string = fread($f, 125))) {
if ($string) {
$this->conn->send($string);
} else {
sleep(1);
}
}
}
}
public function onError($error) {
echo "error!\n";
}
public function onClose($data) {
echo "StatusApp shutdown for client {$this->conn->getAddressString()}\n";
} // onClose()
public function __construct(WebSocketConnection $conn) {
$this->conn = $conn;
echo "StatusApp instantiated for host {$conn->getHost()}\n";
}
} // StatusApp{}