Files
lotteryLaravel/app/Services/PlayerRealtimeBroadcaster.php

49 lines
1.2 KiB
PHP
Raw Permalink 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\Services;
use App\Events\BalanceUpdateBroadcast;
/**
* 玩家私有频道实时广播。
*
* 对齐界面文档 §2.1balance.update频道 `player.{id}`)。
* 注意:玩家私有频道广播与大厅公共频道分开管理。
*/
final class PlayerRealtimeBroadcaster
{
/** `balance.update` —— 钱包余额变动 */
public function notifyBalanceUpdate(
int $playerId,
string $currencyCode,
int $balanceMinor,
int $changeMinor,
string $reason,
): void {
if (! $this->driverSupportsRealtime()) {
return;
}
broadcast(new BalanceUpdateBroadcast(
$playerId,
$currencyCode,
$balanceMinor,
$changeMinor,
$reason,
(int) floor(microtime(true) * 1000),
));
}
private function driverSupportsRealtime(): bool
{
$default = config('broadcasting.default');
if ($default === null || $default === 'null') {
return false;
}
$driver = config("broadcasting.connections.{$default}.driver") ?? $default;
return ! in_array($driver, ['null', 'log'], true);
}
}