added wss tools 4
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user