Skip to content

Commit

Permalink
Merge pull request #1061 from Anisan/patch-3
Browse files Browse the repository at this point in the history
Websocket optimization
  • Loading branch information
sergejey authored Dec 12, 2022
2 parents 7e5855f + a219fbf commit 37b32b5
Show file tree
Hide file tree
Showing 2 changed files with 499 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class MajordomoApplication extends Application
private $_filename = '';
private $_latestAlive = 0;
private $_scenesDynamicElements = array();
private $_started;

public function __construct() {
$this->_started = date("Y-m-d H:i:s");
}

public function onConnect($client)
{
Expand Down Expand Up @@ -77,13 +82,47 @@ private function cycleAlive()
if ($ws_clients_total != $old_value) {
setGlobal('WSClientsTotal', $ws_clients_total, 1);
}
// send ping
foreach ($this->_clients as $client){
if ($client->getClientIp() != "127.0.0.1")
$client->send('ping', 'ping', false);
}
}
global $websockets_script_started;
if ($websockets_script_started > 0 && (time() - $websockets_script_started) > 6 * 60 * 60) {
exit; // restart every 6 hours
}
}

private function _actionStatus($data, $client_id)
{
$this->cycleAlive();
$status = [];
$clients = [];
$status["YOUR_ID"] = $client_id;
$status["STARTED"] = $this->_started;
$status["COUNT_CLIENTS"] = count($this->_clients);
foreach ($this->_clients as $client) {
$data = [];
$data['ID'] = $client->getClientId();
$data['IP'] = $client->getClientIp();
$data['CONNECTED'] = $client->connected;
$traffic = [];
$traffic['IN_PACKET'] = $client->inPacket;
$traffic['OUT_PACKET'] = $client->outPacket;
$traffic['IN_BYTES'] = $client->inBytes;
$traffic['OUT_BYTES'] = $client->outBytes;
$data['TRAFFIC'] = $traffic;
$data['SUBCRIBED_TO'] = $client->subscribedTo;
$data['WATCHED_PROPERTIES'] = $client->watchedProperties;
$clients[] = $data;
}
$status["CLIENTS"] = $clients;
$status["COUNT_CACHED"] = count($this->_cachedProperties);
$status["CACHED"] = $this->_cachedProperties;
$encodedData = $this->_encodeData('status', json_encode($status));
$this->_clients[$client_id]->send($encodedData);
}

private function _actionSubscribe($data, $client_id)
{
Expand Down Expand Up @@ -230,10 +269,24 @@ private function _actionSubscribe($data, $client_id)
if (defined('DEBUG_WEBSOCKETS') && DEBUG_WEBSOCKETS == 1) {
DebMes($this->_clients[$client_id]->getClientIp() . " Watching:\n" . json_encode($tmp), 'websockets');
}
$send_data = array();
foreach ($tmp as $property) {
$this->_clients[$client_id]->subscribedTo['properties'][mb_strtolower($property, 'UTF-8')] = 1;
$this->_clients[$client_id]->watchedProperties[mb_strtolower($property, 'UTF-8')]['properties'] = 1;
$property_name_lc = mb_strtolower($property, 'UTF-8');
$this->_clients[$client_id]->subscribedTo['properties'][$property_name_lc] = 1;
$this->_clients[$client_id]->watchedProperties[$property_name_lc]['properties'] = 1;
if (isSet($this->_cachedProperties[$property_name_lc]))
$send_data[] = array('PROPERTY' => $property, 'VALUE' => $this->_cachedProperties[$property_name_lc]);
else
$send_data[] = array('PROPERTY' => $property, 'VALUE' => getGlobal($property));
}
if (isset($send_data[0])) {
if (defined('DEBUG_WEBSOCKETS') && DEBUG_WEBSOCKETS == 1) {
DebMes($this->_clients[$client_id]->getClientIp() . " Sending cached properties\n" . json_encode($send_data), 'websockets');
}
$encodedData = $this->_encodeData('properties', json_encode($send_data));
$this->_clients[$client_id]->send($encodedData);
}

}

if ($data['TYPE'] == 'events') {
Expand Down Expand Up @@ -307,8 +360,9 @@ private function _actionPostProperty($data)
$property_name_lc = mb_strtolower($property_name,'UTF-8');
$property_value = $received_values[$property_name];
if (defined('DEBUG_WEBSOCKETS') && DEBUG_WEBSOCKETS == 1) {
//DebMes("Update property ".$property_name,'websockets');
DebMes("Update property ".$property_name." => ".$property_value." (total cache:".count($this->_cachedProperties).")",'websockets');
}
$this->_cachedProperties[$property_name_lc] = $property_value;
//process property update
$found_subscribers = 0;

Expand Down Expand Up @@ -501,7 +555,7 @@ private function _actionPostProperty($data)
//properties
if (isset($client->watchedProperties[$property_name_lc]['properties'])) {
$send_data = array();
$send_data[] = array('PROPERTY' => $property_name, 'VALUE' => getGlobal($property_name));
$send_data[] = array('PROPERTY' => $property_name, 'VALUE' => $property_value);
if (isset($send_data[0])) {
if (defined('DEBUG_WEBSOCKETS') && DEBUG_WEBSOCKETS == 1) {
DebMes($client->getClientIp() . " Sending updated properties\n" . json_encode($send_data), 'websockets');
Expand All @@ -516,11 +570,10 @@ private function _actionPostProperty($data)
$tmp = explode('.', $property_name_lc);
if (isset($client->watchedProperties[$tmp[0]]['properties'])) {
$send_data = array();
$send_data[] = array('PROPERTY' => $property_name, 'VALUE' => getGlobal($property_name));
$send_data[] = array('PROPERTY' => $property_name, 'VALUE' => $property_value);
if (isset($send_data[0])) {
if (defined('DEBUG_WEBSOCKETS') && DEBUG_WEBSOCKETS == 1) {
DebMes($client->getClientIp() . " Sending updated properties\n" . json_encode($send_data), 'websockets');
DebMes($client->getClientIp() . " Sending updated properties\n" . json_encode($send_data), 'websockets');
}
$encodedData = $this->_encodeData('properties', json_encode($send_data));
$client->send($encodedData);
Expand Down
Loading

0 comments on commit 37b32b5

Please sign in to comment.