@@ -15,6 +15,11 @@ class MajordomoApplication extends Application
1515 private $ _filename = '' ;
1616 private $ _latestAlive = 0 ;
1717 private $ _scenesDynamicElements = array ();
18+ private $ _started ;
19+
20+ public function __construct () {
21+ $ this ->_started = date ("Y-m-d H:i:s " );
22+ }
1823
1924 public function onConnect ($ client )
2025 {
@@ -77,13 +82,47 @@ private function cycleAlive()
7782 if ($ ws_clients_total != $ old_value ) {
7883 setGlobal ('WSClientsTotal ' , $ ws_clients_total , 1 );
7984 }
85+ // send ping
86+ foreach ($ this ->_clients as $ client ){
87+ if ($ client ->getClientIp () != "127.0.0.1 " )
88+ $ client ->send ('ping ' , 'ping ' , false );
89+ }
8090 }
8191 global $ websockets_script_started ;
8292 if ($ websockets_script_started > 0 && (time () - $ websockets_script_started ) > 6 * 60 * 60 ) {
8393 exit ; // restart every 6 hours
8494 }
8595 }
8696
97+ private function _actionStatus ($ data , $ client_id )
98+ {
99+ $ this ->cycleAlive ();
100+ $ status = [];
101+ $ clients = [];
102+ $ status ["YOUR_ID " ] = $ client_id ;
103+ $ status ["STARTED " ] = $ this ->_started ;
104+ $ status ["COUNT_CLIENTS " ] = count ($ this ->_clients );
105+ foreach ($ this ->_clients as $ client ) {
106+ $ data = [];
107+ $ data ['ID ' ] = $ client ->getClientId ();
108+ $ data ['IP ' ] = $ client ->getClientIp ();
109+ $ data ['CONNECTED ' ] = $ client ->connected ;
110+ $ traffic = [];
111+ $ traffic ['IN_PACKET ' ] = $ client ->inPacket ;
112+ $ traffic ['OUT_PACKET ' ] = $ client ->outPacket ;
113+ $ traffic ['IN_BYTES ' ] = $ client ->inBytes ;
114+ $ traffic ['OUT_BYTES ' ] = $ client ->outBytes ;
115+ $ data ['TRAFFIC ' ] = $ traffic ;
116+ $ data ['SUBCRIBED_TO ' ] = $ client ->subscribedTo ;
117+ $ data ['WATCHED_PROPERTIES ' ] = $ client ->watchedProperties ;
118+ $ clients [] = $ data ;
119+ }
120+ $ status ["CLIENTS " ] = $ clients ;
121+ $ status ["COUNT_CACHED " ] = count ($ this ->_cachedProperties );
122+ $ status ["CACHED " ] = $ this ->_cachedProperties ;
123+ $ encodedData = $ this ->_encodeData ('status ' , json_encode ($ status ));
124+ $ this ->_clients [$ client_id ]->send ($ encodedData );
125+ }
87126
88127 private function _actionSubscribe ($ data , $ client_id )
89128 {
@@ -230,10 +269,24 @@ private function _actionSubscribe($data, $client_id)
230269 if (defined ('DEBUG_WEBSOCKETS ' ) && DEBUG_WEBSOCKETS == 1 ) {
231270 DebMes ($ this ->_clients [$ client_id ]->getClientIp () . " Watching: \n" . json_encode ($ tmp ), 'websockets ' );
232271 }
272+ $ send_data = array ();
233273 foreach ($ tmp as $ property ) {
234- $ this ->_clients [$ client_id ]->subscribedTo ['properties ' ][mb_strtolower ($ property , 'UTF-8 ' )] = 1 ;
235- $ this ->_clients [$ client_id ]->watchedProperties [mb_strtolower ($ property , 'UTF-8 ' )]['properties ' ] = 1 ;
274+ $ property_name_lc = mb_strtolower ($ property , 'UTF-8 ' );
275+ $ this ->_clients [$ client_id ]->subscribedTo ['properties ' ][$ property_name_lc ] = 1 ;
276+ $ this ->_clients [$ client_id ]->watchedProperties [$ property_name_lc ]['properties ' ] = 1 ;
277+ if (isSet ($ this ->_cachedProperties [$ property_name_lc ]))
278+ $ send_data [] = array ('PROPERTY ' => $ property , 'VALUE ' => $ this ->_cachedProperties [$ property_name_lc ]);
279+ else
280+ $ send_data [] = array ('PROPERTY ' => $ property , 'VALUE ' => getGlobal ($ property ));
236281 }
282+ if (isset ($ send_data [0 ])) {
283+ if (defined ('DEBUG_WEBSOCKETS ' ) && DEBUG_WEBSOCKETS == 1 ) {
284+ DebMes ($ this ->_clients [$ client_id ]->getClientIp () . " Sending cached properties \n" . json_encode ($ send_data ), 'websockets ' );
285+ }
286+ $ encodedData = $ this ->_encodeData ('properties ' , json_encode ($ send_data ));
287+ $ this ->_clients [$ client_id ]->send ($ encodedData );
288+ }
289+
237290 }
238291
239292 if ($ data ['TYPE ' ] == 'events ' ) {
@@ -307,8 +360,9 @@ private function _actionPostProperty($data)
307360 $ property_name_lc = mb_strtolower ($ property_name ,'UTF-8 ' );
308361 $ property_value = $ received_values [$ property_name ];
309362 if (defined ('DEBUG_WEBSOCKETS ' ) && DEBUG_WEBSOCKETS == 1 ) {
310- // DebMes("Update property ".$property_name,'websockets');
363+ DebMes ("Update property " .$ property_name. " => " . $ property_value . " (total cache: " . count ( $ this -> _cachedProperties ). " ) " ,'websockets ' );
311364 }
365+ $ this ->_cachedProperties [$ property_name_lc ] = $ property_value ;
312366 //process property update
313367 $ found_subscribers = 0 ;
314368
@@ -501,7 +555,7 @@ private function _actionPostProperty($data)
501555 //properties
502556 if (isset ($ client ->watchedProperties [$ property_name_lc ]['properties ' ])) {
503557 $ send_data = array ();
504- $ send_data [] = array ('PROPERTY ' => $ property_name , 'VALUE ' => getGlobal ( $ property_name ) );
558+ $ send_data [] = array ('PROPERTY ' => $ property_name , 'VALUE ' => $ property_value );
505559 if (isset ($ send_data [0 ])) {
506560 if (defined ('DEBUG_WEBSOCKETS ' ) && DEBUG_WEBSOCKETS == 1 ) {
507561 DebMes ($ client ->getClientIp () . " Sending updated properties \n" . json_encode ($ send_data ), 'websockets ' );
@@ -516,11 +570,10 @@ private function _actionPostProperty($data)
516570 $ tmp = explode ('. ' , $ property_name_lc );
517571 if (isset ($ client ->watchedProperties [$ tmp [0 ]]['properties ' ])) {
518572 $ send_data = array ();
519- $ send_data [] = array ('PROPERTY ' => $ property_name , 'VALUE ' => getGlobal ( $ property_name ) );
573+ $ send_data [] = array ('PROPERTY ' => $ property_name , 'VALUE ' => $ property_value );
520574 if (isset ($ send_data [0 ])) {
521575 if (defined ('DEBUG_WEBSOCKETS ' ) && DEBUG_WEBSOCKETS == 1 ) {
522576 DebMes ($ client ->getClientIp () . " Sending updated properties \n" . json_encode ($ send_data ), 'websockets ' );
523- DebMes ($ client ->getClientIp () . " Sending updated properties \n" . json_encode ($ send_data ), 'websockets ' );
524577 }
525578 $ encodedData = $ this ->_encodeData ('properties ' , json_encode ($ send_data ));
526579 $ client ->send ($ encodedData );
0 commit comments