Files
Socket/app/services/process/CountdownService.php
T
testadmin 8edc071997 fix: 倒计时卡住根因修复 — Redis TTL缓冲+前端watchdog兜底
根因:CountdownService用while(sleep(1))阻塞循环推送倒计时,
Redis key TTL=wait_time,3台同时运行时worker争用导致循环慢于
Redis过期时间,key先过期→循环中断→倒计时冻结在当前秒数。

修复:
1. Redis TTL加10秒缓冲(waitTime+10),防止key提前过期
2. 前端加watchdog兜底:服务端2秒没推送就本地继续倒计时
2026-05-19 14:25:48 +08:00

75 lines
2.5 KiB
PHP

<?php
namespace app\services\process;
use freedom\utils\RedisUtil;
use freedom\utils\SocketSession;
use think\facade\Cache;
/**
* TODO 倒计时服务
* Class CountdownService
* @package app\services\process
*/
class CountdownService
{
/**
* TODO 倒计时发送
* @param int $numberTabId
* @param array $tableInfo
* @param array $numberTabInfo
* @return void
*/
public static function countdown(int $numberTabId, array $tableInfo, array $numberTabInfo){
$ws = app('\think\swoole\WebSocket');
//处理倒计时
$waitTime = intval($tableInfo['wait_time']);
//将倒计时用作是redis的过期时间
RedisUtil::save('countdown_'.$numberTabInfo['id'],$waitTime,$waitTime + 10);
while($waitTime >= 0){
$countdown = Cache::store('redis')->get('countdown_'.$numberTabInfo['id']);
if ($countdown){
$ws->to(SocketSession::HOUSE_NAME)->emit('startBetCountDown',['status' => true, 'table_id' => $tableInfo['id'], 'count_down' => $waitTime]);
$waitTime--;
sleep(1);
if ($waitTime == 0){
//关闭倒计时
EndBetService::endBet($numberTabId,$tableInfo);
}
}else{
break;
}
}
}
/**
* TODO 倒计时发送(Rob)
* @param int $numberTabId
* @param array $tableInfo
* @param array $numberTabInfo
* @return void
*/
public static function countdownRob(int $numberTabId, array $tableInfo, array $numberTabInfo){
$ws = app('\think\swoole\WebSocket');
//处理倒计时
$waitTime = intval($tableInfo['rob_time']);
//将倒计时用作是redis的过期时间
RedisUtil::save('countdownRob_'.$numberTabInfo['id'],$waitTime,$waitTime);
while($waitTime >= 0){
$countdown = Cache::store('redis')->get('countdownRob_'.$numberTabInfo['id']);
if ($countdown){
$ws->to(SocketSession::HOUSE_NAME)->emit('startRobCountDown',['status' => true, 'table_id' => $tableInfo['id'], 'count_down' => $waitTime]);
$waitTime--;
sleep(1);
if ($waitTime == 0){
//关闭倒计时
EndRobService::endRob($numberTabId,$tableInfo);
}
}else{
break;
}
}
}
}