Files
boursecu_server/app/Http/Controllers/Site/PriceStreamController.php
T
2026-08-16 22:00:23 +03:30

133 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\Site;
use App\Http\Controllers\Controller;
use App\Services\AssetSummaryService;
use App\Services\CustomerPresenceService;
use App\Services\LivePriceCacheService;
use App\Services\PriceBroadcastService;
use App\Services\PriceStreamService;
use App\Services\ZaarboxPriceService;
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);
}
$customer = auth('customer')->user();
$customerId = $customer ? (int) $customer->id : 0;
$customerOwnerType = $customer ? (int) $customer->type : 0;
return response()->stream(function () use ($service, $customerId, $customerOwnerType) {
@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();
};
$presence = app(CustomerPresenceService::class);
if ($customerId > 0) {
$presence->touch($customerId, true);
}
$cache = app(LivePriceCacheService::class);
$last = $cache->get() ?? $cache->buildFromDatabase();
if (is_array($last) && $last !== []) {
if ($customerId > 0) {
$last = app(\App\Services\CustomerSpecialPricingService::class)
->applyToLastPayload($last, $customerId);
}
$send('last', app(ZaarboxPriceService::class)->attachGoldOunceToPayload($last));
}
$list = app(PriceBroadcastService::class)->buildListPayload(false);
if (is_array($list) && $list !== []) {
$send('list', $list);
}
if ($customerId > 0) {
$send(
'special-pricing',
app(\App\Services\CustomerSpecialPricingService::class)->clientPayloadForCustomer($customerId)
);
}
$customerLastId = 0;
if ($customerId > 0) {
$assets = app(AssetSummaryService::class)->buildForCustomer(
$customerId,
$customerOwnerType > 0 ? $customerOwnerType : 2
);
if ($assets !== []) {
$send('assets', $assets);
}
$unread = app(\App\Services\AppNotificationUnreadService::class);
if ($unread->featureEnabled()) {
$send('unread-count', $unread->payloadForCustomer($customerId));
}
$customerLastId = $service->currentCustomerSeq($customerId);
}
$lastId = $service->currentSeq();
$heartbeatTicks = 0;
echo "retry: 5000\n\n";
@flush();
while (! connection_aborted()) {
foreach ($service->consumeSince($lastId) as $event) {
$send((string) $event['event'], $event['data']);
$lastId = (int) $event['id'];
}
if ($customerId > 0) {
foreach ($service->consumeCustomerSince($customerId, $customerLastId) as $event) {
$send((string) $event['event'], $event['data']);
$customerLastId = (int) $event['id'];
}
}
$heartbeatTicks++;
// هر ۵ ثانیه — LiteSpeed/پروکسی اتصال بیکار را نمی‌بندد
if ($heartbeatTicks >= 10) {
$send('ping', ['ts' => time()]);
if ($customerId > 0) {
$presence->touch($customerId);
}
$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',
]);
}
}