added last seen
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Site;
|
|||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Services\AssetSummaryService;
|
use App\Services\AssetSummaryService;
|
||||||
|
use App\Services\CustomerPresenceService;
|
||||||
use App\Services\LivePriceCacheService;
|
use App\Services\LivePriceCacheService;
|
||||||
use App\Services\PriceBroadcastService;
|
use App\Services\PriceBroadcastService;
|
||||||
use App\Services\PriceStreamService;
|
use App\Services\PriceStreamService;
|
||||||
@@ -45,6 +46,11 @@ class PriceStreamController extends Controller
|
|||||||
@flush();
|
@flush();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$presence = app(CustomerPresenceService::class);
|
||||||
|
if ($customerId > 0) {
|
||||||
|
$presence->touch($customerId, true);
|
||||||
|
}
|
||||||
|
|
||||||
$cache = app(LivePriceCacheService::class);
|
$cache = app(LivePriceCacheService::class);
|
||||||
$last = $cache->get() ?? $cache->buildFromDatabase();
|
$last = $cache->get() ?? $cache->buildFromDatabase();
|
||||||
if (is_array($last) && $last !== []) {
|
if (is_array($last) && $last !== []) {
|
||||||
@@ -91,6 +97,9 @@ class PriceStreamController extends Controller
|
|||||||
// هر ۵ ثانیه — LiteSpeed/پروکسی اتصال بیکار را نمیبندد
|
// هر ۵ ثانیه — LiteSpeed/پروکسی اتصال بیکار را نمیبندد
|
||||||
if ($heartbeatTicks >= 10) {
|
if ($heartbeatTicks >= 10) {
|
||||||
$send('ping', ['ts' => time()]);
|
$send('ping', ['ts' => time()]);
|
||||||
|
if ($customerId > 0) {
|
||||||
|
$presence->touch($customerId);
|
||||||
|
}
|
||||||
$heartbeatTicks = 0;
|
$heartbeatTicks = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\Customer;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Presence از اتصال SSE قیمت: تا وقتی استریم مشتری باز است last_seen_at لمس میشود.
|
||||||
|
* ادمین با آستانهٔ زمانی (مثلاً ۲ دقیقه) آنلاین بودن را میفهمد.
|
||||||
|
*/
|
||||||
|
class CustomerPresenceService
|
||||||
|
{
|
||||||
|
public const TOUCH_THROTTLE_SECONDS = 30;
|
||||||
|
|
||||||
|
/** آستانهٔ آنلاین بودن در کلاینت ادمین هم باید همتراز باشد (~۲ دقیقه). */
|
||||||
|
public const ONLINE_THRESHOLD_SECONDS = 120;
|
||||||
|
|
||||||
|
public function touch(int $customerId, bool $force = false): void
|
||||||
|
{
|
||||||
|
if ($customerId <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key = 'customer_sse_presence:'.$customerId;
|
||||||
|
if (! $force && Cache::has($key)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::put($key, 1, self::TOUCH_THROTTLE_SECONDS);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Customer::query()
|
||||||
|
->where('id', $customerId)
|
||||||
|
->update(['last_seen_at' => now()]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// استریم قیمت نباید بهخاطر presence بمیرد
|
||||||
|
report($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user