fix some issue from sse

This commit is contained in:
2026-08-27 11:53:29 +03:30
parent 40b185187c
commit 17d79c0ce6
2 changed files with 95 additions and 19 deletions
@@ -53,33 +53,48 @@ class PriceStreamController extends Controller
$cache = app(LivePriceCacheService::class);
$config = app(\App\Services\ConfigService::class);
$last = $cache->get();
// کش ممکن است no_quote صفرِ کوتاه‌عمر داشته باشد؛ با build از montzar_live_price جبران کن
if (! is_array($last) || ! $config->payloadHasDisplayablePrice($last)) {
$last = $cache->buildFromDatabase();
}
if (is_array($last) && $last !== []) {
if ($customerId > 0) {
$last = app(\App\Services\CustomerSpecialPricingService::class)
->applyToLastPayload($last, $customerId);
$specialPricing = app(\App\Services\CustomerSpecialPricingService::class);
$zaarbox = app(ZaarboxPriceService::class);
$broadcast = app(PriceBroadcastService::class);
$resolveLastPayload = static function () use ($cache, $config, $specialPricing, $zaarbox, $customerId): ?array {
$last = $cache->get();
if (! is_array($last) || ! $config->payloadHasDisplayablePrice($last)) {
$last = $cache->buildFromDatabase();
}
$send('last', app(ZaarboxPriceService::class)->attachGoldOunceToPayload($last));
if (! is_array($last) || $last === []) {
return null;
}
if ($customerId > 0) {
$last = $specialPricing->applyToLastPayload($last, $customerId);
}
return $zaarbox->attachGoldOunceToPayload($last);
};
$last = $resolveLastPayload();
$lastSentIdentity = null;
if (is_array($last) && $last !== []) {
$send('last', $last);
$lastSentIdentity = PriceBroadcastService::lastPriceIdentity($last);
}
$hoursSettings = app(\App\Services\ConfigService::class)->clientTradingHoursSettings();
$hoursSettings = $config->clientTradingHoursSettings();
if ($hoursSettings !== []) {
$send('settings', $hoursSettings);
}
$list = app(PriceBroadcastService::class)->buildListPayload(false);
$list = $broadcast->buildListPayload(false);
$lastSentListIdentity = null;
if (is_array($list) && $list !== []) {
$send('list', $list);
$lastSentListIdentity = PriceBroadcastService::listIdentity($list);
}
if ($customerId > 0) {
$send(
'special-pricing',
app(\App\Services\CustomerSpecialPricingService::class)->clientPayloadForCustomer($customerId)
$specialPricing->clientPayloadForCustomer($customerId)
);
}
@@ -109,8 +124,16 @@ class PriceStreamController extends Controller
while (! connection_aborted()) {
foreach ($service->consumeSince($lastId) as $event) {
$send((string) $event['event'], $event['data']);
$eventName = (string) $event['event'];
$eventData = $event['data'];
$send($eventName, $eventData);
$lastId = (int) $event['id'];
if ($eventName === 'last' && is_array($eventData)) {
$lastSentIdentity = PriceBroadcastService::lastPriceIdentity($eventData);
}
if ($eventName === 'list' && is_array($eventData)) {
$lastSentListIdentity = PriceBroadcastService::listIdentity($eventData);
}
}
if ($customerId > 0) {
@@ -121,12 +144,36 @@ class PriceStreamController extends Controller
}
$heartbeatTicks++;
// هر ۱ ثانیه: قیمت را مستقیم از کش/DB بکش
// (اگر inbox بین tick و PHP-FPM هم‌رسانی نشود، فقط ping دیده می‌شد)
if ($heartbeatTicks % 2 === 0) {
$pulled = $resolveLastPayload();
if (is_array($pulled) && $config->payloadHasDisplayablePrice($pulled)) {
$identity = PriceBroadcastService::lastPriceIdentity($pulled);
if ($lastSentIdentity !== $identity) {
$send('last', $pulled);
$lastSentIdentity = $identity;
}
}
}
// هر ۵ ثانیه — LiteSpeed/پروکسی اتصال بیکار را نمی‌بندد
if ($heartbeatTicks >= 10) {
$send('ping', ['ts' => time()]);
if ($customerId > 0) {
$presence->touch($customerId);
}
$pulledList = $broadcast->buildListPayload(true);
if (is_array($pulledList) && $pulledList !== []) {
$listIdentity = PriceBroadcastService::listIdentity($pulledList);
if ($lastSentListIdentity !== $listIdentity) {
$send('list', $pulledList);
$lastSentListIdentity = $listIdentity;
}
}
$heartbeatTicks = 0;
}