added wss tools 4

This commit is contained in:
2026-06-18 16:12:53 +03:30
parent 2a77c24dcc
commit f44063dbd2
6 changed files with 268 additions and 56 deletions
+24 -13
View File
@@ -8,25 +8,19 @@ use Illuminate\Support\Facades\Cache;
class PriceBroadcastService
{
private const LAST_HASH_KEY = 'ws_broadcast:last_hash';
private const LAST_HASH_KEY = 'price_broadcast:last_hash';
private const LIST_HASH_KEY = 'ws_broadcast:list_hash';
private const LIST_HASH_KEY = 'price_broadcast:list_hash';
/**
* montzar فقط trigger «price_update» را به کلاینت‌ها relay می‌کند.
* برادکست SSE — payload کامل last/list وقتی داده عوض شود.
*
* @param array<string, mixed>|null $lastPayload
*/
public function broadcastAfterPriceUpdate(?array $lastPayload = null, bool $force = false): void
{
$ws = app(WebsocketBroadcastService::class);
if (! $ws->isEnabled()) {
return;
}
if ($force) {
$ws->sendEvent('price_update', []);
$stream = app(PriceStreamService::class);
if (! $stream->isEnabled()) {
return;
}
@@ -35,6 +29,19 @@ class PriceBroadcastService
$lastPayload = $cache->get() ?? $cache->buildFromDatabase();
}
if ($force) {
if (is_array($lastPayload) && $lastPayload !== []) {
$stream->publish('last', $lastPayload);
}
$listPayload = $this->buildProductListPayload();
if ($listPayload !== null) {
$stream->publish('list', $listPayload);
}
return;
}
$lastChanged = is_array($lastPayload) && $lastPayload !== []
&& $this->rememberHashIfChanged(self::LAST_HASH_KEY, $lastPayload);
@@ -42,8 +49,12 @@ class PriceBroadcastService
$listChanged = $listPayload !== null
&& $this->rememberHashIfChanged(self::LIST_HASH_KEY, $listPayload);
if ($lastChanged || $listChanged) {
$ws->sendEvent('price_update', []);
if ($lastChanged) {
$stream->publish('last', $lastPayload);
}
if ($listChanged) {
$stream->publish('list', $listPayload);
}
}