Files
boursecu_server/app/Http/Controllers/Site/PriceStreamController.php
T

190 lines
7.2 KiB
PHP
Raw Normal View History

2026-06-18 16:12:53 +03:30
<?php
namespace App\Http\Controllers\Site;
use App\Http\Controllers\Controller;
2026-06-27 15:51:44 +03:30
use App\Services\AssetSummaryService;
2026-07-22 17:17:23 +03:30
use App\Services\CustomerPresenceService;
2026-06-18 16:12:53 +03:30
use App\Services\LivePriceCacheService;
2026-06-18 19:44:23 +03:30
use App\Services\PriceBroadcastService;
2026-06-18 16:12:53 +03:30
use App\Services\PriceStreamService;
2026-07-12 20:58:34 +03:30
use App\Services\ZaarboxPriceService;
2026-06-18 16:12:53 +03:30
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class PriceStreamController extends Controller
{
public function stream(Request $request): StreamedResponse
{
$service = app(PriceStreamService::class);
if (! $service->isEnabled()) {
abort(404);
}
2026-06-27 15:51:44 +03:30
$customer = auth('customer')->user();
$customerId = $customer ? (int) $customer->id : 0;
$customerOwnerType = $customer ? (int) $customer->type : 0;
return response()->stream(function () use ($service, $customerId, $customerOwnerType) {
2026-06-18 16:12:53 +03:30
@set_time_limit(0);
if (function_exists('apache_setenv')) {
@apache_setenv('no-gzip', '1');
}
@ini_set('zlib.output_compression', '0');
@ini_set('implicit_flush', '1');
while (ob_get_level() > 0) {
@ob_end_flush();
}
$send = static function (string $event, $data): void {
echo 'event: '.$event."\n";
echo 'data: '.json_encode($data, JSON_UNESCAPED_UNICODE)."\n\n";
@flush();
};
2026-07-22 17:17:23 +03:30
$presence = app(CustomerPresenceService::class);
if ($customerId > 0) {
$presence->touch($customerId, true);
}
2026-06-18 16:12:53 +03:30
$cache = app(LivePriceCacheService::class);
2026-08-26 21:47:09 +03:30
$config = app(\App\Services\ConfigService::class);
2026-08-27 11:53:29 +03:30
$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();
2026-08-16 22:00:23 +03:30
}
2026-08-27 11:53:29 +03:30
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);
2026-06-18 16:12:53 +03:30
}
2026-08-27 11:53:29 +03:30
$hoursSettings = $config->clientTradingHoursSettings();
if ($hoursSettings !== []) {
$send('settings', $hoursSettings);
}
2026-08-27 11:53:29 +03:30
$list = $broadcast->buildListPayload(false);
$lastSentListIdentity = null;
2026-06-18 19:44:23 +03:30
if (is_array($list) && $list !== []) {
$send('list', $list);
2026-08-27 11:53:29 +03:30
$lastSentListIdentity = PriceBroadcastService::listIdentity($list);
2026-06-18 19:44:23 +03:30
}
2026-08-16 22:00:23 +03:30
if ($customerId > 0) {
$send(
'special-pricing',
2026-08-27 11:53:29 +03:30
$specialPricing->clientPayloadForCustomer($customerId)
2026-08-16 22:00:23 +03:30
);
}
2026-06-27 15:51:44 +03:30
$customerLastId = 0;
if ($customerId > 0) {
$assets = app(AssetSummaryService::class)->buildForCustomer(
$customerId,
$customerOwnerType > 0 ? $customerOwnerType : 2
);
if ($assets !== []) {
$send('assets', $assets);
}
2026-07-29 17:00:51 +03:30
$unread = app(\App\Services\AppNotificationUnreadService::class);
if ($unread->featureEnabled()) {
$send('unread-count', $unread->payloadForCustomer($customerId));
}
2026-06-27 15:51:44 +03:30
$customerLastId = $service->currentCustomerSeq($customerId);
}
2026-06-18 19:32:28 +03:30
$lastId = $service->currentSeq();
2026-06-18 16:12:53 +03:30
$heartbeatTicks = 0;
2026-06-18 20:00:25 +03:30
echo "retry: 5000\n\n";
@flush();
2026-06-18 16:12:53 +03:30
while (! connection_aborted()) {
2026-06-18 19:32:28 +03:30
foreach ($service->consumeSince($lastId) as $event) {
2026-08-27 11:53:29 +03:30
$eventName = (string) $event['event'];
$eventData = $event['data'];
$send($eventName, $eventData);
2026-06-18 19:32:28 +03:30
$lastId = (int) $event['id'];
2026-08-27 11:53:29 +03:30
if ($eventName === 'last' && is_array($eventData)) {
$lastSentIdentity = PriceBroadcastService::lastPriceIdentity($eventData);
}
if ($eventName === 'list' && is_array($eventData)) {
$lastSentListIdentity = PriceBroadcastService::listIdentity($eventData);
}
2026-06-18 16:12:53 +03:30
}
2026-06-27 15:51:44 +03:30
if ($customerId > 0) {
foreach ($service->consumeCustomerSince($customerId, $customerLastId) as $event) {
$send((string) $event['event'], $event['data']);
$customerLastId = (int) $event['id'];
}
}
2026-06-18 16:12:53 +03:30
$heartbeatTicks++;
2026-08-27 11:53:29 +03:30
// هر ۱ ثانیه: قیمت را مستقیم از کش/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;
}
}
}
2026-06-18 20:00:25 +03:30
// هر ۵ ثانیه — LiteSpeed/پروکسی اتصال بیکار را نمی‌بندد
if ($heartbeatTicks >= 10) {
$send('ping', ['ts' => time()]);
2026-07-22 17:17:23 +03:30
if ($customerId > 0) {
$presence->touch($customerId);
}
2026-08-27 11:53:29 +03:30
$pulledList = $broadcast->buildListPayload(true);
if (is_array($pulledList) && $pulledList !== []) {
$listIdentity = PriceBroadcastService::listIdentity($pulledList);
if ($lastSentListIdentity !== $listIdentity) {
$send('list', $pulledList);
$lastSentListIdentity = $listIdentity;
}
}
2026-06-18 16:12:53 +03:30
$heartbeatTicks = 0;
}
usleep(500000);
}
}, 200, [
'Content-Type' => 'text/event-stream; charset=UTF-8',
'Cache-Control' => 'no-cache, no-store, must-revalidate',
'Connection' => 'keep-alive',
'X-Accel-Buffering' => 'no',
]);
}
}