added wss tools 7

This commit is contained in:
2026-06-18 19:44:23 +03:30
parent 017ab348c7
commit c15136c5e7
4 changed files with 125 additions and 9 deletions
+44
View File
@@ -100,6 +100,15 @@ class PriceStreamService
$key = $this->inboxKey();
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
$lastEvent = $events !== [] ? $events[array_key_last($events)] : null;
if (is_array($lastEvent)
&& ($lastEvent['event'] ?? null) === $event
&& $this->eventDataHash($event, $lastEvent['data'] ?? null) === $this->eventDataHash($event, $data)) {
return;
}
$inbox['seq'] = (int) ($inbox['seq'] ?? 0) + 1;
$inbox['events'][] = [
'id' => $inbox['seq'],
@@ -119,6 +128,41 @@ class PriceStreamService
return self::INBOX_PREFIX.'global';
}
/**
* @param array<string, mixed>|array<int, mixed> $data
*/
private function eventDataHash(string $event, mixed $data): string
{
if (! is_array($data)) {
return md5('');
}
if ($event === 'last') {
return md5(json_encode(PriceBroadcastService::lastPriceIdentity($data), JSON_UNESCAPED_UNICODE));
}
if ($event === 'list') {
$identity = [];
foreach ($data as $product) {
if (! is_array($product)) {
continue;
}
$identity[] = [
'id' => (int) ($product['id'] ?? 0),
'price' => (int) ($product['price'] ?? 0),
'price_profit' => (int) ($product['price_profit'] ?? 0),
'show' => (bool) ($product['show'] ?? true),
'active' => (bool) ($product['active'] ?? true),
];
}
return md5(json_encode($identity, JSON_UNESCAPED_UNICODE));
}
return md5(json_encode($data, JSON_UNESCAPED_UNICODE));
}
private function normalizeHost(string $host): string
{
$host = strtolower(trim($host));