|
18 | 18 | use \Wrench\Application\NamedApplication; |
19 | 19 |
|
20 | 20 | class AutoReloadApplication extends Application { |
21 | | - |
22 | | - protected $clients = array(); |
23 | | - protected $savedTimestamp = time(); |
24 | | - protected $savedTimestampPath = __DIR__."/../../../../../../public/latest-change.txt"; |
25 | | - |
26 | | - /** |
27 | | - * Set the saved timestamp. If the latest-change file doesn't exist simply use the current time as the saved time |
28 | | - */ |
29 | | - public function __construct() { |
30 | | - |
31 | | - if (file_exists($savedTimestampPath)) { |
32 | | - $this->savedTimestamp = file_get_contents($savedTimestampPath); |
33 | | - } |
34 | | - |
35 | | - } |
36 | | - |
37 | | - /** |
38 | | - * When a client connects add it to the list of connected clients |
39 | | - */ |
40 | | - public function onConnect($client) { |
41 | | - $id = $client->getId(); |
42 | | - $this->clients[$id] = $client; |
43 | | - } |
44 | | - |
45 | | - /** |
46 | | - * When a client disconnects remove it from the list of connected clients |
47 | | - */ |
48 | | - public function onDisconnect($client) { |
49 | | - $id = $client->getId(); |
50 | | - unset($this->clients[$id]); |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Dead function in this instance |
55 | | - */ |
56 | | - public function onData($data, $client) { |
57 | | - // function not in use |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Sends out a message once a second to all connected clients containing the contents of latest-change.txt |
62 | | - */ |
63 | | - public function onUpdate() { |
64 | | - |
65 | | - if (file_exists($savedTimestampPath)) { |
66 | | - $readTimestamp = file_get_contents($savedTimestampPath); |
67 | | - if ($readTimestamp != $this->savedTimestamp) { |
68 | | - foreach ($this->clients as $sendto) { |
69 | | - $sendto->send($readTimestamp); |
70 | | - } |
71 | | - $this->savedTimestamp = $readTimestamp; |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - } |
76 | | - |
| 21 | + |
| 22 | + protected $clients = array(); |
| 23 | + protected $savedTimestamp = ''; |
| 24 | + protected $savedTimestampPath = ''; |
| 25 | + |
| 26 | + /** |
| 27 | + * Set the saved timestamp. If the latest-change file doesn't exist simply use the current time as the saved time |
| 28 | + */ |
| 29 | + public function __construct() { |
| 30 | + $this->savedTimestampPath = __DIR__."/../../../../../../public/latest-change.txt"; |
| 31 | + if (file_exists($this->savedTimestampPath)) { |
| 32 | + $this->savedTimestamp = file_get_contents($this->savedTimestampPath); |
| 33 | + } else { |
| 34 | + $this->savedTimestamp = time(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * When a client connects add it to the list of connected clients |
| 40 | + */ |
| 41 | + public function onConnect($client) { |
| 42 | + $id = $client->getId(); |
| 43 | + $this->clients[$id] = $client; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * When a client disconnects remove it from the list of connected clients |
| 48 | + */ |
| 49 | + public function onDisconnect($client) { |
| 50 | + $id = $client->getId(); |
| 51 | + unset($this->clients[$id]); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Dead function in this instance |
| 56 | + */ |
| 57 | + public function onData($data, $client) { |
| 58 | + // function not in use |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sends out a message once a second to all connected clients containing the contents of latest-change.txt |
| 63 | + */ |
| 64 | + public function onUpdate() { |
| 65 | + |
| 66 | + if (file_exists($this->savedTimestampPath)) { |
| 67 | + $readTimestamp = file_get_contents($this->savedTimestampPath); |
| 68 | + if ($readTimestamp != $this->savedTimestamp) { |
| 69 | + foreach ($this->clients as $sendto) { |
| 70 | + $sendto->send($readTimestamp); |
| 71 | + } |
| 72 | + $this->savedTimestamp = $readTimestamp; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
77 | 78 | } |
0 commit comments