1.重构实时消息WebSocket连接
2.MySQL备份
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
namespace app\admin\controller\game;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\admin\PushChannelConfigHelper;
|
||||
use app\common\service\GameLiveService;
|
||||
use app\common\service\GameRecordService;
|
||||
use support\Response;
|
||||
@@ -39,21 +38,6 @@ class Live extends Backend
|
||||
return $this->success('', GameLiveService::buildSnapshot($recordId));
|
||||
}
|
||||
|
||||
public function pushConfig(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'url' => PushChannelConfigHelper::wsBaseUrl(),
|
||||
'app_key' => (string) config('plugin.webman.push.app.app_key'),
|
||||
'channel' => 'game-live',
|
||||
'event' => 'bet-updated',
|
||||
]);
|
||||
}
|
||||
|
||||
public function calculate(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
|
||||
49
app/admin/controller/test/GameCurrentStatus.php
Normal file
49
app/admin/controller/test/GameCurrentStatus.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\test;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\admin\WebSocketConfigHelper;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* WebSocket 测试:状态流(period.tick / period.opened)
|
||||
*/
|
||||
class GameCurrentStatus extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
public function wsConfig(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$subscribeTopics = [
|
||||
'period.tick',
|
||||
'period.opened',
|
||||
'period.locked',
|
||||
'period.payout',
|
||||
'bet.accepted',
|
||||
'wallet.changed',
|
||||
'auto.spin.progress',
|
||||
];
|
||||
|
||||
return $this->success('', [
|
||||
'name' => 'ws.period',
|
||||
'ws_url' => WebSocketConfigHelper::wsUrl(),
|
||||
'connect_tip' => '连接成功后会自动订阅下列主题;也可在「发送消息」中手动改订阅。未订阅时不会收到业务推送。',
|
||||
'subscribe_topics' => $subscribeTopics,
|
||||
'sample_messages' => [
|
||||
'{"action":"ping"}',
|
||||
'{"action":"subscribe","topics":["period.tick","period.opened"]}',
|
||||
'{"action":"subscribe","topics":["bet.accepted","wallet.changed","auto.spin.progress"]}',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\test;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\admin\PushChannelConfigHelper;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 推送测试:public-game-period(对局公共频道)
|
||||
*/
|
||||
class PushGamePeriod extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
public function pushConfig(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'url' => PushChannelConfigHelper::wsBaseUrl(),
|
||||
'app_key' => PushChannelConfigHelper::appKey(),
|
||||
'channel' => 'public-game-period',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\test;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\admin\PushChannelConfigHelper;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 推送测试:public-operation-notice(公告广播频道)
|
||||
*/
|
||||
class PushOperationNotice extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
public function pushConfig(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'url' => PushChannelConfigHelper::wsBaseUrl(),
|
||||
'app_key' => PushChannelConfigHelper::appKey(),
|
||||
'channel' => 'public-operation-notice',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\test;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\library\admin\PushChannelConfigHelper;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 推送测试:private-user-{uuid}(用户私有频道)
|
||||
*/
|
||||
class PushPrivateUser extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
public function pushConfig(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$uuid = trim((string) ($request->get('uuid') ?? $request->post('uuid') ?? ''));
|
||||
if ($uuid === '') {
|
||||
return $this->error(__('Parameter %s can not be empty', ['uuid']));
|
||||
}
|
||||
if (strlen($uuid) > 64 || !preg_match('/^[0-9a-zA-Z_-]+$/', $uuid)) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'url' => PushChannelConfigHelper::wsBaseUrl(),
|
||||
'app_key' => PushChannelConfigHelper::appKey(),
|
||||
'channel' => 'private-user-' . $uuid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ use app\common\model\DepositOrder;
|
||||
use app\common\model\GameConfig;
|
||||
use app\common\model\WithdrawOrder;
|
||||
use app\common\service\DepositOrderExpireService;
|
||||
use app\common\service\UserPushService;
|
||||
use support\Response;
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
@@ -332,7 +331,7 @@ class Finance extends MobileBase
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟第三方异步通知:验签后调用 DepositSettlement::settle 入账,并推送 wallet.changed。
|
||||
* 模拟第三方异步通知:验签后调用 DepositSettlement::settle 入账。
|
||||
*/
|
||||
public function depositMockNotify(Request $request): Response
|
||||
{
|
||||
@@ -373,19 +372,6 @@ class Finance extends MobileBase
|
||||
null,
|
||||
'channel_code=' . $pc
|
||||
);
|
||||
$uid = intval(strval($order->user_id));
|
||||
if ($uid > 0) {
|
||||
$coinAfter = is_string($result['balance_after'] ?? null) ? $result['balance_after'] : strval($result['balance_after'] ?? '0');
|
||||
$credit = is_string($result['credit'] ?? null) ? $result['credit'] : strval($result['credit'] ?? '0');
|
||||
UserPushService::publish($uid, UserPushService::EVT_WALLET_CHANGED, [
|
||||
'reason' => 'deposit',
|
||||
'ref_type' => 'deposit_order',
|
||||
'ref_id' => (string) $orderId,
|
||||
'order_no' => $orderNo,
|
||||
'delta' => $credit,
|
||||
'balance_after' => $coinAfter,
|
||||
]);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
return $this->mobileError(2000, $e->getMessage());
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use app\common\model\UserWalletRecord;
|
||||
use app\common\service\GameHotDataCoordinator;
|
||||
use app\common\service\GameHotDataRedis;
|
||||
use app\common\service\GameRecordService;
|
||||
use app\common\service\UserPushService;
|
||||
use app\common\service\GameWebSocketEventBus;
|
||||
use support\think\Db;
|
||||
use Webman\Http\Request;
|
||||
use support\Response;
|
||||
@@ -139,12 +139,27 @@ class Game extends MobileBase
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交下注:入参极简——period_no + numbers + bet_amount(整笔总金额) + idempotency_key。
|
||||
*
|
||||
* 下注判定:开奖号码 ∈ pick_numbers 即算中奖,赔付按整笔 total_amount × odds 计算
|
||||
* (派彩 = 压注总额 × 连胜奖励表 odds_factor;streak_at_bet 为下注时快照)。
|
||||
* 与前端文档对齐:/api/game/current_status
|
||||
*/
|
||||
public function currentStatus(Request $request): Response
|
||||
{
|
||||
return $this->periodCurrent($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 兼容旧路由:/api/game/betPlace
|
||||
* 新语义与 place_bet 一致:bet_amount 作为“单注金额”。
|
||||
*/
|
||||
public function betPlace(Request $request): Response
|
||||
{
|
||||
return $this->placeBet($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交下注:入参为 period_no + numbers + single_bet_amount + idempotency_key。
|
||||
* 兼容前端传参 bet_amount(作为 single_bet_amount 同义字段)。
|
||||
*/
|
||||
public function placeBet(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeMobile($request);
|
||||
if ($response !== null) {
|
||||
@@ -152,16 +167,14 @@ class Game extends MobileBase
|
||||
}
|
||||
$periodNo = trim((string) $request->post('period_no', ''));
|
||||
$numbersRaw = $request->post('numbers', '');
|
||||
$betAmount = trim((string) $request->post('bet_amount', ''));
|
||||
$singleBetAmount = trim((string) ($request->post('single_bet_amount', $request->post('bet_amount', ''))));
|
||||
$idempotencyKey = trim((string) $request->post('idempotency_key', ''));
|
||||
if ($periodNo === '' || $betAmount === '' || $idempotencyKey === '') {
|
||||
if ($periodNo === '' || $singleBetAmount === '' || $idempotencyKey === '') {
|
||||
return $this->mobileError(1001, 'Missing parameters');
|
||||
}
|
||||
if (!is_numeric($betAmount) || bccomp($betAmount, '0', 2) <= 0) {
|
||||
if (!is_numeric($singleBetAmount) || bccomp($singleBetAmount, '0', 2) <= 0) {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
}
|
||||
$totalAmount = bcadd($betAmount, '0', 2);
|
||||
|
||||
$numbers = $this->parseBetNumbersFromRequest($numbersRaw);
|
||||
if ($numbers === []) {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
@@ -170,6 +183,9 @@ class Game extends MobileBase
|
||||
if (count($numbers) > $maxSelect) {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
}
|
||||
$singleAmount = bcadd($singleBetAmount, '0', 2);
|
||||
$numberCount = (string) count($numbers);
|
||||
$totalAmount = bcmul($singleAmount, $numberCount, 2);
|
||||
|
||||
if (!GameRecordService::isLiveRuntimeEnabled()) {
|
||||
return $this->mobileError(3001, 'Game is paused');
|
||||
@@ -273,19 +289,28 @@ class Game extends MobileBase
|
||||
}
|
||||
|
||||
GameHotDataCoordinator::afterUserCommitted($userId);
|
||||
UserPushService::publish($userId, UserPushService::EVT_BET_ACCEPTED, [
|
||||
'order_no' => $orderNo,
|
||||
'period_no' => (string) $period->period_no,
|
||||
'status' => 'accepted',
|
||||
'balance_after' => $after,
|
||||
'total_amount' => $totalAmount,
|
||||
'current_streak' => $streakAtBet,
|
||||
GameWebSocketEventBus::publish('bet.accepted', [
|
||||
'user_id' => $userId,
|
||||
'period_no' => $period->period_no,
|
||||
'numbers' => $numbers,
|
||||
'single_bet_amount' => $singleAmount,
|
||||
'numbers_count' => count($numbers),
|
||||
'total_amount' => $totalAmount,
|
||||
'balance_after' => $after,
|
||||
'accepted_at' => time(),
|
||||
]);
|
||||
GameWebSocketEventBus::publish('wallet.changed', [
|
||||
'user_id' => $userId,
|
||||
'balance_after' => $after,
|
||||
'biz_type' => 'bet',
|
||||
'changed_at' => time(),
|
||||
]);
|
||||
|
||||
return $this->mobileSuccess([
|
||||
'order_no' => $orderNo,
|
||||
'period_no' => $period->period_no,
|
||||
'status' => 'accepted',
|
||||
'single_bet_amount' => $singleAmount,
|
||||
'numbers_count' => count($numbers),
|
||||
'locked_balance' => '0.00',
|
||||
'balance_after' => $after,
|
||||
'current_streak' => $streakAtBet,
|
||||
@@ -295,6 +320,61 @@ class Game extends MobileBase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动托管(无推送模式):先落托管配置,实际执行仍由客户端轮询 current_status 驱动。
|
||||
*/
|
||||
public function autoSpin(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeMobile($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$action = trim((string) $request->post('action', 'start'));
|
||||
if ($action === 'stop') {
|
||||
return $this->mobileSuccess([
|
||||
'status' => 'stopped',
|
||||
'auto_mode' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$periodNo = trim((string) $request->post('period_no', ''));
|
||||
$numbersRaw = $request->post('numbers', '');
|
||||
$singleBetAmount = trim((string) ($request->post('single_bet_amount', $request->post('bet_amount', ''))));
|
||||
$rounds = $this->intValue($request->post('rounds', 1));
|
||||
if ($periodNo === '' || $singleBetAmount === '' || $rounds < 1) {
|
||||
return $this->mobileError(1001, 'Missing parameters');
|
||||
}
|
||||
if (!is_numeric($singleBetAmount) || bccomp($singleBetAmount, '0', 2) <= 0) {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
}
|
||||
$numbers = $this->parseBetNumbersFromRequest($numbersRaw);
|
||||
if ($numbers === []) {
|
||||
return $this->mobileError(1003, 'Invalid parameter value');
|
||||
}
|
||||
$userIdValue = filter_var($this->auth->id ?? null, FILTER_VALIDATE_INT);
|
||||
if ($userIdValue === false) {
|
||||
$userIdValue = 0;
|
||||
}
|
||||
GameWebSocketEventBus::publish('auto.spin.progress', [
|
||||
'user_id' => $userIdValue,
|
||||
'period_no' => $periodNo,
|
||||
'rounds' => $rounds,
|
||||
'remaining_rounds' => $rounds,
|
||||
'completed_rounds' => 0,
|
||||
'status' => 'scheduled',
|
||||
'server_time' => time(),
|
||||
]);
|
||||
return $this->mobileSuccess([
|
||||
'status' => 'scheduled',
|
||||
'auto_mode' => true,
|
||||
'period_no' => $periodNo,
|
||||
'numbers' => $numbers,
|
||||
'single_bet_amount' => bcadd($singleBetAmount, '0', 2),
|
||||
'rounds' => $rounds,
|
||||
'remaining_rounds' => $rounds,
|
||||
]);
|
||||
}
|
||||
|
||||
public function betMyOrders(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeMobile($request);
|
||||
|
||||
@@ -81,10 +81,10 @@ return [
|
||||
'连胜奖励' => 'Win streak rewards',
|
||||
'连胜降低档位' => 'Streak reduction tiers',
|
||||
'钱包加减点' => 'Wallet adjust',
|
||||
'测试频道监听' => 'Test channel monitoring',
|
||||
'推送-对局公共频道' => 'Push: public game period',
|
||||
'推送-公告广播频道' => 'Push: operation notices',
|
||||
'推送-用户私有频道' => 'Push: user private',
|
||||
'测试频道监听' => 'WebSocket channel test',
|
||||
'接口-当前状态' => 'API: current status',
|
||||
'接口-下注' => 'API: place bet',
|
||||
'接口-自动托管' => 'API: auto spin',
|
||||
'渠道管理' => 'Channel management',
|
||||
'管理员提现记录' => 'Admin withdraw records',
|
||||
'管理员钱包' => 'Admin wallets',
|
||||
@@ -100,7 +100,7 @@ return [
|
||||
// 部分按钮 title 为动作名(game/live 等迁移写入)
|
||||
'index' => 'View',
|
||||
'snapshot' => 'Snapshot',
|
||||
'pushConfig' => 'Push config',
|
||||
'wsConfig' => 'WebSocket config',
|
||||
'recordSettings' => 'Period settings',
|
||||
'createNextManual' => 'Create next period manually',
|
||||
'periodSettings' => 'Period settings',
|
||||
|
||||
@@ -47,7 +47,7 @@ return [
|
||||
'approve' => '审核通过',
|
||||
'reject' => '审核驳回',
|
||||
'snapshot' => '快照',
|
||||
'pushConfig' => '推送配置',
|
||||
'wsConfig' => '连接配置',
|
||||
'recordSettings' => '期次设置',
|
||||
'createNextManual' => '手动创建下一期',
|
||||
'periodSettings' => '期号设置',
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\admin;
|
||||
|
||||
/**
|
||||
* 后台推送测试页:读取 webman/push 配置(与 game/Live::pushConfig 口径一致)
|
||||
*/
|
||||
final class PushChannelConfigHelper
|
||||
{
|
||||
public static function wsBaseUrl(): string
|
||||
{
|
||||
$client = trim((string) config('plugin.webman.push.app.websocket_client'));
|
||||
if ($client !== '') {
|
||||
return self::normalizeClientWsBase($client);
|
||||
}
|
||||
|
||||
$ws = (string) config('plugin.webman.push.app.websocket');
|
||||
$ws = str_replace('websocket://', 'ws://', $ws);
|
||||
|
||||
return str_replace('0.0.0.0', '127.0.0.1', $ws);
|
||||
}
|
||||
|
||||
private static function normalizeClientWsBase(string $url): string
|
||||
{
|
||||
$url = rtrim(trim($url), '/');
|
||||
if (str_starts_with($url, 'websocket://')) {
|
||||
return str_replace('websocket://', 'ws://', $url);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public static function appKey(): string
|
||||
{
|
||||
return (string) config('plugin.webman.push.app.app_key');
|
||||
}
|
||||
}
|
||||
18
app/common/library/admin/WebSocketConfigHelper.php
Normal file
18
app/common/library/admin/WebSocketConfigHelper.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\admin;
|
||||
|
||||
final class WebSocketConfigHelper
|
||||
{
|
||||
public static function wsUrl(): string
|
||||
{
|
||||
$url = trim((string) env('H5_WEBSOCKET_URL', ''));
|
||||
if ($url !== '') {
|
||||
return $url;
|
||||
}
|
||||
return 'ws://127.0.0.1:3131';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\finance;
|
||||
|
||||
use app\common\service\GameWebSocketEventBus;
|
||||
use RuntimeException;
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
@@ -183,6 +184,14 @@ final class DepositSettlement
|
||||
throw new RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
GameWebSocketEventBus::publish('wallet.changed', [
|
||||
'user_id' => $userId,
|
||||
'balance_after' => $balanceAfter,
|
||||
'biz_type' => 'deposit',
|
||||
'order_no' => $orderNo,
|
||||
'changed_at' => $now,
|
||||
]);
|
||||
|
||||
return [
|
||||
'order_id' => $orderId,
|
||||
'order_no' => $orderNo,
|
||||
|
||||
@@ -134,33 +134,6 @@ final class GameBetSettleService
|
||||
GameHotDataCoordinator::afterUserCommitted($userId);
|
||||
}
|
||||
|
||||
foreach ($aggregateByUser as $userId => $agg) {
|
||||
$hitOrderCount = 0;
|
||||
foreach ($agg['orders'] as $o) {
|
||||
if (($o['hit'] ?? false) === true) {
|
||||
$hitOrderCount++;
|
||||
}
|
||||
}
|
||||
UserPushService::publish((int) $userId, UserPushService::EVT_BET_SETTLED, [
|
||||
'period_no' => $agg['period_no'],
|
||||
'result_number' => $resultNumber,
|
||||
'total_win_amount' => $agg['total_win'],
|
||||
'order_count' => count($agg['orders']),
|
||||
'hit_order_count' => $hitOrderCount,
|
||||
'balance_after' => $agg['balance_after'],
|
||||
]);
|
||||
|
||||
if (bccomp($agg['total_win'], '0', 2) > 0) {
|
||||
UserPushService::publish((int) $userId, UserPushService::EVT_WALLET_CHANGED, [
|
||||
'reason' => 'payout',
|
||||
'ref_type' => 'game_period',
|
||||
'ref_id' => (string) $recordId,
|
||||
'delta' => $agg['total_win'],
|
||||
'balance_after' => $agg['balance_after'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$jackpotHits = [];
|
||||
foreach ($jackpotNotify as $uid => $_) {
|
||||
if (!isset($aggregateByUser[$uid])) {
|
||||
@@ -210,9 +183,8 @@ final class GameBetSettleService
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
$out = self::settleBetsForDraw($rid, $rn);
|
||||
self::settleBetsForDraw($rid, $rn);
|
||||
Db::commit();
|
||||
JackpotPushService::publishHits($out['jackpot_hits'] ?? []);
|
||||
$count++;
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
@@ -322,6 +294,13 @@ final class GameBetSettleService
|
||||
'update_time' => $now,
|
||||
]);
|
||||
GameHotDataCoordinator::afterUserCommitted($userId);
|
||||
GameWebSocketEventBus::publish('wallet.changed', [
|
||||
'user_id' => $userId,
|
||||
'balance_after' => $after,
|
||||
'biz_type' => 'payout',
|
||||
'ref_id' => $betId,
|
||||
'changed_at' => $now,
|
||||
]);
|
||||
|
||||
return $after;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ use app\common\service\GameHotDataCoordinator;
|
||||
use app\common\service\GameHotDataLock;
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
use Webman\Push\Api;
|
||||
|
||||
final class GameLiveService
|
||||
{
|
||||
@@ -384,8 +383,6 @@ final class GameLiveService
|
||||
GameRecordStatService::refreshForRecordId($rid);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
JackpotPushService::publishHits($settleOut['jackpot_hits'] ?? []);
|
||||
|
||||
self::publishPublicPeriodOpened((string) $record['period_no'], $finalNumber, $now);
|
||||
self::publishPublicPeriodPayout((string) $record['period_no'], $finalNumber, $payoutUntil);
|
||||
self::publishSnapshot(null);
|
||||
@@ -611,72 +608,60 @@ final class GameLiveService
|
||||
|
||||
public static function publishSnapshot(?int $recordId = null): void
|
||||
{
|
||||
try {
|
||||
$payload = self::buildSnapshot($recordId);
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL, self::EVENT, $payload);
|
||||
self::publishPublicPeriodTick($payload, $api);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
private static function createPushApi(): Api
|
||||
{
|
||||
return new Api(
|
||||
str_replace('0.0.0.0', '127.0.0.1', (string) config('plugin.webman.push.app.api')),
|
||||
(string) config('plugin.webman.push.app.app_key'),
|
||||
(string) config('plugin.webman.push.app.app_secret')
|
||||
);
|
||||
$snapshot = self::buildSnapshot($recordId);
|
||||
self::publishPublicPeriodTick($snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动端公共频道:每秒心跳,含期号、倒计时、阶段(对齐 lobbyInit/periodCurrent 语义)
|
||||
*/
|
||||
private static function publishPublicPeriodTick(array $snapshot, Api $api): void
|
||||
private static function publishPublicPeriodTick(array $snapshot): void
|
||||
{
|
||||
$record = $snapshot['record'] ?? null;
|
||||
$serverTime = (int) ($snapshot['server_time'] ?? time());
|
||||
$remaining = (int) ($snapshot['remaining_seconds'] ?? 0);
|
||||
$betCloseIn = (int) ($snapshot['bet_remaining_seconds'] ?? 0);
|
||||
$payoutRem = (int) ($snapshot['payout_remaining_seconds'] ?? 0);
|
||||
$isPayout = !empty($snapshot['is_payout_phase']);
|
||||
$periodNo = '';
|
||||
$dbStatus = 0;
|
||||
$periodId = 0;
|
||||
$status = 'finished';
|
||||
$resultNumber = null;
|
||||
if (is_array($record)) {
|
||||
$periodNo = (string) ($record['period_no'] ?? '');
|
||||
$dbStatus = (int) ($record['status'] ?? 0);
|
||||
$rn = $record['result_number'] ?? null;
|
||||
$resultNumber = is_numeric((string) $rn) ? (int) $rn : null;
|
||||
}
|
||||
if ($record === null || $periodNo === '') {
|
||||
$status = 'idle';
|
||||
} else {
|
||||
$status = self::mapPublicPeriodStatus($dbStatus, $betCloseIn);
|
||||
}
|
||||
$payload = [
|
||||
'server_time' => $serverTime,
|
||||
'period_no' => $periodNo,
|
||||
'status' => $status,
|
||||
'countdown' => $remaining,
|
||||
'bet_close_in'=> $betCloseIn,
|
||||
'payout_remaining_seconds' => $payoutRem,
|
||||
'is_payout_phase' => $isPayout,
|
||||
'payout_message' => $isPayout ? '派彩中,请稍候' : '',
|
||||
];
|
||||
if ($periodNo !== '' && $record !== null) {
|
||||
$start = (int) ($record['period_start_at'] ?? 0);
|
||||
$betSeconds = (int) ($snapshot['bet_seconds'] ?? 20);
|
||||
$periodSeconds = (int) ($snapshot['period_seconds'] ?? 30);
|
||||
if ($start > 0) {
|
||||
$payload['lock_at'] = $start + $betSeconds;
|
||||
$payload['open_at'] = $start + $periodSeconds;
|
||||
$periodNoRaw = $record['period_no'] ?? '';
|
||||
if (is_string($periodNoRaw)) {
|
||||
$periodNo = $periodNoRaw;
|
||||
}
|
||||
$periodIdRaw = $record['id'] ?? 0;
|
||||
$periodIdParsed = filter_var($periodIdRaw, FILTER_VALIDATE_INT);
|
||||
if ($periodIdParsed !== false && $periodIdParsed > 0) {
|
||||
$periodId = $periodIdParsed;
|
||||
}
|
||||
$dbStatusRaw = $record['status'] ?? 4;
|
||||
$dbStatus = filter_var($dbStatusRaw, FILTER_VALIDATE_INT);
|
||||
if ($dbStatus === false) {
|
||||
$dbStatus = 4;
|
||||
}
|
||||
$betRemainingRaw = $snapshot['bet_remaining_seconds'] ?? 0;
|
||||
$betRemaining = filter_var($betRemainingRaw, FILTER_VALIDATE_INT);
|
||||
if ($betRemaining === false || $betRemaining < 0) {
|
||||
$betRemaining = 0;
|
||||
}
|
||||
$status = self::mapPublicPeriodStatus($dbStatus, $betRemaining);
|
||||
$resultRaw = $record['result_number'] ?? null;
|
||||
$resultParsed = filter_var($resultRaw, FILTER_VALIDATE_INT);
|
||||
if ($resultParsed !== false && $resultParsed > 0) {
|
||||
$resultNumber = $resultParsed;
|
||||
}
|
||||
}
|
||||
if ($resultNumber !== null) {
|
||||
$payload['result_number'] = $resultNumber;
|
||||
}
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_TICK, $payload);
|
||||
|
||||
$payload = [
|
||||
'period_id' => $periodId,
|
||||
'period_no' => $periodNo,
|
||||
'status' => $status,
|
||||
'countdown' => max(0, self::safeInt($snapshot['remaining_seconds'] ?? 0)),
|
||||
'bet_close_in' => max(0, self::safeInt($snapshot['bet_remaining_seconds'] ?? 0)),
|
||||
'result_number' => $resultNumber,
|
||||
'runtime_enabled' => !empty($snapshot['runtime_enabled']),
|
||||
'server_time' => time(),
|
||||
];
|
||||
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_TICK, $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -684,32 +669,26 @@ final class GameLiveService
|
||||
*/
|
||||
private static function publishPublicPeriodLocked(array $record): void
|
||||
{
|
||||
try {
|
||||
$start = (int) ($record['period_start_at'] ?? 0);
|
||||
$betSeconds = self::getConfigInt(self::KEY_BET_SECONDS, 20);
|
||||
$periodNo = (string) ($record['period_no'] ?? '');
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'lock_at' => $start > 0 ? $start + $betSeconds : time(),
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_LOCKED, $payload);
|
||||
} catch (Throwable) {
|
||||
$periodNo = is_string($record['period_no'] ?? null) ? $record['period_no'] : '';
|
||||
$periodId = filter_var($record['id'] ?? 0, FILTER_VALIDATE_INT);
|
||||
if ($periodId === false) {
|
||||
$periodId = 0;
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_LOCKED, [
|
||||
'period_id' => $periodId,
|
||||
'period_no' => $periodNo,
|
||||
'status' => 'locked',
|
||||
'server_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function publishPublicPeriodOpened(string $periodNo, int $resultNumber, int $openTime): void
|
||||
{
|
||||
try {
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'open_time' => $openTime,
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_OPENED, $payload);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_OPENED, [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'open_time' => $openTime,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -717,17 +696,12 @@ final class GameLiveService
|
||||
*/
|
||||
private static function publishPublicPeriodPayout(string $periodNo, int $resultNumber, int $payoutUntil): void
|
||||
{
|
||||
try {
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'payout_until' => $payoutUntil,
|
||||
'message' => '派彩中,请稍候',
|
||||
];
|
||||
$api = self::createPushApi();
|
||||
$api->trigger(self::CHANNEL_PUBLIC_GAME_PERIOD, self::EVT_PERIOD_PAYOUT, $payload);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
GameWebSocketEventBus::publish(self::EVT_PERIOD_PAYOUT, [
|
||||
'period_no' => $periodNo,
|
||||
'result_number' => $resultNumber,
|
||||
'payout_until' => $payoutUntil,
|
||||
'server_time' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -911,4 +885,13 @@ final class GameLiveService
|
||||
$index = random_int(0, count($numbers) - 1);
|
||||
return $numbers[$index];
|
||||
}
|
||||
|
||||
private static function safeInt($value): int
|
||||
{
|
||||
$parsed = filter_var($value, FILTER_VALIDATE_INT);
|
||||
if ($parsed === false) {
|
||||
return 0;
|
||||
}
|
||||
return $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
93
app/common/service/GameWebSocketEventBus.php
Normal file
93
app/common/service/GameWebSocketEventBus.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use support\Redis;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 通过 Redis 列表在不同进程间投递 WebSocket 事件。
|
||||
*/
|
||||
final class GameWebSocketEventBus
|
||||
{
|
||||
private const KEY_QUEUE = 'dfw:v1:ws:event:queue';
|
||||
private const MAX_BATCH = 100;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function publish(string $topic, array $data): void
|
||||
{
|
||||
$topic = trim($topic);
|
||||
if ($topic === '') {
|
||||
return;
|
||||
}
|
||||
$payload = [
|
||||
'topic' => $topic,
|
||||
'event' => $topic,
|
||||
'data' => $data,
|
||||
'server_time' => time(),
|
||||
];
|
||||
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
if (!is_string($json) || $json === '') {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Redis::lPush(self::KEY_QUEUE, $json);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{topic:string,event:string,data:array<string,mixed>,server_time:int}>
|
||||
*/
|
||||
public static function popBatch(int $limit = self::MAX_BATCH): array
|
||||
{
|
||||
if ($limit <= 0) {
|
||||
return [];
|
||||
}
|
||||
if ($limit > self::MAX_BATCH) {
|
||||
$limit = self::MAX_BATCH;
|
||||
}
|
||||
|
||||
$out = [];
|
||||
try {
|
||||
for ($i = 0; $i < $limit; $i++) {
|
||||
$raw = Redis::rPop(self::KEY_QUEUE);
|
||||
if (!is_string($raw) || $raw === '') {
|
||||
break;
|
||||
}
|
||||
$decoded = json_decode($raw, true);
|
||||
if (!is_array($decoded)) {
|
||||
continue;
|
||||
}
|
||||
$topicRaw = $decoded['topic'] ?? '';
|
||||
$eventRaw = $decoded['event'] ?? '';
|
||||
$dataRaw = $decoded['data'] ?? [];
|
||||
$serverTimeRaw = $decoded['server_time'] ?? time();
|
||||
if (!is_string($topicRaw) || trim($topicRaw) === '') {
|
||||
continue;
|
||||
}
|
||||
$topic = trim($topicRaw);
|
||||
$event = is_string($eventRaw) && trim($eventRaw) !== '' ? trim($eventRaw) : $topic;
|
||||
$data = is_array($dataRaw) ? $dataRaw : [];
|
||||
$serverTime = filter_var($serverTimeRaw, FILTER_VALIDATE_INT);
|
||||
if ($serverTime === false || $serverTime <= 0) {
|
||||
$serverTime = time();
|
||||
}
|
||||
$out[] = [
|
||||
'topic' => $topic,
|
||||
'event' => $event,
|
||||
'data' => $data,
|
||||
'server_time' => $serverTime,
|
||||
];
|
||||
}
|
||||
} catch (Throwable) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use Throwable;
|
||||
use Webman\Push\Api;
|
||||
|
||||
/**
|
||||
* 大奖派彩:玩家私有频道 + 公共频道(对局频道 + 公告频道,便于大厅与公告测试页均能收到)
|
||||
*/
|
||||
final class JackpotPushService
|
||||
{
|
||||
private const CHANNEL_GAME_PERIOD = 'public-game-period';
|
||||
|
||||
private const CHANNEL_OPERATION_NOTICE = 'public-operation-notice';
|
||||
|
||||
private const EVT_JACKPOT_HIT = 'jackpot.hit';
|
||||
|
||||
/**
|
||||
* @param list<array{user_id: int, period_no: string, total_win: string, result_number: int}> $hits
|
||||
*/
|
||||
public static function publishHits(array $hits): void
|
||||
{
|
||||
foreach ($hits as $h) {
|
||||
$uid = (int) ($h['user_id'] ?? 0);
|
||||
if ($uid <= 0) {
|
||||
continue;
|
||||
}
|
||||
$periodNo = (string) ($h['period_no'] ?? '');
|
||||
$totalWin = (string) ($h['total_win'] ?? '0');
|
||||
$rn = (int) ($h['result_number'] ?? 0);
|
||||
UserPushService::publish($uid, UserPushService::EVT_JACKPOT_HIT, [
|
||||
'period_no' => $periodNo,
|
||||
'total_win_amount' => $totalWin,
|
||||
'result_number' => $rn,
|
||||
'is_jackpot' => true,
|
||||
]);
|
||||
self::publishPublicChannels($periodNo, $uid, $totalWin, $rn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $payload
|
||||
*/
|
||||
private static function triggerChannel(Api $api, string $channel, array $payload): void
|
||||
{
|
||||
$api->trigger($channel, self::EVT_JACKPOT_HIT, $payload);
|
||||
}
|
||||
|
||||
private static function publishPublicChannels(string $periodNo, int $userId, string $totalWin, int $resultNumber): void
|
||||
{
|
||||
try {
|
||||
$api = new Api(
|
||||
str_replace('0.0.0.0', '127.0.0.1', (string) config('plugin.webman.push.app.api')),
|
||||
(string) config('plugin.webman.push.app.app_key'),
|
||||
(string) config('plugin.webman.push.app.app_secret')
|
||||
);
|
||||
$payload = [
|
||||
'period_no' => $periodNo,
|
||||
'user_id' => $userId,
|
||||
'total_win_amount' => $totalWin,
|
||||
'result_number' => $resultNumber,
|
||||
'message' => '恭喜玩家命中大奖派彩',
|
||||
];
|
||||
self::triggerChannel($api, self::CHANNEL_GAME_PERIOD, $payload);
|
||||
self::triggerChannel($api, self::CHANNEL_OPERATION_NOTICE, $payload);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use support\think\Db;
|
||||
use Throwable;
|
||||
use Webman\Push\Api;
|
||||
|
||||
/**
|
||||
* 用户私有频道推送:private-user-{uuid}(与移动端接口设计草案 7.1 一致)
|
||||
*/
|
||||
final class UserPushService
|
||||
{
|
||||
public const EVT_BET_ACCEPTED = 'bet.accepted';
|
||||
|
||||
/** 单注开奖结果(含未中奖 win_amount=0) */
|
||||
public const EVT_BET_SETTLED = 'bet.settled';
|
||||
|
||||
public const EVT_WALLET_CHANGED = 'wallet.changed';
|
||||
|
||||
/** 命中配置为「大奖」的连胜档派彩(私有频道) */
|
||||
public const EVT_JACKPOT_HIT = 'jackpot.hit';
|
||||
|
||||
private static function channelName(string $uuid): string
|
||||
{
|
||||
return 'private-user-' . $uuid;
|
||||
}
|
||||
|
||||
private static function createApi(): Api
|
||||
{
|
||||
return new Api(
|
||||
str_replace('0.0.0.0', '127.0.0.1', (string) config('plugin.webman.push.app.api')),
|
||||
(string) config('plugin.webman.push.app.app_key'),
|
||||
(string) config('plugin.webman.push.app.app_secret')
|
||||
);
|
||||
}
|
||||
|
||||
public static function uuidForUserId(int $userId): ?string
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return null;
|
||||
}
|
||||
$u = Db::name('user')->where('id', $userId)->value('uuid');
|
||||
|
||||
return is_string($u) && $u !== '' ? $u : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function publish(int $userId, string $event, array $data): void
|
||||
{
|
||||
$uuid = self::uuidForUserId($userId);
|
||||
if ($uuid === null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
self::createApi()->trigger(self::channelName($uuid), $event, $data);
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
151
app/process/GameWebSocketServer.php
Normal file
151
app/process/GameWebSocketServer.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\process;
|
||||
|
||||
use app\common\service\GameWebSocketEventBus;
|
||||
use Workerman\Connection\TcpConnection;
|
||||
use Workerman\Timer;
|
||||
|
||||
/**
|
||||
* 后台测试页 WebSocket 服务(仅用于连接联调)
|
||||
*/
|
||||
class GameWebSocketServer
|
||||
{
|
||||
/** @var array<int, TcpConnection> */
|
||||
private static array $connections = [];
|
||||
|
||||
private static bool $eventBusConsumerStarted = false;
|
||||
|
||||
/**
|
||||
* 从 Redis 队列拉取事件并推送给已订阅连接。
|
||||
* 部分环境下 WebSocket 进程的 onWorkerStart 可能未触发,因此在首帧握手处也会兜底启动一次(全局仅注册一个 Timer)。
|
||||
*/
|
||||
private static function ensureEventBusConsumer(): void
|
||||
{
|
||||
if (self::$eventBusConsumerStarted) {
|
||||
return;
|
||||
}
|
||||
self::$eventBusConsumerStarted = true;
|
||||
Timer::add(1, static function (): void {
|
||||
$events = GameWebSocketEventBus::popBatch();
|
||||
if ($events === []) {
|
||||
return;
|
||||
}
|
||||
foreach ($events as $event) {
|
||||
$topic = $event['topic'] ?? '';
|
||||
if (!is_string($topic) || $topic === '') {
|
||||
continue;
|
||||
}
|
||||
$eventName = $event['event'] ?? $topic;
|
||||
$data = $event['data'] ?? [];
|
||||
if (!is_array($data)) {
|
||||
$data = [];
|
||||
}
|
||||
$serverTime = $event['server_time'] ?? time();
|
||||
foreach (self::$connections as $connection) {
|
||||
$topics = $connection->topics ?? [];
|
||||
if (!is_array($topics) || !in_array($topic, $topics, true)) {
|
||||
continue;
|
||||
}
|
||||
$connection->send(json_encode([
|
||||
'event' => $eventName,
|
||||
'topic' => $topic,
|
||||
'data' => $data,
|
||||
'server_time' => $serverTime,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function onWorkerStart(): void
|
||||
{
|
||||
self::ensureEventBusConsumer();
|
||||
}
|
||||
|
||||
public function onConnect(TcpConnection $connection): void
|
||||
{
|
||||
$connection->topics = [];
|
||||
self::$connections[$connection->id] = $connection;
|
||||
}
|
||||
|
||||
public function onWebSocketConnect(TcpConnection $connection): void
|
||||
{
|
||||
self::ensureEventBusConsumer();
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ws.connected',
|
||||
'message' => 'WebSocket connected',
|
||||
'connection_id' => $connection->id,
|
||||
'server_time' => time(),
|
||||
'heartbeat_interval' => 30,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
public function onMessage(TcpConnection $connection, string $payload): void
|
||||
{
|
||||
$decoded = json_decode($payload, true);
|
||||
if (!is_array($decoded)) {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ws.error',
|
||||
'message' => 'Invalid JSON payload',
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
return;
|
||||
}
|
||||
|
||||
$action = $decoded['action'] ?? '';
|
||||
if ($action === 'ping') {
|
||||
$connection->send(json_encode([
|
||||
'event' => 'pong',
|
||||
'server_time' => date('Y-m-d H:i:s'),
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
return;
|
||||
}
|
||||
|
||||
if ($action === 'subscribe') {
|
||||
$topics = $decoded['topics'] ?? [];
|
||||
$sanitized = [];
|
||||
if (is_array($topics)) {
|
||||
foreach ($topics as $topic) {
|
||||
if (!is_string($topic)) {
|
||||
continue;
|
||||
}
|
||||
$value = trim($topic);
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
$sanitized[] = $value;
|
||||
}
|
||||
}
|
||||
$connection->topics = array_values(array_unique($sanitized));
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ws.subscribed',
|
||||
'topics' => $connection->topics,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
return;
|
||||
}
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ws.error',
|
||||
'message' => 'Unsupported action',
|
||||
'received_action' => $action,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
public function onError(TcpConnection $connection, int $code, string $msg): void
|
||||
{
|
||||
$connection->send(json_encode([
|
||||
'event' => 'ws.error',
|
||||
'message' => 'Server internal error',
|
||||
'code' => $code,
|
||||
'detail' => $msg,
|
||||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
public function onClose(TcpConnection $connection): void
|
||||
{
|
||||
$connection->topics = [];
|
||||
unset(self::$connections[$connection->id]);
|
||||
}
|
||||
}
|
||||
@@ -40,8 +40,7 @@
|
||||
"nelexa/zip": "^4.0.0",
|
||||
"voku/anti-xss": "^4.1",
|
||||
"topthink/think-validate": "^3.0",
|
||||
"ext-bcmath": "*",
|
||||
"webman/push": "^1.1"
|
||||
"ext-bcmath": "*"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-event": "For better performance. "
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
return [
|
||||
'enable' => true,
|
||||
'websocket' => 'websocket://0.0.0.0:3131',
|
||||
'api' => 'http://0.0.0.0:3232',
|
||||
/** 浏览器连接的 WebSocket 基址(不含 /app/)。生产/前后端分离必填,如 wss://api.example.com */
|
||||
'websocket_client' => env('PUSH_WEBSOCKET_CLIENT_URL', ''),
|
||||
'app_key' => '6d0af5971ad191f2dc8a500885cb79c7',
|
||||
'app_secret' => 'c457f0be89cd48d481b37f16c0a97f5f',
|
||||
'channel_hook' => 'http://127.0.0.1:7979/plugin/webman/push/hook',
|
||||
'auth' => '/plugin/webman/push/auth'
|
||||
];
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Webman\Push\Server;
|
||||
|
||||
return [
|
||||
'server' => [
|
||||
'handler' => Server::class,
|
||||
'listen' => config('plugin.webman.push.app.websocket'),
|
||||
'count' => 1, // 必须是1
|
||||
'reloadable' => false, // 执行reload不重启
|
||||
'constructor' => [
|
||||
'api_listen' => config('plugin.webman.push.app.api'),
|
||||
'app_info' => [
|
||||
config('plugin.webman.push.app.app_key') => [
|
||||
'channel_hook' => config('plugin.webman.push.app.channel_hook'),
|
||||
'app_secret' => config('plugin.webman.push.app.app_secret'),
|
||||
],
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of webman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
use support\Request;
|
||||
use Webman\Route;
|
||||
use Webman\Push\Api;
|
||||
|
||||
/**
|
||||
* 推送 js 客户端
|
||||
* - 生产环境 Nginx 常把 root 指到 public/:须存在 public/plugin/webman/push/push.js(与 vendor 同步)
|
||||
* - 若请求进入 Webman,则优先读 public,其次 vendor
|
||||
*/
|
||||
Route::get('/plugin/webman/push/push.js', function (Request $request) {
|
||||
$publicFile = public_path() . DIRECTORY_SEPARATOR . 'plugin' . DIRECTORY_SEPARATOR . 'webman' . DIRECTORY_SEPARATOR . 'push' . DIRECTORY_SEPARATOR . 'push.js';
|
||||
if (is_file($publicFile)) {
|
||||
return response()->file($publicFile);
|
||||
}
|
||||
$vendorFile = base_path() . '/vendor/webman/push/src/push.js';
|
||||
if (is_file($vendorFile)) {
|
||||
return response()->file($vendorFile);
|
||||
}
|
||||
|
||||
return response('push.js not found. Run: copy vendor/webman/push/src/push.js to public/plugin/webman/push/push.js', 404);
|
||||
});
|
||||
|
||||
/**
|
||||
* 私有频道鉴权,这里应该使用session辨别当前用户身份,然后确定该用户是否有权限监听channel_name
|
||||
*/
|
||||
Route::post(config('plugin.webman.push.app.auth'), function (Request $request) {
|
||||
$pusher = new Api(str_replace('0.0.0.0', '127.0.0.1', config('plugin.webman.push.app.api')), config('plugin.webman.push.app.app_key'), config('plugin.webman.push.app.app_secret'));
|
||||
$channel_name = $request->post('channel_name');
|
||||
$session = $request->session();
|
||||
// 这里应该通过session和channel_name判断当前用户是否有权限监听channel_name
|
||||
$has_authority = true;
|
||||
if ($has_authority) {
|
||||
return response($pusher->socketAuth($channel_name, $request->post('socket_id')));
|
||||
} else {
|
||||
return response('Forbidden', 403);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* 当频道上线以及下线时触发的回调
|
||||
* 频道上线:是指某个频道从没有连接在线到有连接在线的事件
|
||||
* 频道下线:是指某个频道的所有连接都断开触发的事件
|
||||
*/
|
||||
Route::post(parse_url(config('plugin.webman.push.app.channel_hook'), PHP_URL_PATH), function (Request $request) {
|
||||
|
||||
// 没有x-pusher-signature头视为伪造请求
|
||||
if (!$webhook_signature = $request->header('x-pusher-signature')) {
|
||||
return response('401 Not authenticated', 401);
|
||||
}
|
||||
|
||||
$body = $request->rawBody();
|
||||
|
||||
// 计算签名,$app_secret 是双方使用的密钥,是保密的,外部无从得知
|
||||
$expected_signature = hash_hmac('sha256', $body, config('plugin.webman.push.app.app_secret'), false);
|
||||
|
||||
// 安全校验,如果签名不一致可能是伪造的请求,返回401状态码
|
||||
if ($webhook_signature !== $expected_signature) {
|
||||
return response('401 Not authenticated', 401);
|
||||
}
|
||||
|
||||
// 这里存储这上线 下线的channel数据
|
||||
$payload = json_decode($body, true);
|
||||
|
||||
$channels_online = $channels_offline = [];
|
||||
|
||||
foreach ($payload['events'] as $event) {
|
||||
if ($event['name'] === 'channel_added') {
|
||||
$channels_online[] = $event['channel'];
|
||||
} else if ($event['name'] === 'channel_removed') {
|
||||
$channels_offline[] = $event['channel'];
|
||||
}
|
||||
}
|
||||
|
||||
// 业务根据需要处理上下线的channel,例如将在线状态写入数据库,通知其它channel等
|
||||
// 上线的所有channel
|
||||
echo 'online channels: ' . implode(',', $channels_online) . "\n";
|
||||
// 下线的所有channel
|
||||
echo 'offline channels: ' . implode(',', $channels_offline) . "\n";
|
||||
|
||||
return 'OK';
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -64,6 +64,13 @@ return [
|
||||
'count' => 1,
|
||||
'reloadable' => false,
|
||||
],
|
||||
// 后台测试页 WebSocket 连接服务(供 /admin/test/gameCurrentStatus 联调)
|
||||
'gameWebSocketServer' => [
|
||||
'handler' => app\process\GameWebSocketServer::class,
|
||||
'listen' => env('H5_WEBSOCKET_LISTEN', 'websocket://0.0.0.0:3131'),
|
||||
'count' => 1,
|
||||
'reloadable' => false,
|
||||
],
|
||||
|
||||
// File update detection and automatic reload
|
||||
'monitor' => [
|
||||
|
||||
@@ -131,6 +131,9 @@ Route::add(['GET', 'POST'], '/api/game/periodHistory', [\app\api\controller\Game
|
||||
Route::add(['GET', 'POST'], '/api/game/periodCurrent', [\app\api\controller\Game::class, 'periodCurrent']);
|
||||
Route::post('/api/game/betPlace', [\app\api\controller\Game::class, 'betPlace']);
|
||||
Route::add(['GET', 'POST'], '/api/game/betMyOrders', [\app\api\controller\Game::class, 'betMyOrders']);
|
||||
Route::add(['GET', 'POST'], '/api/game/currentStatus', [\app\api\controller\Game::class, 'currentStatus']);
|
||||
Route::post('/api/game/placeBet', [\app\api\controller\Game::class, 'placeBet']);
|
||||
Route::post('/api/game/autoSpin', [\app\api\controller\Game::class, 'autoSpin']);
|
||||
|
||||
Route::add(['GET', 'POST'], '/api/wallet/balanceSummary', [\app\api\controller\Wallet::class, 'balanceSummary']);
|
||||
Route::add(['GET', 'POST'], '/api/wallet/recordList', [\app\api\controller\Wallet::class, 'recordList']);
|
||||
|
||||
1272
database/sql/buildadmin-webman-game-number_20260424134854_backup.sql
Normal file
1272
database/sql/buildadmin-webman-game-number_20260424134854_backup.sql
Normal file
File diff suppressed because one or more lines are too long
@@ -264,7 +264,7 @@
|
||||
## 4. 下注与对局模块(game/bet)
|
||||
|
||||
### 4.1 获取当前期详情
|
||||
- **POST** `/api/game/periodCurrent`
|
||||
- **POST** `/api/game/currentStatus`(兼容旧路径 `/api/game/periodCurrent`)
|
||||
|
||||
返回参数:
|
||||
- `runtime_enabled`:bool(含义:同 `lobbyInit.runtime_enabled`)
|
||||
@@ -276,19 +276,22 @@
|
||||
- `result_number`:int/null(未开奖为 null,含义:开奖号码)
|
||||
|
||||
### 4.2 提交下注
|
||||
- **POST** `/api/game/betPlace`
|
||||
- 用途:单期手动下注;玩家只需选择**压注号码**与**本笔压注总金额**。开奖只出一个号码,若该号码 ∈ 所选号码集合即视为**中奖**,派彩按整笔 `bet_amount`(落库为 `total_amount`)× 赔率计算(赔率与连胜倍率见服务端实现)。
|
||||
- **POST** `/api/game/placeBet`(兼容旧路径 `/api/game/betPlace`)
|
||||
- 用途:单期手动下注;玩家传入**压注号码**与**单注金额 `single_bet_amount`**。服务端按 `single_bet_amount × numbers数量` 计算本笔总扣款(落库 `total_amount`),开奖只出一个号码,若该号码 ∈ 所选号码集合即视为中奖。
|
||||
|
||||
请求参数:
|
||||
- `period_no`:string(含义:下注目标期号)
|
||||
- `numbers`:string(含义:本次压注号码集合,**英文逗号分隔**,如 `1,8,16`;每个号码为 1–36 的整数,数量不超过 `pick_max_number_count`(同 `lobbyInit.bet_config`),重复号码会去重)
|
||||
- `bet_amount`:string(含义:**本笔整笔压注金额**,> 0;服务端按此金额从余额扣款并写入注单 `total_amount`,**不再**按「单号金额 × 号码个数」计算)
|
||||
- `single_bet_amount`:string(含义:**单注金额**,> 0)
|
||||
- `bet_amount`:string(兼容字段,含义同 `single_bet_amount`)
|
||||
- `idempotency_key`:string(必填,含义:防止重复下单)
|
||||
|
||||
返回参数:
|
||||
- `order_no`:string(含义:下注订单号)
|
||||
- `period_no`:string(含义:实际落单期号)
|
||||
- `status`:string(`accepted`/`rejected`,含义:受理结果)
|
||||
- `single_bet_amount`:string(含义:本次单注金额)
|
||||
- `numbers_count`:int(含义:本次号码数量)
|
||||
- `locked_balance`:string(可选,含义:冻结金额)
|
||||
- `balance_after`:string(含义:下单后余额)
|
||||
- `current_streak`:int(含义:下单后连胜快照)
|
||||
@@ -298,9 +301,22 @@
|
||||
- `3001`:游戏已暂停(`runtime_enabled=false`,后台「游戏实时对局」关闭运行开关或作废本局后未重新开启;与非法流程类错误同段)
|
||||
- `5000`:系统繁忙;或 **用户 Redis 互斥锁**未获取(与后台钱包/并发写同一用户串行,文案与后台一致:「该用户正在被其他管理员操作(钱包/并发保存),请稍后再试」);或 **`coin` 条件更新**未命中(并发下注/派彩/后台已改余额:「扣款失败:该用户余额已被其他请求修改(如下注、派彩或其他管理员已保存),请刷新后重试」)。
|
||||
|
||||
> 说明:一键重复上一注、自动托管开启/停止均由前端控制,客户端在相应时机调用 `/api/game/betPlace` 即可完成,不再提供独立接口。
|
||||
### 4.3 自动托管
|
||||
- **POST** `/api/game/autoSpin`
|
||||
|
||||
### 4.3 查询我的下注记录(最近1个月)
|
||||
请求参数:
|
||||
- `action`:string(`start`/`stop`)
|
||||
- `period_no`:string(`action=start` 时必填)
|
||||
- `numbers`:string(`action=start` 时必填,英文逗号分隔)
|
||||
- `single_bet_amount`:string(`action=start` 时必填,支持兼容字段 `bet_amount`)
|
||||
- `rounds`:int(`action=start` 时必填,>=1)
|
||||
|
||||
返回参数:
|
||||
- `status`:string(`scheduled`/`stopped`)
|
||||
- `auto_mode`:bool
|
||||
- `remaining_rounds`:int(仅 `start` 返回)
|
||||
|
||||
### 4.4 查询我的下注记录(最近1个月)
|
||||
- **POST** `/api/game/betMyOrders`
|
||||
|
||||
请求参数:
|
||||
@@ -655,114 +671,54 @@
|
||||
|
||||
---
|
||||
|
||||
## 7. 推送模块(webman/push)
|
||||
## 7. WebSocket(H5)与状态同步
|
||||
|
||||
> 用于移动端实时监听对局状态、开奖结果、余额变更与强公告事件。
|
||||
> 协议与客户端行为对齐 [Pusher Channels](https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol/)(webman/push 内置兼容客户端 `push.js`)。
|
||||
> 参考:[webman/push 官方文档](https://www.workerman.net/doc/webman/plugin/push.html)
|
||||
> 本版本已移除 webman/push 频道模式;H5 前端使用原生 WebSocket 直连,HTTP 轮询仅作为弱网兜底。
|
||||
|
||||
### 7.1 频道命名与职责(优化版)
|
||||
### 7.1 WebSocket 连接与消息
|
||||
|
||||
| 频道名 | 类型 | 订阅方 | 典型事件 |
|
||||
|--------|------|--------|----------|
|
||||
| `private-user-{user.uuid}` | 私有(`private-` 前缀) | 当前登录用户;`{user.uuid}` 与登录态/档案中的 **10 位 `uuid`** 一致 | `bet.accepted`、`wallet.changed`、`withdraw.review_required`、定向 `notice.popout` 等 |
|
||||
| `public-game-period` | 公共 | 所有在线客户端 | `period.tick`、`period.locked`、`period.opened` |
|
||||
| `public-operation-notice` | 公共 | 所有在线客户端 | 全站/渠道级 `notice.popout`(与私有公告二选一或并存,由实现约定) |
|
||||
- **连接地址**:由服务端配置下发(后台测试页读取 `H5_WEBSOCKET_URL`)
|
||||
- **客户端**:浏览器原生 `WebSocket`(`ws://` / `wss://`)
|
||||
- **连接时携带参数(建议)**:
|
||||
- URL Query:`token`(用户登录态 user-token)、`auth_token`(接口鉴权)、`device_id`(设备标识)、`lang`(`zh/en`)
|
||||
- 示例:`wss://ws.example.com/game?token=xxx&auth_token=xxx&device_id=ios_001&lang=zh`
|
||||
- **连接成功返回(服务端首帧建议)**:
|
||||
- `event`:`ws.connected`
|
||||
- `connection_id`:连接唯一标识
|
||||
- `server_time`:服务器时间戳(秒)
|
||||
- `heartbeat_interval`:心跳间隔(秒)
|
||||
- **连接失败返回(建议)**:
|
||||
- `event`:`ws.error`
|
||||
- `code`:错误码(如 `1101` 未登录、`1103` 鉴权失败)
|
||||
- `message`:错误描述
|
||||
- **建议消息**:
|
||||
- 心跳:`{"action":"ping"}`
|
||||
- 订阅状态流:`{"action":"subscribe","topics":["period.tick","period.opened"]}`
|
||||
- 订阅资金流:`{"action":"subscribe","topics":["bet.accepted","wallet.changed"]}`
|
||||
- 订阅托管流:`{"action":"subscribe","topics":["auto.spin.progress","wallet.changed"]}`
|
||||
|
||||
约定说明:
|
||||
### 7.1A 后台连接方式(管理端联调)
|
||||
|
||||
- **用户私有频道一律使用对外标识 `uuid`,不使用数据库主键 `user_id`**,避免与后台、日志、多端展示口径不一致,并降低枚举内网 ID 的风险。
|
||||
- 名称以 `private-` 开头的频道必须通过 **私有频道鉴权**(见 7.2)成功后才能收到服务端推送。
|
||||
- `public-*` 可直接订阅,无需鉴权 HTTP 步骤。
|
||||
- 后台菜单:仅保留一个菜单 `连接服务器websocket`,用于统一联调 WebSocket
|
||||
- 后台连接入口:
|
||||
- `/admin/test.GameCurrentStatus/wsConfig`
|
||||
- 后台页面能力:
|
||||
- 读取 `ws_url`、`connect_tip`、`sample_messages`
|
||||
- 手动连接/断开 WebSocket
|
||||
- 手动发送订阅与心跳报文
|
||||
- 实时查看服务端返回帧内容(用于联调事件格式)
|
||||
|
||||
### 7.2 连接地址与鉴权流程
|
||||
### 7.2 HTTP 兜底接口
|
||||
|
||||
**WebSocket 连接 URL(与官方 `push.js` 一致)**
|
||||
- **当前期状态**:`POST /api/game/currentStatus`(建议 1 秒/次兜底)
|
||||
- **开奖记录**:`POST /api/game/periodHistory`(建议 3~5 秒/次兜底)
|
||||
- **余额快照**:`POST /api/wallet/balanceSummary`(下注后主动刷新)
|
||||
|
||||
- 形如:`{websocket_base}/app/{app_key}`
|
||||
- 示例(本地默认配置见 `config/plugin/webman/push/app.php`):`ws://127.0.0.1:3131/app/{app_key}`
|
||||
- 生产环境请改为 `wss://` 与对外域名,并与网关/证书一致。
|
||||
### 7.3 一致性规则
|
||||
|
||||
**连接建立后的协议步骤(简述)**
|
||||
|
||||
1. 客户端建立 WebSocket,服务端下发 `pusher:connection_established`,payload 内含 **`socket_id`**(后续鉴权必填)。
|
||||
2. 订阅 **公共** 频道:发送 `pusher:subscribe`,`data` 仅含 `channel` 名即可。
|
||||
3. 订阅 **私有** 频道:
|
||||
- 客户端向 **鉴权接口** 发起 `POST`(`Content-Type: application/x-www-form-urlencoded`),表单字段:`channel_name`、`socket_id`。
|
||||
- 默认鉴权路径为 **`/plugin/webman/push/auth`**(与 `config/plugin/webman/push/app.php` 中 `auth` 一致,可随部署调整)。
|
||||
- 服务端校验「当前登录用户是否允许订阅该 `channel_name`」——对 `private-user-{uuid}` 应校验 **`uuid` 与当前用户一致**,否则返回 `403`。
|
||||
- 鉴权成功返回的 JSON 由 `push.js` 原样作为 `pusher:subscribe` 的 `data` 发送(含 `auth` 等字段)。
|
||||
|
||||
**与移动端登录态的关系**
|
||||
|
||||
- 客户端在调用鉴权接口时,除 `channel_name` / `socket_id` 外,需携带与 REST API 一致的 **`user-token`(及业务所需的 `auth-token`)**,由服务端解析用户身份后再比对 `private-user-{uuid}`。
|
||||
- **不建议**依赖浏览器 Cookie Session 作为唯一依据(H5 外还有 App 内嵌、小程序等);若仅沿用框架示例中的 Session,需在落地实现中改为 **无状态 token 校验**。
|
||||
|
||||
### 7.3 事件定义(初设)
|
||||
|
||||
| 事件名 | 建议频道 | 说明 |
|
||||
|--------|----------|------|
|
||||
| `period.tick` | `public-game-period` | 倒计时广播 |
|
||||
| `period.locked` | `public-game-period` | 封盘 |
|
||||
| `period.opened` | `public-game-period` | 开奖完成(中奖号码) |
|
||||
| `bet.accepted` | `private-user-{uuid}` | 下注成功回执 |
|
||||
| `bet.settled` | `private-user-{uuid}` | **每期每用户一条**:该局开奖对该用户全部注单的汇总(`total_win_amount`、`order_count`、`hit_order_count`、`result_number`、`balance_after`;不再按单笔注单重复推送) |
|
||||
| `wallet.changed` | `private-user-{uuid}` | 余额变化(中奖派彩入账等;`reason=payout` 等) |
|
||||
| `notice.popout` | `public-operation-notice` 或 `private-user-{uuid}` | 强公告(按业务选择广播或定向) |
|
||||
| `withdraw.review_required` | `private-user-{uuid}` | 提现进入审核 |
|
||||
|
||||
### 7.4 消息形态(客户端解析)
|
||||
|
||||
连接上收到的单帧一般为 JSON,常见两类:
|
||||
|
||||
- 协议类:`event` 为 `pusher:connection_established`、`pusher_internal:subscription_succeeded` 等。
|
||||
- 业务类:`event` 为业务事件名,`channel` 为频道名,`data` 为负载(可能为字符串化的 JSON,客户端需 `JSON.parse` 一次)。
|
||||
|
||||
业务负载示例(与初设一致,字段以实际实现为准):
|
||||
|
||||
```json
|
||||
{
|
||||
"event": "period.opened",
|
||||
"channel": "public-game-period",
|
||||
"data": {
|
||||
"period_no": "20260416001",
|
||||
"result_number": 18,
|
||||
"open_time": 1776326400
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 7.5 降级与一致性
|
||||
|
||||
- 推送仅作 **体验增强**:断线、弱网时客户端仍应以 **HTTP 轮询/用户主动刷新**(如 `/api/game/periodCurrent`、`/api/wallet/balanceSummary`)为准。
|
||||
- 同一业务状态以 **服务端落库与接口查询** 为最终一致;推送到达顺序不保证与业务因果严格一致,需客户端幂等与去重(可带 `period_no` / `order_no` / 时间戳)。
|
||||
|
||||
### 7.6 使用 Apipost 调试 WebSocket 与私有频道
|
||||
|
||||
Apipost(v7+)支持 **WebSocket**:新建请求 → 选择 **WebSocket** → 类型选 **Raw**。私有频道遵循「先拿 `socket_id` → 再 HTTP 鉴权 → 再发 `pusher:subscribe`」,与 `vendor/webman/push/src/push.js` 行为一致。
|
||||
|
||||
**A. 仅调试公共频道(如 `public-game-period`)**
|
||||
|
||||
1. 启动 webman 与 push 进程,确认 `config/plugin/webman/push/app.php` 中 `websocket`、`app_key`。
|
||||
2. 在 Apipost 中 WebSocket URL 填:`ws://127.0.0.1:3131/app/{app_key}`(将 `{app_key}` 换成配置中的真实值)。
|
||||
3. 点击连接,在消息面板应收到一帧 `pusher:connection_established`,从中取出 `socket_id`(公共订阅可不依赖后续步骤,但便于对照协议)。
|
||||
4. 在发送框填入一行 JSON(勿带代码块标记)并发送:
|
||||
`{"event":"pusher:subscribe","data":{"channel":"public-game-period"}}`
|
||||
5. 成功时随后会收到 `pusher_internal:subscription_succeeded`;之后服务端向该频道 `trigger` 的事件会出现在消息列表中。
|
||||
|
||||
**B. 调试用户私有频道 `private-user-{uuid}`**
|
||||
|
||||
1. 同上先连接,从首帧解析出 **`socket_id`**。
|
||||
2. 新建 **HTTP** 请求:`POST http://{你的HTTP入口}/plugin/webman/push/auth`
|
||||
- Header:`Content-Type: application/x-www-form-urlencoded`
|
||||
- Body(x-www-form-urlencoded):`channel_name=private-user-{替换为真实uuid}&socket_id={上一步的socket_id}`
|
||||
- 若鉴权已接入 `user-token`,请在 Header 中一并带上与移动端一致的 **`user-token`**(及 `auth-token` 等),否则会得到 `403` 或无效签名。
|
||||
3. 将接口返回的 **JSON 正文**(整段)作为 `pusher:subscribe` 的 `data`:在 Apipost WebSocket 发送
|
||||
`{"event":"pusher:subscribe","data": <上一步响应 JSON 对象>}`
|
||||
注意:`push.js` 会把鉴权返回与 `channel` 字段合并后再发送;若手搓 JSON,需保证与官方协议一致(含 `auth` 字段)。
|
||||
4. 订阅成功后即可在消息面板等待该私有频道上的业务事件。
|
||||
|
||||
**说明**:若仅做协议连通性验证,可暂时使用服务端对鉴权接口的占位实现;**上线前**必须落实「`channel_name` 与当前用户 `uuid` 匹配」校验,避免越权订阅。
|
||||
- 倒计时以服务端下发时间为准,不信任本地时钟累计。
|
||||
- 下注成功后以 `placeBet` 返回的 `balance_after` 为准,再调用钱包接口兜底。
|
||||
- WebSocket 断线后立即重连,并并发触发 `currentStatus + balanceSummary` 全量回补。
|
||||
|
||||
---
|
||||
|
||||
@@ -772,13 +728,10 @@ Apipost(v7+)支持 **WebSocket**:新建请求 → 选择 **WebSocket** →
|
||||
1. `GET /api/v1/authToken?secret=xxx×tamp=xxx&device_id=xxx&signature=xxx` 获取 `auth-token`
|
||||
2. `POST /api/user/login` 登录(请求头带 `auth-token`)
|
||||
3. `POST /api/game/lobbyInit` 拉首页初始化(请求头带 `auth-token`)
|
||||
3. 建立 webman/push 连接并订阅:
|
||||
- `public-game-period`
|
||||
- `private-user-{user.uuid}`(`uuid` 取自登录/档案接口,与 7.1 一致)
|
||||
4. 收到 `period.tick` 实时刷新倒计时
|
||||
5. 用户下注调用 `POST /api/game/betPlace`
|
||||
6. 监听 `bet.accepted` + `wallet.changed` 更新下注结果和余额
|
||||
7. 监听 `period.opened` 渲染开奖动画并刷新开奖记录
|
||||
4. 建立 WebSocket(H5)连接,发送订阅消息监听状态流
|
||||
5. 用户下注调用 `POST /api/game/placeBet`
|
||||
6. 下单后调用 `POST /api/wallet/balanceSummary` 刷新余额(并等待 WebSocket 消息)
|
||||
7. 断线或页面回前台时,兜底调用 `currentStatus + periodHistory` 回补状态
|
||||
|
||||
## 8.2 充值到下注到提现闭环
|
||||
1. 拉取档位:`POST /api/finance/depositTierList`(玩家选择一档,并记下该档 `channels[].code`)
|
||||
@@ -786,8 +739,8 @@ Apipost(v7+)支持 **WebSocket**:新建请求 → 选择 **WebSocket** →
|
||||
- 返回 `paid=false`、`status=pending`、**非空 `pay_url`**:客户端在 WebView/浏览器中打开 `pay_url`(`GET /api/finance/depositMockPayPage`);用户在模拟页点击确认后,由 `POST /api/finance/depositMockNotify` 完成入账,或轮询 `depositDetail` / 等 `wallet.changed` 再刷新余额
|
||||
- 未来接真实第三方:将 `pay_url` 换为真网关,入账仅在支付平台 **异步通知** 中调用 `DepositSettlement::settle`(与当前 `depositMockNotify` 路径一致)
|
||||
3. 客户端可选轮询 `POST /api/finance/depositDetail` 兜底确认状态;入账成功后会收到 `wallet.changed`
|
||||
4. 下注:`POST /api/game/betPlace`
|
||||
5. 派彩后收到 `wallet.changed`
|
||||
4. 下注:`POST /api/game/placeBet`
|
||||
5. 轮询余额:`POST /api/wallet/balanceSummary`
|
||||
6. 查询流水:`POST /api/wallet/recordList`
|
||||
7. 提现:`POST /api/finance/withdrawCreate`(即时冻结 `user.coin` 与写出 `withdraw` 流水) -> `POST /api/finance/withdrawDetail`
|
||||
|
||||
@@ -799,22 +752,20 @@ Apipost(v7+)支持 **WebSocket**:新建请求 → 选择 **WebSocket** →
|
||||
|
||||
---
|
||||
|
||||
## 9. 游戏时序流程图(接口 + 推送)
|
||||
## 9. 游戏时序流程图(WebSocket + HTTP兜底)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
A[用户登录 /api/user/login] --> B[拉初始化 /api/game/lobbyInit]
|
||||
B --> C[连接webman/push并订阅频道]
|
||||
C --> D[收到 period.tick 倒计时]
|
||||
D --> E{0-20秒下注期?}
|
||||
E -- 是 --> F[提交下注 /api/game/betPlace]
|
||||
F --> G[推送 bet.accepted + wallet.changed]
|
||||
E -- 否 --> H[进入封盘状态 period.locked]
|
||||
H --> I[服务端算票与开奖]
|
||||
I --> J[推送 period.opened]
|
||||
J --> K[客户端开奖动画与结果展示]
|
||||
K --> L[客户端刷新开奖记录 /api/game/periodHistory]
|
||||
L --> D
|
||||
B --> C[连接 WebSocket 并订阅主题]
|
||||
C --> D{0-20秒下注期?}
|
||||
D -- 是 --> E[提交下注 /api/game/placeBet]
|
||||
E --> F[刷新余额 /api/wallet/balanceSummary]
|
||||
D -- 否 --> G[进入封盘与开奖阶段]
|
||||
G --> H[服务端算票与开奖]
|
||||
H --> I[WebSocket 推送状态变化]
|
||||
I --> J[断线兜底 /api/game/currentStatus]
|
||||
J --> C
|
||||
```
|
||||
|
||||
---
|
||||
@@ -874,7 +825,7 @@ flowchart TD
|
||||
1. **登录方式**:仅账号密码,还是要短信/邮箱验证码?
|
||||
2. **提现收款类型**:首版只做银行卡,还是同时支持电子钱包/加密地址?
|
||||
3. **自动托管**:是否首期上线;若不上线可先隐藏 `auto-bet` 接口。
|
||||
4. **push事件最小集**:是否先只上 `period.tick`、`period.opened`、`wallet.changed` 三类。
|
||||
4. **WebSocket 主题定义**:状态流、资金流、托管流的 topic 与消息体是否按本文固定。
|
||||
5. **错误码规范**:是否已有公司统一错误码表;若有需对齐替换本草案码段。
|
||||
|
||||
确认后可进入下一步:按该文档落地 controller + validate + service + 路由。
|
||||
|
||||
252
docs/_36字花_前端开发对接与交互逻辑说明书.txt
Normal file
252
docs/_36字花_前端开发对接与交互逻辑说明书.txt
Normal file
@@ -0,0 +1,252 @@
|
||||
《"36字花" 前端开发对接与交互逻辑说明书》
|
||||
适用对象
|
||||
:前端开发工程师 (React / Vue / 移动端 H5 开发者)
|
||||
核心要求
|
||||
:极高的渲染性能(移动端务必保持 60FPS,禁止重绘卡顿),极致的状态同步(30秒循环状态机绝对不可发生状态错乱)。
|
||||
一、 全局架构与技术栈建议 (Architecture Guidelines)
|
||||
状态管理 (State Management)
|
||||
:由于游戏状态极其复杂(连赢限额、统一下注金额、倒计时同步),强烈推荐使用全局状态管理库(如 React的
|
||||
Redux/Zustand
|
||||
或 Vue的
|
||||
Pinia
|
||||
),将
|
||||
“业务逻辑数据层”
|
||||
与
|
||||
“UI渲染层”
|
||||
完全解耦。
|
||||
动画性能 (Animation Performance)
|
||||
:
|
||||
盘面的 10 种状态切换和高频边框动画,
|
||||
严禁使用 JS 帧动画操作 DOM
|
||||
。
|
||||
必须全部采用
|
||||
CSS3
|
||||
transition
|
||||
、
|
||||
animation
|
||||
、
|
||||
box-shadow
|
||||
或
|
||||
SVG (
|
||||
stroke-dasharray
|
||||
)
|
||||
。
|
||||
涉及到全屏爆金币的粒子特效 (Particle Effects),建议直接调用轻量级 Canvas 动画库(如
|
||||
tsparticles
|
||||
或
|
||||
PixiJS
|
||||
)。
|
||||
时间同步 (Time Sync)
|
||||
:绝对不要信任客户端(手机/浏览器)的本地时间!游戏倒计时必须以 WebSocket/API 下发的服务器时间戳为基准进行倒推补偿。
|
||||
二、 核心状态机:30秒生命周期 (The 30s Lifecycle) 注意⚠️这个时间后台可配置
|
||||
前端的整个游戏循环被严格划分为 4 个生命周期,请在代码中建立全局的
|
||||
GameState
|
||||
枚举,并根据状态驱动 UI 渲染:
|
||||
GameState.BETTING
|
||||
|
||||
(0 - 20秒:下注期)
|
||||
行为
|
||||
:允许用户点击格子、选筹码、点确认。
|
||||
UI
|
||||
:倒计时递减。背景跑马灯常态运转。
|
||||
GameState.LOCKED
|
||||
|
||||
(20.0秒:封盘锁定)
|
||||
行为
|
||||
:
|
||||
前端立即进行物理锁盘
|
||||
。无论网络是否有延迟,只要本地计算到达20.0秒,立即禁用所有输入框、点击事件和按钮。
|
||||
UI
|
||||
:弹出
|
||||
[停止下注]
|
||||
提示,未点击确认的预选状态 (
|
||||
PRE_SELECTED
|
||||
) 格子全部强制清除。
|
||||
GameState.DRAWING
|
||||
|
||||
(20 - 25秒:算票与开奖)
|
||||
行为
|
||||
:等待后端 WebSocket 推送开奖结果。
|
||||
UI
|
||||
:收到结果后,前端触发 3 秒的高频加速跑马灯,最后 0.5 秒光圈定格在中奖格子上,触发
|
||||
WINNING
|
||||
状态大爆动画。
|
||||
GameState.PAYOUT
|
||||
|
||||
(25 - 30秒:结算派彩)
|
||||
行为
|
||||
:监听余额变更推送,更新连赢 (Streak) 状态。
|
||||
UI
|
||||
:播放中奖特效,更新底部走势图 (红蓝圆点),准备进入下一局。
|
||||
三、 组件级交互逻辑:36字花主键盘 (The 36-Grid System)
|
||||
前端需要封装一个
|
||||
<Cell />
|
||||
组件,该组件接收一个
|
||||
status
|
||||
属性(范围 0-9),并根据该枚举值切换对应的 CSS Class。
|
||||
📌 10 种状态枚举 (CellStatus Enum) 映射逻辑
|
||||
IDLE
|
||||
(闲置):默认状态,深色,偶发微光 CSS 动画。
|
||||
MARQUEE
|
||||
(跑马灯焦点):全局维护一个
|
||||
activeCellIndex
|
||||
每 0.1秒随机变更,命中的组件亮起青色霓虹边框。
|
||||
HOVER
|
||||
(悬浮):
|
||||
:hover
|
||||
伪类触发(仅 PC)。
|
||||
PRE_SELECTED
|
||||
(预选中):前端本地状态数组。显示筹码图标,金边流光。
|
||||
LOCKED
|
||||
(已确认):调用
|
||||
place_bet
|
||||
API 成功后切换至此状态。显示锁定印章,绿色边框。
|
||||
DISABLED
|
||||
(禁用):封盘时,或已选中数量达标(5个)时,其余格子强制渲染黑色 60% 遮罩。
|
||||
ERROR
|
||||
(错误抖动):触发 CSS
|
||||
shake
|
||||
动画,维持 0.5s 后回退到上一个状态。
|
||||
WINNING
|
||||
(中奖大爆):开奖目标,Z-index 置顶,放大 1.25 倍,播放强脉冲 CSS。
|
||||
LOSER
|
||||
(落选陪跑):透明度设为
|
||||
opacity: 0.2
|
||||
。
|
||||
AUTO_ACTIVE
|
||||
(自动托管中):全局遮罩下,该格子穿透遮罩,显示紫色虚线动画与
|
||||
AUTO
|
||||
印章。
|
||||
📌 核心防呆交互逻辑 (必须用代码写死限制)
|
||||
统一下注金额联动 (Chip Sync)
|
||||
:
|
||||
全局维护一个
|
||||
currentChipValue
|
||||
(当前选中的筹码)。
|
||||
如果用户修改了
|
||||
currentChipValue
|
||||
,前端必须
|
||||
遍历所有状态为
|
||||
PRE_SELECTED
|
||||
的格子,将它们显示的筹码瞬间同步为新金额
|
||||
。
|
||||
数量限制校验 (Max 5 Limit)
|
||||
:
|
||||
实时计算:
|
||||
count(PRE_SELECTED) + count(LOCKED)
|
||||
必须
|
||||
<= 5
|
||||
。
|
||||
等于 5 时,其余 31 个
|
||||
IDLE
|
||||
状态的格子必须变成
|
||||
DISABLED
|
||||
状态。如果强行点击,触发
|
||||
ERROR
|
||||
动画。
|
||||
连赢上限校验 (Streak Bet Limit)
|
||||
:
|
||||
如果玩家上一局赢了,API 会下发一个
|
||||
streakMaxBetLimit
|
||||
(连赢最高下注总额,如 💎 100)。
|
||||
前端需要写一个
|
||||
useEffect
|
||||
/
|
||||
watch
|
||||
:实时计算
|
||||
当前选中数字数量 * currentChipValue
|
||||
。如果这个乘积
|
||||
> streakMaxBetLimit
|
||||
,或者当前账户余额不足,左下角的那个筹码图标必须添加
|
||||
disabled
|
||||
属性(变灰不可点)。
|
||||
四、 核心中控台交互 (Control Panel & Actions)
|
||||
1.
|
||||
[✅ 确定下注 Confirm]
|
||||
主按钮的状态机
|
||||
这是全场最重要的按钮,前端必须维护它独立的四态机:
|
||||
Disabled
|
||||
:未选中任何格子时。
|
||||
Ready (高亮呼吸)
|
||||
:选中格子 > 0,且总注金 <= 余额。绑定
|
||||
onClick -> handleSubmit
|
||||
。
|
||||
Error (红色)
|
||||
:总注金 > 余额。文字变成“余额不足”。
|
||||
Success (绿色)
|
||||
:收到 API 200 成功响应后,维持绿色直到本局结束。
|
||||
1.
|
||||
Auto-Spin(自动托管)逻辑流
|
||||
数据层
|
||||
:调用
|
||||
/api/auto_spin
|
||||
告知后端要买哪些数字、金额和局数。
|
||||
UI 层
|
||||
:前端进入
|
||||
AUTO_MODE
|
||||
全局变量。
|
||||
整个盘面外层盖一个
|
||||
<div className="glass-overlay" />
|
||||
,
|
||||
pointer-events: none
|
||||
(阻断一切鼠标点击)。
|
||||
只有目标格子的状态被设为
|
||||
AUTO_ACTIVE
|
||||
,并使用 CSS
|
||||
z-index
|
||||
穿透遮罩。
|
||||
监听后端 WebSocket 下发的每一局扣款成功事件,更新底部控制台的进度条(如
|
||||
3/50 局
|
||||
)。
|
||||
3.
|
||||
Red/Blue 路子图渲染逻辑 (Trend Chart)
|
||||
接收一个含有最近 30 期开奖数字的 Array
|
||||
[08, 15, 36, ...]
|
||||
。
|
||||
渲染判断
|
||||
:
|
||||
item % 2 === 0
|
||||
(偶数) 渲染蓝色圆圈;
|
||||
item % 2 !== 0
|
||||
(奇数) 渲染红色圆圈。
|
||||
入场动画
|
||||
:当有新数字加入 Array 时,最后一个圆圈必须带有
|
||||
slide-in
|
||||
或
|
||||
pop-in
|
||||
动画。
|
||||
五、 网络延迟与极端异常处理 (Edge Cases & Fallbacks)
|
||||
博彩游戏的前端,对异常处理的要求极高,请前端必须实现以下机制:
|
||||
首屏强制公告 (Welcome Pop-out)
|
||||
进页面时调用
|
||||
/api/user/announcement
|
||||
。如果有未读公告,弹出
|
||||
<Modal />
|
||||
。
|
||||
“进入游戏”的
|
||||
Button
|
||||
绑定
|
||||
disabled={!isChecked}
|
||||
。不勾选绝对不给进。
|
||||
压秒网络卡顿防错 (The 19.9s Click)
|
||||
场景
|
||||
:倒计时剩 0.1 秒,玩家点击了【确定下注】,前端发起了 HTTP/Socket 请求,但因为网络差,请求还没到服务器,本地倒计时先归零了。
|
||||
处理方案
|
||||
:前端立即锁盘进入
|
||||
LOCKED
|
||||
状态,并显示 Loading (spinner)。当 2 秒后收到后端的 400 Bad Request(提示已封盘)时,前端必须
|
||||
清除这个格子的状态,并弹窗提示
|
||||
[网络延迟,下注失败,未扣款]
|
||||
。千万不能强行把它变成
|
||||
LOCKED
|
||||
绿勾。
|
||||
断线重连恢复 (Reconnection Recovery)
|
||||
场景
|
||||
:玩家切出微信看消息,5分钟后切回浏览器。
|
||||
处理方案
|
||||
:前端检测到
|
||||
visibilitychange
|
||||
或者 Socket 断开,必须立刻重新发起
|
||||
/api/game/current_status
|
||||
全量拉取请求。根据服务器返回的数据,瞬间重置本地的倒计时、当前连胜数、走势图数据。
|
||||
绝对不要依赖本地时间的积累运算。
|
||||
BIN
docs/《_36字花_ 前端开发对接与交互逻辑说明书》.docx
Normal file
BIN
docs/《_36字花_ 前端开发对接与交互逻辑说明书》.docx
Normal file
Binary file not shown.
@@ -1,746 +0,0 @@
|
||||
|
||||
function Push(options) {
|
||||
this.doNotConnect = 0;
|
||||
options = options || {};
|
||||
options.heartbeat = options.heartbeat || 25000;
|
||||
options.pingTimeout = options.pingTimeout || 10000;
|
||||
this.config = options;
|
||||
this.uid = 0;
|
||||
this.channels = {};
|
||||
this.connection = null;
|
||||
this.pingTimeoutTimer = 0;
|
||||
Push.instances.push(this);
|
||||
this.createConnection();
|
||||
}
|
||||
|
||||
Push.prototype.checkoutPing = function() {
|
||||
var _this = this;
|
||||
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
|
||||
_this.checkoutPingTimer = setTimeout(function () {
|
||||
_this.checkoutPingTimer = 0;
|
||||
if (_this.connection.state === 'connected') {
|
||||
_this.connection.send('{"event":"pusher:ping","data":{}}');
|
||||
if (_this.pingTimeoutTimer) {
|
||||
clearTimeout(_this.pingTimeoutTimer);
|
||||
_this.pingTimeoutTimer = 0;
|
||||
}
|
||||
_this.pingTimeoutTimer = setTimeout(function () {
|
||||
_this.connection.closeAndClean();
|
||||
if (!_this.connection.doNotConnect) {
|
||||
_this.connection.waitReconnect();
|
||||
}
|
||||
}, _this.config.pingTimeout);
|
||||
}
|
||||
}, this.config.heartbeat);
|
||||
};
|
||||
|
||||
Push.prototype.channel = function (name) {
|
||||
return this.channels.find(name);
|
||||
};
|
||||
Push.prototype.allChannels = function () {
|
||||
return this.channels.all();
|
||||
};
|
||||
Push.prototype.createConnection = function () {
|
||||
if (this.connection) {
|
||||
throw Error('Connection already exist');
|
||||
}
|
||||
var _this = this;
|
||||
var url = this.config.url;
|
||||
function updateSubscribed () {
|
||||
for (var i in _this.channels) {
|
||||
_this.channels[i].subscribed = false;
|
||||
}
|
||||
}
|
||||
this.connection = new Connection({
|
||||
url: url,
|
||||
app_key: this.config.app_key,
|
||||
onOpen: function () {
|
||||
_this.connection.state ='connecting';
|
||||
_this.checkoutPing();
|
||||
},
|
||||
onMessage: function(params) {
|
||||
if(_this.pingTimeoutTimer) {
|
||||
clearTimeout(_this.pingTimeoutTimer);
|
||||
_this.pingTimeoutTimer = 0;
|
||||
}
|
||||
|
||||
params = JSON.parse(params.data);
|
||||
var event = params.event;
|
||||
var channel_name = params.channel;
|
||||
|
||||
if (event === 'pusher:pong') {
|
||||
_this.checkoutPing();
|
||||
return;
|
||||
}
|
||||
if (event === 'pusher:error') {
|
||||
throw Error(params.data.message);
|
||||
}
|
||||
var data = JSON.parse(params.data), channel;
|
||||
if (event === 'pusher_internal:subscription_succeeded') {
|
||||
channel = _this.channels[channel_name];
|
||||
channel.subscribed = true;
|
||||
channel.processQueue();
|
||||
channel.emit('pusher:subscription_succeeded');
|
||||
return;
|
||||
}
|
||||
if (event === 'pusher:connection_established') {
|
||||
_this.connection.socket_id = data.socket_id;
|
||||
_this.connection.updateNetworkState('connected');
|
||||
_this.subscribeAll();
|
||||
}
|
||||
if (event.indexOf('pusher_internal') !== -1) {
|
||||
console.log("Event '"+event+"' not implement");
|
||||
return;
|
||||
}
|
||||
channel = _this.channels[channel_name];
|
||||
if (channel) {
|
||||
channel.emit(event, data);
|
||||
}
|
||||
},
|
||||
onClose: function () {
|
||||
updateSubscribed();
|
||||
},
|
||||
onError: function () {
|
||||
updateSubscribed();
|
||||
}
|
||||
});
|
||||
};
|
||||
Push.prototype.disconnect = function () {
|
||||
this.connection.doNotConnect = 1;
|
||||
this.connection.close();
|
||||
};
|
||||
|
||||
Push.prototype.subscribeAll = function () {
|
||||
if (this.connection.state !== 'connected') {
|
||||
return;
|
||||
}
|
||||
for (var channel_name in this.channels) {
|
||||
//this.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
|
||||
this.channels[channel_name].processSubscribe();
|
||||
}
|
||||
};
|
||||
|
||||
Push.prototype.unsubscribe = function (channel_name) {
|
||||
if (this.channels[channel_name]) {
|
||||
delete this.channels[channel_name];
|
||||
if (this.connection.state === 'connected') {
|
||||
this.connection.send(JSON.stringify({event:"pusher:unsubscribe", data:{channel:channel_name}}));
|
||||
}
|
||||
}
|
||||
};
|
||||
Push.prototype.unsubscribeAll = function () {
|
||||
var channels = Object.keys(this.channels);
|
||||
if (channels.length) {
|
||||
if (this.connection.state === 'connected') {
|
||||
for (var channel_name in this.channels) {
|
||||
this.unsubscribe(channel_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.channels = {};
|
||||
};
|
||||
Push.prototype.subscribe = function (channel_name) {
|
||||
if (this.channels[channel_name]) {
|
||||
return this.channels[channel_name];
|
||||
}
|
||||
if (channel_name.indexOf('private-') === 0) {
|
||||
return createPrivateChannel(channel_name, this);
|
||||
}
|
||||
if (channel_name.indexOf('presence-') === 0) {
|
||||
return createPresenceChannel(channel_name, this);
|
||||
}
|
||||
return createChannel(channel_name, this);
|
||||
};
|
||||
Push.instances = [];
|
||||
|
||||
function createChannel(channel_name, push)
|
||||
{
|
||||
var channel = new Channel(push.connection, channel_name);
|
||||
push.channels[channel_name] = channel;
|
||||
channel.subscribeCb = function () {
|
||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
|
||||
}
|
||||
channel.processSubscribe();
|
||||
return channel;
|
||||
}
|
||||
|
||||
function createPrivateChannel(channel_name, push)
|
||||
{
|
||||
var channel = new Channel(push.connection, channel_name);
|
||||
push.channels[channel_name] = channel;
|
||||
channel.subscribeCb = function () {
|
||||
__ajax({
|
||||
url: push.config.auth,
|
||||
type: 'POST',
|
||||
data: {channel_name: channel_name, socket_id: push.connection.socket_id},
|
||||
success: function (data) {
|
||||
data = JSON.parse(data);
|
||||
data.channel = channel_name;
|
||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:data}));
|
||||
},
|
||||
error: function (e) {
|
||||
throw Error(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
channel.processSubscribe();
|
||||
return channel;
|
||||
}
|
||||
|
||||
function createPresenceChannel(channel_name, push)
|
||||
{
|
||||
return createPrivateChannel(channel_name, push);
|
||||
}
|
||||
|
||||
/*window.addEventListener('online', function(){
|
||||
var con;
|
||||
for (var i in Push.instances) {
|
||||
con = Push.instances[i].connection;
|
||||
con.reconnectInterval = 1;
|
||||
if (con.state === 'connecting') {
|
||||
con.connect();
|
||||
}
|
||||
}
|
||||
});*/
|
||||
|
||||
|
||||
function Connection(options) {
|
||||
this.dispatcher = new Dispatcher();
|
||||
__extends(this, this.dispatcher);
|
||||
var properies = ['on', 'off', 'emit'];
|
||||
for (var i in properies) {
|
||||
this[properies[i]] = this.dispatcher[properies[i]];
|
||||
}
|
||||
this.options = options;
|
||||
this.state = 'initialized'; //initialized connecting connected disconnected
|
||||
this.doNotConnect = 0;
|
||||
this.reconnectInterval = 1;
|
||||
this.connection = null;
|
||||
this.reconnectTimer = 0;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
Connection.prototype.updateNetworkState = function(state){
|
||||
var old_state = this.state;
|
||||
this.state = state;
|
||||
if (old_state !== state) {
|
||||
this.emit('state_change', { previous: old_state, current: state });
|
||||
}
|
||||
};
|
||||
|
||||
Connection.prototype.connect = function () {
|
||||
this.doNotConnect = 0;
|
||||
if (this.state === 'connected') {
|
||||
console.log('networkState is "' + this.state + '" and do not need connect');
|
||||
return;
|
||||
}
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = 0;
|
||||
}
|
||||
|
||||
this.closeAndClean();
|
||||
|
||||
var options = this.options;
|
||||
var websocket = new WebSocket(options.url+'/app/'+options.app_key);
|
||||
|
||||
this.updateNetworkState('connecting');
|
||||
|
||||
var _this = this;
|
||||
websocket.onopen = function (res) {
|
||||
_this.reconnectInterval = 1;
|
||||
if (_this.doNotConnect) {
|
||||
_this.updateNetworkState('disconnected');
|
||||
websocket.close();
|
||||
return;
|
||||
}
|
||||
if (options.onOpen) {
|
||||
options.onOpen(res);
|
||||
}
|
||||
};
|
||||
|
||||
if (options.onMessage) {
|
||||
websocket.onmessage = options.onMessage;
|
||||
}
|
||||
|
||||
websocket.onclose = function (res) {
|
||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
||||
_this.updateNetworkState('disconnected');
|
||||
if (!_this.doNotConnect) {
|
||||
_this.waitReconnect();
|
||||
}
|
||||
if (options.onClose) {
|
||||
options.onClose(res);
|
||||
}
|
||||
};
|
||||
|
||||
websocket.onerror = function (res) {
|
||||
_this.close();
|
||||
if (!_this.doNotConnect) {
|
||||
_this.waitReconnect();
|
||||
}
|
||||
if (options.onError) {
|
||||
options.onError(res);
|
||||
}
|
||||
};
|
||||
this.connection = websocket;
|
||||
}
|
||||
|
||||
Connection.prototype.closeAndClean = function () {
|
||||
if(this.connection) {
|
||||
var websocket = this.connection;
|
||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
||||
try {
|
||||
websocket.close();
|
||||
} catch (e) {}
|
||||
this.updateNetworkState('disconnected');
|
||||
}
|
||||
};
|
||||
|
||||
Connection.prototype.waitReconnect = function () {
|
||||
if (this.state === 'connected' || this.state === 'connecting') {
|
||||
return;
|
||||
}
|
||||
if (!this.doNotConnect) {
|
||||
this.updateNetworkState('connecting');
|
||||
var _this = this;
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
}
|
||||
this.reconnectTimer = setTimeout(function(){
|
||||
_this.connect();
|
||||
}, this.reconnectInterval);
|
||||
if (this.reconnectInterval < 1000) {
|
||||
this.reconnectInterval = 1000;
|
||||
} else {
|
||||
// 每次重连间隔增大一倍
|
||||
this.reconnectInterval = this.reconnectInterval * 2;
|
||||
}
|
||||
// 有网络的状态下,重连间隔最大2秒
|
||||
if (this.reconnectInterval > 2000 && navigator.onLine) {
|
||||
_this.reconnectInterval = 2000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Connection.prototype.send = function(data) {
|
||||
if (this.state !== 'connected') {
|
||||
console.trace('networkState is "' + this.state + '", can not send ' + data);
|
||||
return;
|
||||
}
|
||||
this.connection.send(data);
|
||||
}
|
||||
|
||||
Connection.prototype.close = function(){
|
||||
this.updateNetworkState('disconnected');
|
||||
this.connection.close();
|
||||
}
|
||||
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) {d[p] = b[p];}
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
|
||||
function Channel(connection, channel_name) {
|
||||
this.subscribed = false;
|
||||
this.dispatcher = new Dispatcher();
|
||||
this.connection = connection;
|
||||
this.channelName = channel_name;
|
||||
this.subscribeCb = null;
|
||||
this.queue = [];
|
||||
__extends(this, this.dispatcher);
|
||||
var properies = ['on', 'off', 'emit'];
|
||||
for (var i in properies) {
|
||||
this[properies[i]] = this.dispatcher[properies[i]];
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.processSubscribe = function () {
|
||||
if (this.connection.state !== 'connected') {
|
||||
return;
|
||||
}
|
||||
this.subscribeCb();
|
||||
};
|
||||
|
||||
Channel.prototype.processQueue = function () {
|
||||
if (this.connection.state !== 'connected' || !this.subscribed) {
|
||||
return;
|
||||
}
|
||||
for (var i in this.queue) {
|
||||
this.queue[i]();
|
||||
}
|
||||
this.queue = [];
|
||||
};
|
||||
|
||||
Channel.prototype.trigger = function (event, data) {
|
||||
if (event.indexOf('client-') !== 0) {
|
||||
throw new Error("Event '" + event + "' should start with 'client-'");
|
||||
}
|
||||
var _this = this;
|
||||
this.queue.push(function () {
|
||||
_this.connection.send(JSON.stringify({ event: event, data: data, channel: _this.channelName }));
|
||||
});
|
||||
this.processQueue();
|
||||
};
|
||||
|
||||
////////////////
|
||||
var Collections = (function () {
|
||||
var exports = {};
|
||||
function extend(target) {
|
||||
var sources = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
sources[_i - 1] = arguments[_i];
|
||||
}
|
||||
for (var i = 0; i < sources.length; i++) {
|
||||
var extensions = sources[i];
|
||||
for (var property in extensions) {
|
||||
if (extensions[property] && extensions[property].constructor &&
|
||||
extensions[property].constructor === Object) {
|
||||
target[property] = extend(target[property] || {}, extensions[property]);
|
||||
}
|
||||
else {
|
||||
target[property] = extensions[property];
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
exports.extend = extend;
|
||||
function stringify() {
|
||||
var m = ["Push"];
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
if (typeof arguments[i] === "string") {
|
||||
m.push(arguments[i]);
|
||||
}
|
||||
else {
|
||||
m.push(safeJSONStringify(arguments[i]));
|
||||
}
|
||||
}
|
||||
return m.join(" : ");
|
||||
}
|
||||
|
||||
exports.stringify = stringify;
|
||||
function arrayIndexOf(array, item) {
|
||||
var nativeIndexOf = Array.prototype.indexOf;
|
||||
if (array === null) {
|
||||
return -1;
|
||||
}
|
||||
if (nativeIndexOf && array.indexOf === nativeIndexOf) {
|
||||
return array.indexOf(item);
|
||||
}
|
||||
for (var i = 0, l = array.length; i < l; i++) {
|
||||
if (array[i] === item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
exports.arrayIndexOf = arrayIndexOf;
|
||||
function objectApply(object, f) {
|
||||
for (var key in object) {
|
||||
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
||||
f(object[key], key, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.objectApply = objectApply;
|
||||
function keys(object) {
|
||||
var keys = [];
|
||||
objectApply(object, function (_, key) {
|
||||
keys.push(key);
|
||||
});
|
||||
return keys;
|
||||
}
|
||||
|
||||
exports.keys = keys;
|
||||
function values(object) {
|
||||
var values = [];
|
||||
objectApply(object, function (value) {
|
||||
values.push(value);
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
exports.values = values;
|
||||
function apply(array, f, context) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
f.call(context || (window), array[i], i, array);
|
||||
}
|
||||
}
|
||||
|
||||
exports.apply = apply;
|
||||
function map(array, f) {
|
||||
var result = [];
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result.push(f(array[i], i, array, result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.map = map;
|
||||
function mapObject(object, f) {
|
||||
var result = {};
|
||||
objectApply(object, function (value, key) {
|
||||
result[key] = f(value);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.mapObject = mapObject;
|
||||
function filter(array, test) {
|
||||
test = test || function (value) {
|
||||
return !!value;
|
||||
};
|
||||
var result = [];
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (test(array[i], i, array, result)) {
|
||||
result.push(array[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.filter = filter;
|
||||
function filterObject(object, test) {
|
||||
var result = {};
|
||||
objectApply(object, function (value, key) {
|
||||
if ((test && test(value, key, object, result)) || Boolean(value)) {
|
||||
result[key] = value;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.filterObject = filterObject;
|
||||
function flatten(object) {
|
||||
var result = [];
|
||||
objectApply(object, function (value, key) {
|
||||
result.push([key, value]);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.flatten = flatten;
|
||||
function any(array, test) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (test(array[i], i, array)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.any = any;
|
||||
function all(array, test) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (!test(array[i], i, array)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
exports.all = all;
|
||||
function encodeParamsObject(data) {
|
||||
return mapObject(data, function (value) {
|
||||
if (typeof value === "object") {
|
||||
value = safeJSONStringify(value);
|
||||
}
|
||||
return encodeURIComponent(base64_1["default"](value.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
exports.encodeParamsObject = encodeParamsObject;
|
||||
function buildQueryString(data) {
|
||||
var params = filterObject(data, function (value) {
|
||||
return value !== undefined;
|
||||
});
|
||||
return map(flatten(encodeParamsObject(params)), util_1["default"].method("join", "=")).join("&");
|
||||
}
|
||||
|
||||
exports.buildQueryString = buildQueryString;
|
||||
function decycleObject(object) {
|
||||
var objects = [], paths = [];
|
||||
return (function derez(value, path) {
|
||||
var i, name, nu;
|
||||
switch (typeof value) {
|
||||
case 'object':
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
for (i = 0; i < objects.length; i += 1) {
|
||||
if (objects[i] === value) {
|
||||
return {$ref: paths[i]};
|
||||
}
|
||||
}
|
||||
objects.push(value);
|
||||
paths.push(path);
|
||||
if (Object.prototype.toString.apply(value) === '[object Array]') {
|
||||
nu = [];
|
||||
for (i = 0; i < value.length; i += 1) {
|
||||
nu[i] = derez(value[i], path + '[' + i + ']');
|
||||
}
|
||||
}
|
||||
else {
|
||||
nu = {};
|
||||
for (name in value) {
|
||||
if (Object.prototype.hasOwnProperty.call(value, name)) {
|
||||
nu[name] = derez(value[name], path + '[' + JSON.stringify(name) + ']');
|
||||
}
|
||||
}
|
||||
}
|
||||
return nu;
|
||||
case 'number':
|
||||
case 'string':
|
||||
case 'boolean':
|
||||
return value;
|
||||
}
|
||||
}(object, '$'));
|
||||
}
|
||||
|
||||
exports.decycleObject = decycleObject;
|
||||
function safeJSONStringify(source) {
|
||||
try {
|
||||
return JSON.stringify(source);
|
||||
}
|
||||
catch (e) {
|
||||
return JSON.stringify(decycleObject(source));
|
||||
}
|
||||
}
|
||||
|
||||
exports.safeJSONStringify = safeJSONStringify;
|
||||
return exports;
|
||||
})();
|
||||
|
||||
var Dispatcher = (function () {
|
||||
function Dispatcher(failThrough) {
|
||||
this.callbacks = new CallbackRegistry();
|
||||
this.global_callbacks = [];
|
||||
this.failThrough = failThrough;
|
||||
}
|
||||
Dispatcher.prototype.on = function (eventName, callback, context) {
|
||||
this.callbacks.add(eventName, callback, context);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.on_global = function (callback) {
|
||||
this.global_callbacks.push(callback);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.off = function (eventName, callback, context) {
|
||||
this.callbacks.remove(eventName, callback, context);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.emit = function (eventName, data) {
|
||||
var i;
|
||||
for (i = 0; i < this.global_callbacks.length; i++) {
|
||||
this.global_callbacks[i](eventName, data);
|
||||
}
|
||||
var callbacks = this.callbacks.get(eventName);
|
||||
if (callbacks && callbacks.length > 0) {
|
||||
for (i = 0; i < callbacks.length; i++) {
|
||||
callbacks[i].fn.call(callbacks[i].context || (window), data);
|
||||
}
|
||||
}
|
||||
else if (this.failThrough) {
|
||||
this.failThrough(eventName, data);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
return Dispatcher;
|
||||
}());
|
||||
|
||||
var CallbackRegistry = (function () {
|
||||
function CallbackRegistry() {
|
||||
this._callbacks = {};
|
||||
}
|
||||
CallbackRegistry.prototype.get = function (name) {
|
||||
return this._callbacks[prefix(name)];
|
||||
};
|
||||
CallbackRegistry.prototype.add = function (name, callback, context) {
|
||||
var prefixedEventName = prefix(name);
|
||||
this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || [];
|
||||
this._callbacks[prefixedEventName].push({
|
||||
fn: callback,
|
||||
context: context
|
||||
});
|
||||
};
|
||||
CallbackRegistry.prototype.remove = function (name, callback, context) {
|
||||
if (!name && !callback && !context) {
|
||||
this._callbacks = {};
|
||||
return;
|
||||
}
|
||||
var names = name ? [prefix(name)] : Collections.keys(this._callbacks);
|
||||
if (callback || context) {
|
||||
this.removeCallback(names, callback, context);
|
||||
}
|
||||
else {
|
||||
this.removeAllCallbacks(names);
|
||||
}
|
||||
};
|
||||
CallbackRegistry.prototype.removeCallback = function (names, callback, context) {
|
||||
Collections.apply(names, function (name) {
|
||||
this._callbacks[name] = Collections.filter(this._callbacks[name] || [], function (oning) {
|
||||
return (callback && callback !== oning.fn) ||
|
||||
(context && context !== oning.context);
|
||||
});
|
||||
if (this._callbacks[name].length === 0) {
|
||||
delete this._callbacks[name];
|
||||
}
|
||||
}, this);
|
||||
};
|
||||
CallbackRegistry.prototype.removeAllCallbacks = function (names) {
|
||||
Collections.apply(names, function (name) {
|
||||
delete this._callbacks[name];
|
||||
}, this);
|
||||
};
|
||||
return CallbackRegistry;
|
||||
}());
|
||||
function prefix(name) {
|
||||
return "_" + name;
|
||||
}
|
||||
|
||||
function __ajax(options){
|
||||
options=options||{};
|
||||
options.type=(options.type||'GET').toUpperCase();
|
||||
options.dataType=options.dataType||'json';
|
||||
var params=formatParams(options.data);
|
||||
|
||||
var xhr;
|
||||
if(window.XMLHttpRequest){
|
||||
xhr=new XMLHttpRequest();
|
||||
}else{
|
||||
xhr=ActiveXObject('Microsoft.XMLHTTP');
|
||||
}
|
||||
|
||||
xhr.onreadystatechange=function(){
|
||||
if(xhr.readyState === 4){
|
||||
var status=xhr.status;
|
||||
if(status>=200 && status<300){
|
||||
options.success&&options.success(xhr.responseText,xhr.responseXML);
|
||||
}else{
|
||||
options.error&&options.error(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(options.type==='GET'){
|
||||
xhr.open('GET',options.url+'?'+params,true);
|
||||
xhr.send(null);
|
||||
}else if(options.type==='POST'){
|
||||
xhr.open('POST',options.url,true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
xhr.send(params);
|
||||
}
|
||||
}
|
||||
|
||||
function formatParams(data){
|
||||
var arr=[];
|
||||
for(var name in data){
|
||||
arr.push(encodeURIComponent(name)+'='+encodeURIComponent(data[name]));
|
||||
}
|
||||
return arr.join('&');
|
||||
}
|
||||
@@ -1,746 +0,0 @@
|
||||
|
||||
function Push(options) {
|
||||
this.doNotConnect = 0;
|
||||
options = options || {};
|
||||
options.heartbeat = options.heartbeat || 25000;
|
||||
options.pingTimeout = options.pingTimeout || 10000;
|
||||
this.config = options;
|
||||
this.uid = 0;
|
||||
this.channels = {};
|
||||
this.connection = null;
|
||||
this.pingTimeoutTimer = 0;
|
||||
Push.instances.push(this);
|
||||
this.createConnection();
|
||||
}
|
||||
|
||||
Push.prototype.checkoutPing = function() {
|
||||
var _this = this;
|
||||
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
|
||||
_this.checkoutPingTimer = setTimeout(function () {
|
||||
_this.checkoutPingTimer = 0;
|
||||
if (_this.connection.state === 'connected') {
|
||||
_this.connection.send('{"event":"pusher:ping","data":{}}');
|
||||
if (_this.pingTimeoutTimer) {
|
||||
clearTimeout(_this.pingTimeoutTimer);
|
||||
_this.pingTimeoutTimer = 0;
|
||||
}
|
||||
_this.pingTimeoutTimer = setTimeout(function () {
|
||||
_this.connection.closeAndClean();
|
||||
if (!_this.connection.doNotConnect) {
|
||||
_this.connection.waitReconnect();
|
||||
}
|
||||
}, _this.config.pingTimeout);
|
||||
}
|
||||
}, this.config.heartbeat);
|
||||
};
|
||||
|
||||
Push.prototype.channel = function (name) {
|
||||
return this.channels.find(name);
|
||||
};
|
||||
Push.prototype.allChannels = function () {
|
||||
return this.channels.all();
|
||||
};
|
||||
Push.prototype.createConnection = function () {
|
||||
if (this.connection) {
|
||||
throw Error('Connection already exist');
|
||||
}
|
||||
var _this = this;
|
||||
var url = this.config.url;
|
||||
function updateSubscribed () {
|
||||
for (var i in _this.channels) {
|
||||
_this.channels[i].subscribed = false;
|
||||
}
|
||||
}
|
||||
this.connection = new Connection({
|
||||
url: url,
|
||||
app_key: this.config.app_key,
|
||||
onOpen: function () {
|
||||
_this.connection.state ='connecting';
|
||||
_this.checkoutPing();
|
||||
},
|
||||
onMessage: function(params) {
|
||||
if(_this.pingTimeoutTimer) {
|
||||
clearTimeout(_this.pingTimeoutTimer);
|
||||
_this.pingTimeoutTimer = 0;
|
||||
}
|
||||
|
||||
params = JSON.parse(params.data);
|
||||
var event = params.event;
|
||||
var channel_name = params.channel;
|
||||
|
||||
if (event === 'pusher:pong') {
|
||||
_this.checkoutPing();
|
||||
return;
|
||||
}
|
||||
if (event === 'pusher:error') {
|
||||
throw Error(params.data.message);
|
||||
}
|
||||
var data = JSON.parse(params.data), channel;
|
||||
if (event === 'pusher_internal:subscription_succeeded') {
|
||||
channel = _this.channels[channel_name];
|
||||
channel.subscribed = true;
|
||||
channel.processQueue();
|
||||
channel.emit('pusher:subscription_succeeded');
|
||||
return;
|
||||
}
|
||||
if (event === 'pusher:connection_established') {
|
||||
_this.connection.socket_id = data.socket_id;
|
||||
_this.connection.updateNetworkState('connected');
|
||||
_this.subscribeAll();
|
||||
}
|
||||
if (event.indexOf('pusher_internal') !== -1) {
|
||||
console.log("Event '"+event+"' not implement");
|
||||
return;
|
||||
}
|
||||
channel = _this.channels[channel_name];
|
||||
if (channel) {
|
||||
channel.emit(event, data);
|
||||
}
|
||||
},
|
||||
onClose: function () {
|
||||
updateSubscribed();
|
||||
},
|
||||
onError: function () {
|
||||
updateSubscribed();
|
||||
}
|
||||
});
|
||||
};
|
||||
Push.prototype.disconnect = function () {
|
||||
this.connection.doNotConnect = 1;
|
||||
this.connection.close();
|
||||
};
|
||||
|
||||
Push.prototype.subscribeAll = function () {
|
||||
if (this.connection.state !== 'connected') {
|
||||
return;
|
||||
}
|
||||
for (var channel_name in this.channels) {
|
||||
//this.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
|
||||
this.channels[channel_name].processSubscribe();
|
||||
}
|
||||
};
|
||||
|
||||
Push.prototype.unsubscribe = function (channel_name) {
|
||||
if (this.channels[channel_name]) {
|
||||
delete this.channels[channel_name];
|
||||
if (this.connection.state === 'connected') {
|
||||
this.connection.send(JSON.stringify({event:"pusher:unsubscribe", data:{channel:channel_name}}));
|
||||
}
|
||||
}
|
||||
};
|
||||
Push.prototype.unsubscribeAll = function () {
|
||||
var channels = Object.keys(this.channels);
|
||||
if (channels.length) {
|
||||
if (this.connection.state === 'connected') {
|
||||
for (var channel_name in this.channels) {
|
||||
this.unsubscribe(channel_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.channels = {};
|
||||
};
|
||||
Push.prototype.subscribe = function (channel_name) {
|
||||
if (this.channels[channel_name]) {
|
||||
return this.channels[channel_name];
|
||||
}
|
||||
if (channel_name.indexOf('private-') === 0) {
|
||||
return createPrivateChannel(channel_name, this);
|
||||
}
|
||||
if (channel_name.indexOf('presence-') === 0) {
|
||||
return createPresenceChannel(channel_name, this);
|
||||
}
|
||||
return createChannel(channel_name, this);
|
||||
};
|
||||
Push.instances = [];
|
||||
|
||||
function createChannel(channel_name, push)
|
||||
{
|
||||
var channel = new Channel(push.connection, channel_name);
|
||||
push.channels[channel_name] = channel;
|
||||
channel.subscribeCb = function () {
|
||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
|
||||
}
|
||||
channel.processSubscribe();
|
||||
return channel;
|
||||
}
|
||||
|
||||
function createPrivateChannel(channel_name, push)
|
||||
{
|
||||
var channel = new Channel(push.connection, channel_name);
|
||||
push.channels[channel_name] = channel;
|
||||
channel.subscribeCb = function () {
|
||||
__ajax({
|
||||
url: push.config.auth,
|
||||
type: 'POST',
|
||||
data: {channel_name: channel_name, socket_id: push.connection.socket_id},
|
||||
success: function (data) {
|
||||
data = JSON.parse(data);
|
||||
data.channel = channel_name;
|
||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:data}));
|
||||
},
|
||||
error: function (e) {
|
||||
throw Error(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
channel.processSubscribe();
|
||||
return channel;
|
||||
}
|
||||
|
||||
function createPresenceChannel(channel_name, push)
|
||||
{
|
||||
return createPrivateChannel(channel_name, push);
|
||||
}
|
||||
|
||||
/*window.addEventListener('online', function(){
|
||||
var con;
|
||||
for (var i in Push.instances) {
|
||||
con = Push.instances[i].connection;
|
||||
con.reconnectInterval = 1;
|
||||
if (con.state === 'connecting') {
|
||||
con.connect();
|
||||
}
|
||||
}
|
||||
});*/
|
||||
|
||||
|
||||
function Connection(options) {
|
||||
this.dispatcher = new Dispatcher();
|
||||
__extends(this, this.dispatcher);
|
||||
var properies = ['on', 'off', 'emit'];
|
||||
for (var i in properies) {
|
||||
this[properies[i]] = this.dispatcher[properies[i]];
|
||||
}
|
||||
this.options = options;
|
||||
this.state = 'initialized'; //initialized connecting connected disconnected
|
||||
this.doNotConnect = 0;
|
||||
this.reconnectInterval = 1;
|
||||
this.connection = null;
|
||||
this.reconnectTimer = 0;
|
||||
this.connect();
|
||||
}
|
||||
|
||||
Connection.prototype.updateNetworkState = function(state){
|
||||
var old_state = this.state;
|
||||
this.state = state;
|
||||
if (old_state !== state) {
|
||||
this.emit('state_change', { previous: old_state, current: state });
|
||||
}
|
||||
};
|
||||
|
||||
Connection.prototype.connect = function () {
|
||||
this.doNotConnect = 0;
|
||||
if (this.state === 'connected') {
|
||||
console.log('networkState is "' + this.state + '" and do not need connect');
|
||||
return;
|
||||
}
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
this.reconnectTimer = 0;
|
||||
}
|
||||
|
||||
this.closeAndClean();
|
||||
|
||||
var options = this.options;
|
||||
var websocket = new WebSocket(options.url+'/app/'+options.app_key);
|
||||
|
||||
this.updateNetworkState('connecting');
|
||||
|
||||
var _this = this;
|
||||
websocket.onopen = function (res) {
|
||||
_this.reconnectInterval = 1;
|
||||
if (_this.doNotConnect) {
|
||||
_this.updateNetworkState('disconnected');
|
||||
websocket.close();
|
||||
return;
|
||||
}
|
||||
if (options.onOpen) {
|
||||
options.onOpen(res);
|
||||
}
|
||||
};
|
||||
|
||||
if (options.onMessage) {
|
||||
websocket.onmessage = options.onMessage;
|
||||
}
|
||||
|
||||
websocket.onclose = function (res) {
|
||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
||||
_this.updateNetworkState('disconnected');
|
||||
if (!_this.doNotConnect) {
|
||||
_this.waitReconnect();
|
||||
}
|
||||
if (options.onClose) {
|
||||
options.onClose(res);
|
||||
}
|
||||
};
|
||||
|
||||
websocket.onerror = function (res) {
|
||||
_this.close();
|
||||
if (!_this.doNotConnect) {
|
||||
_this.waitReconnect();
|
||||
}
|
||||
if (options.onError) {
|
||||
options.onError(res);
|
||||
}
|
||||
};
|
||||
this.connection = websocket;
|
||||
}
|
||||
|
||||
Connection.prototype.closeAndClean = function () {
|
||||
if(this.connection) {
|
||||
var websocket = this.connection;
|
||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
||||
try {
|
||||
websocket.close();
|
||||
} catch (e) {}
|
||||
this.updateNetworkState('disconnected');
|
||||
}
|
||||
};
|
||||
|
||||
Connection.prototype.waitReconnect = function () {
|
||||
if (this.state === 'connected' || this.state === 'connecting') {
|
||||
return;
|
||||
}
|
||||
if (!this.doNotConnect) {
|
||||
this.updateNetworkState('connecting');
|
||||
var _this = this;
|
||||
if (this.reconnectTimer) {
|
||||
clearTimeout(this.reconnectTimer);
|
||||
}
|
||||
this.reconnectTimer = setTimeout(function(){
|
||||
_this.connect();
|
||||
}, this.reconnectInterval);
|
||||
if (this.reconnectInterval < 1000) {
|
||||
this.reconnectInterval = 1000;
|
||||
} else {
|
||||
// 每次重连间隔增大一倍
|
||||
this.reconnectInterval = this.reconnectInterval * 2;
|
||||
}
|
||||
// 有网络的状态下,重连间隔最大2秒
|
||||
if (this.reconnectInterval > 2000 && navigator.onLine) {
|
||||
_this.reconnectInterval = 2000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Connection.prototype.send = function(data) {
|
||||
if (this.state !== 'connected') {
|
||||
console.trace('networkState is "' + this.state + '", can not send ' + data);
|
||||
return;
|
||||
}
|
||||
this.connection.send(data);
|
||||
}
|
||||
|
||||
Connection.prototype.close = function(){
|
||||
this.updateNetworkState('disconnected');
|
||||
this.connection.close();
|
||||
}
|
||||
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) {d[p] = b[p];}
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
|
||||
function Channel(connection, channel_name) {
|
||||
this.subscribed = false;
|
||||
this.dispatcher = new Dispatcher();
|
||||
this.connection = connection;
|
||||
this.channelName = channel_name;
|
||||
this.subscribeCb = null;
|
||||
this.queue = [];
|
||||
__extends(this, this.dispatcher);
|
||||
var properies = ['on', 'off', 'emit'];
|
||||
for (var i in properies) {
|
||||
this[properies[i]] = this.dispatcher[properies[i]];
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.processSubscribe = function () {
|
||||
if (this.connection.state !== 'connected') {
|
||||
return;
|
||||
}
|
||||
this.subscribeCb();
|
||||
};
|
||||
|
||||
Channel.prototype.processQueue = function () {
|
||||
if (this.connection.state !== 'connected' || !this.subscribed) {
|
||||
return;
|
||||
}
|
||||
for (var i in this.queue) {
|
||||
this.queue[i]();
|
||||
}
|
||||
this.queue = [];
|
||||
};
|
||||
|
||||
Channel.prototype.trigger = function (event, data) {
|
||||
if (event.indexOf('client-') !== 0) {
|
||||
throw new Error("Event '" + event + "' should start with 'client-'");
|
||||
}
|
||||
var _this = this;
|
||||
this.queue.push(function () {
|
||||
_this.connection.send(JSON.stringify({ event: event, data: data, channel: _this.channelName }));
|
||||
});
|
||||
this.processQueue();
|
||||
};
|
||||
|
||||
////////////////
|
||||
var Collections = (function () {
|
||||
var exports = {};
|
||||
function extend(target) {
|
||||
var sources = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
sources[_i - 1] = arguments[_i];
|
||||
}
|
||||
for (var i = 0; i < sources.length; i++) {
|
||||
var extensions = sources[i];
|
||||
for (var property in extensions) {
|
||||
if (extensions[property] && extensions[property].constructor &&
|
||||
extensions[property].constructor === Object) {
|
||||
target[property] = extend(target[property] || {}, extensions[property]);
|
||||
}
|
||||
else {
|
||||
target[property] = extensions[property];
|
||||
}
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
exports.extend = extend;
|
||||
function stringify() {
|
||||
var m = ["Push"];
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
if (typeof arguments[i] === "string") {
|
||||
m.push(arguments[i]);
|
||||
}
|
||||
else {
|
||||
m.push(safeJSONStringify(arguments[i]));
|
||||
}
|
||||
}
|
||||
return m.join(" : ");
|
||||
}
|
||||
|
||||
exports.stringify = stringify;
|
||||
function arrayIndexOf(array, item) {
|
||||
var nativeIndexOf = Array.prototype.indexOf;
|
||||
if (array === null) {
|
||||
return -1;
|
||||
}
|
||||
if (nativeIndexOf && array.indexOf === nativeIndexOf) {
|
||||
return array.indexOf(item);
|
||||
}
|
||||
for (var i = 0, l = array.length; i < l; i++) {
|
||||
if (array[i] === item) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
exports.arrayIndexOf = arrayIndexOf;
|
||||
function objectApply(object, f) {
|
||||
for (var key in object) {
|
||||
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
||||
f(object[key], key, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.objectApply = objectApply;
|
||||
function keys(object) {
|
||||
var keys = [];
|
||||
objectApply(object, function (_, key) {
|
||||
keys.push(key);
|
||||
});
|
||||
return keys;
|
||||
}
|
||||
|
||||
exports.keys = keys;
|
||||
function values(object) {
|
||||
var values = [];
|
||||
objectApply(object, function (value) {
|
||||
values.push(value);
|
||||
});
|
||||
return values;
|
||||
}
|
||||
|
||||
exports.values = values;
|
||||
function apply(array, f, context) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
f.call(context || (window), array[i], i, array);
|
||||
}
|
||||
}
|
||||
|
||||
exports.apply = apply;
|
||||
function map(array, f) {
|
||||
var result = [];
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result.push(f(array[i], i, array, result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.map = map;
|
||||
function mapObject(object, f) {
|
||||
var result = {};
|
||||
objectApply(object, function (value, key) {
|
||||
result[key] = f(value);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.mapObject = mapObject;
|
||||
function filter(array, test) {
|
||||
test = test || function (value) {
|
||||
return !!value;
|
||||
};
|
||||
var result = [];
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (test(array[i], i, array, result)) {
|
||||
result.push(array[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.filter = filter;
|
||||
function filterObject(object, test) {
|
||||
var result = {};
|
||||
objectApply(object, function (value, key) {
|
||||
if ((test && test(value, key, object, result)) || Boolean(value)) {
|
||||
result[key] = value;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.filterObject = filterObject;
|
||||
function flatten(object) {
|
||||
var result = [];
|
||||
objectApply(object, function (value, key) {
|
||||
result.push([key, value]);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.flatten = flatten;
|
||||
function any(array, test) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (test(array[i], i, array)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.any = any;
|
||||
function all(array, test) {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
if (!test(array[i], i, array)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
exports.all = all;
|
||||
function encodeParamsObject(data) {
|
||||
return mapObject(data, function (value) {
|
||||
if (typeof value === "object") {
|
||||
value = safeJSONStringify(value);
|
||||
}
|
||||
return encodeURIComponent(base64_1["default"](value.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
exports.encodeParamsObject = encodeParamsObject;
|
||||
function buildQueryString(data) {
|
||||
var params = filterObject(data, function (value) {
|
||||
return value !== undefined;
|
||||
});
|
||||
return map(flatten(encodeParamsObject(params)), util_1["default"].method("join", "=")).join("&");
|
||||
}
|
||||
|
||||
exports.buildQueryString = buildQueryString;
|
||||
function decycleObject(object) {
|
||||
var objects = [], paths = [];
|
||||
return (function derez(value, path) {
|
||||
var i, name, nu;
|
||||
switch (typeof value) {
|
||||
case 'object':
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
for (i = 0; i < objects.length; i += 1) {
|
||||
if (objects[i] === value) {
|
||||
return {$ref: paths[i]};
|
||||
}
|
||||
}
|
||||
objects.push(value);
|
||||
paths.push(path);
|
||||
if (Object.prototype.toString.apply(value) === '[object Array]') {
|
||||
nu = [];
|
||||
for (i = 0; i < value.length; i += 1) {
|
||||
nu[i] = derez(value[i], path + '[' + i + ']');
|
||||
}
|
||||
}
|
||||
else {
|
||||
nu = {};
|
||||
for (name in value) {
|
||||
if (Object.prototype.hasOwnProperty.call(value, name)) {
|
||||
nu[name] = derez(value[name], path + '[' + JSON.stringify(name) + ']');
|
||||
}
|
||||
}
|
||||
}
|
||||
return nu;
|
||||
case 'number':
|
||||
case 'string':
|
||||
case 'boolean':
|
||||
return value;
|
||||
}
|
||||
}(object, '$'));
|
||||
}
|
||||
|
||||
exports.decycleObject = decycleObject;
|
||||
function safeJSONStringify(source) {
|
||||
try {
|
||||
return JSON.stringify(source);
|
||||
}
|
||||
catch (e) {
|
||||
return JSON.stringify(decycleObject(source));
|
||||
}
|
||||
}
|
||||
|
||||
exports.safeJSONStringify = safeJSONStringify;
|
||||
return exports;
|
||||
})();
|
||||
|
||||
var Dispatcher = (function () {
|
||||
function Dispatcher(failThrough) {
|
||||
this.callbacks = new CallbackRegistry();
|
||||
this.global_callbacks = [];
|
||||
this.failThrough = failThrough;
|
||||
}
|
||||
Dispatcher.prototype.on = function (eventName, callback, context) {
|
||||
this.callbacks.add(eventName, callback, context);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.on_global = function (callback) {
|
||||
this.global_callbacks.push(callback);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.off = function (eventName, callback, context) {
|
||||
this.callbacks.remove(eventName, callback, context);
|
||||
return this;
|
||||
};
|
||||
Dispatcher.prototype.emit = function (eventName, data) {
|
||||
var i;
|
||||
for (i = 0; i < this.global_callbacks.length; i++) {
|
||||
this.global_callbacks[i](eventName, data);
|
||||
}
|
||||
var callbacks = this.callbacks.get(eventName);
|
||||
if (callbacks && callbacks.length > 0) {
|
||||
for (i = 0; i < callbacks.length; i++) {
|
||||
callbacks[i].fn.call(callbacks[i].context || (window), data);
|
||||
}
|
||||
}
|
||||
else if (this.failThrough) {
|
||||
this.failThrough(eventName, data);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
return Dispatcher;
|
||||
}());
|
||||
|
||||
var CallbackRegistry = (function () {
|
||||
function CallbackRegistry() {
|
||||
this._callbacks = {};
|
||||
}
|
||||
CallbackRegistry.prototype.get = function (name) {
|
||||
return this._callbacks[prefix(name)];
|
||||
};
|
||||
CallbackRegistry.prototype.add = function (name, callback, context) {
|
||||
var prefixedEventName = prefix(name);
|
||||
this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || [];
|
||||
this._callbacks[prefixedEventName].push({
|
||||
fn: callback,
|
||||
context: context
|
||||
});
|
||||
};
|
||||
CallbackRegistry.prototype.remove = function (name, callback, context) {
|
||||
if (!name && !callback && !context) {
|
||||
this._callbacks = {};
|
||||
return;
|
||||
}
|
||||
var names = name ? [prefix(name)] : Collections.keys(this._callbacks);
|
||||
if (callback || context) {
|
||||
this.removeCallback(names, callback, context);
|
||||
}
|
||||
else {
|
||||
this.removeAllCallbacks(names);
|
||||
}
|
||||
};
|
||||
CallbackRegistry.prototype.removeCallback = function (names, callback, context) {
|
||||
Collections.apply(names, function (name) {
|
||||
this._callbacks[name] = Collections.filter(this._callbacks[name] || [], function (oning) {
|
||||
return (callback && callback !== oning.fn) ||
|
||||
(context && context !== oning.context);
|
||||
});
|
||||
if (this._callbacks[name].length === 0) {
|
||||
delete this._callbacks[name];
|
||||
}
|
||||
}, this);
|
||||
};
|
||||
CallbackRegistry.prototype.removeAllCallbacks = function (names) {
|
||||
Collections.apply(names, function (name) {
|
||||
delete this._callbacks[name];
|
||||
}, this);
|
||||
};
|
||||
return CallbackRegistry;
|
||||
}());
|
||||
function prefix(name) {
|
||||
return "_" + name;
|
||||
}
|
||||
|
||||
function __ajax(options){
|
||||
options=options||{};
|
||||
options.type=(options.type||'GET').toUpperCase();
|
||||
options.dataType=options.dataType||'json';
|
||||
var params=formatParams(options.data);
|
||||
|
||||
var xhr;
|
||||
if(window.XMLHttpRequest){
|
||||
xhr=new XMLHttpRequest();
|
||||
}else{
|
||||
xhr=ActiveXObject('Microsoft.XMLHTTP');
|
||||
}
|
||||
|
||||
xhr.onreadystatechange=function(){
|
||||
if(xhr.readyState === 4){
|
||||
var status=xhr.status;
|
||||
if(status>=200 && status<300){
|
||||
options.success&&options.success(xhr.responseText,xhr.responseXML);
|
||||
}else{
|
||||
options.error&&options.error(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(options.type==='GET'){
|
||||
xhr.open('GET',options.url+'?'+params,true);
|
||||
xhr.send(null);
|
||||
}else if(options.type==='POST'){
|
||||
xhr.open('POST',options.url,true);
|
||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||
xhr.send(params);
|
||||
}
|
||||
}
|
||||
|
||||
function formatParams(data){
|
||||
var arr=[];
|
||||
for(var name in data){
|
||||
arr.push(encodeURIComponent(name)+'='+encodeURIComponent(data[name]));
|
||||
}
|
||||
return arr.join('&');
|
||||
}
|
||||
@@ -9,8 +9,6 @@ export default {
|
||||
'/': ['./frontend/${lang}/index.ts'],
|
||||
[adminBaseRoutePath + '/moduleStore']: ['./backend/${lang}/module.ts'],
|
||||
[adminBaseRoutePath + '/crud/crud']: ['./backend/${lang}/crud/log.ts', './backend/${lang}/crud/state.ts'],
|
||||
/** 推送测试三页:共享 test.push.* 文案(见 PushChannelTestPage.vue) */
|
||||
[adminBaseRoutePath + '/test/pushGamePeriod']: ['./backend/${lang}/test/push.ts'],
|
||||
[adminBaseRoutePath + '/test/pushOperationNotice']: ['./backend/${lang}/test/push.ts'],
|
||||
[adminBaseRoutePath + '/test/pushPrivateUser']: ['./backend/${lang}/test/push.ts'],
|
||||
/** 测试页:WebSocket 单入口(见 WebSocketTestPage.vue) */
|
||||
[adminBaseRoutePath + '/test/gameCurrentStatus']: ['./backend/${lang}/test/ws.ts'],
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ export default {
|
||||
btn_draw: 'Schedule draw',
|
||||
calc_result_number: 'Calculated number',
|
||||
calc_estimated_loss: 'Estimated payout',
|
||||
push_connected: 'Push connected, realtime updates running',
|
||||
push_disconnected: 'Push disconnected, please check service status',
|
||||
push_connected: 'Realtime connection established',
|
||||
push_disconnected: 'Polling mode enabled (push removed)',
|
||||
candidate_title: 'Candidate payout estimates',
|
||||
number: 'Number',
|
||||
estimated_loss: 'Estimated payout',
|
||||
|
||||
3
web/src/lang/backend/en/test/gameCurrentStatus.ts
Normal file
3
web/src/lang/backend/en/test/gameCurrentStatus.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default {
|
||||
tip: 'WebSocket connection test for status stream: listen to period.tick / period.opened events.',
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export default {
|
||||
connected: 'Push connected (subscribed)',
|
||||
disconnected: 'Disconnected or idle',
|
||||
user_uuid: 'User uuid',
|
||||
user_uuid_placeholder: '10-char public id as in mobile profile',
|
||||
btn_connect: 'Connect & subscribe',
|
||||
btn_disconnect: 'Disconnect',
|
||||
btn_clear: 'Clear log',
|
||||
channel_label: 'Channel',
|
||||
log_title: 'Event log',
|
||||
log_empty: 'No messages yet. Ensure the push worker is running and the server publishes to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to public-game-period (global period channel) for period.tick / period.locked / period.opened / period.payout / jackpot.hit. The server must publish to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to public-operation-notice for broadcast notices such as notice.popout. The server must publish to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to private-user-{uuid}: enter the player uuid; auth uses /plugin/webman/push/auth like the mobile client. For bet.accepted, wallet.changed, etc.',
|
||||
}
|
||||
16
web/src/lang/backend/en/test/ws.ts
Normal file
16
web/src/lang/backend/en/test/ws.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export default {
|
||||
ws_connected: 'WebSocket connected',
|
||||
ws_disconnected: 'WebSocket disconnected',
|
||||
config_api: 'Config API',
|
||||
ws_url: 'WebSocket URL',
|
||||
ws_url_placeholder: 'Input ws:// or wss:// URL',
|
||||
send_message: 'Send message',
|
||||
btn_load_config: 'Load config',
|
||||
btn_connect: 'Connect',
|
||||
btn_disconnect: 'Disconnect',
|
||||
btn_send: 'Send message',
|
||||
btn_clear: 'Clear log',
|
||||
log_title: 'WebSocket log',
|
||||
log_empty: 'No logs yet. Connect first and then send a message.',
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ export default {
|
||||
btn_draw: '预约开奖',
|
||||
calc_result_number: '计算开奖号码',
|
||||
calc_estimated_loss: '计算预估赔付',
|
||||
push_connected: '推送服务已连接,页面数据实时更新中',
|
||||
push_disconnected: '推送服务连接中断,请检查服务是否启动',
|
||||
push_connected: '实时连接已建立',
|
||||
push_disconnected: '已切换为轮询模式(无推送)',
|
||||
candidate_title: '候选号码赔付预估',
|
||||
number: '号码',
|
||||
estimated_loss: '预估赔付',
|
||||
|
||||
3
web/src/lang/backend/zh-cn/test/gameCurrentStatus.ts
Normal file
3
web/src/lang/backend/zh-cn/test/gameCurrentStatus.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default {
|
||||
tip: 'WebSocket 连接测试(状态流):按文档监听 period.tick / period.opened 等事件。',
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export default {
|
||||
connected: '推送服务已连接(已订阅频道)',
|
||||
disconnected: '未连接或已断开',
|
||||
user_uuid: '用户 uuid',
|
||||
user_uuid_placeholder: '与移动端档案一致的 10 位对外标识',
|
||||
btn_connect: '连接并订阅',
|
||||
btn_disconnect: '断开',
|
||||
btn_clear: '清空日志',
|
||||
channel_label: '当前频道',
|
||||
log_title: '事件日志',
|
||||
log_empty: '暂无推送,请确认 push 进程已启动且服务端会向对应频道发消息。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅文档中的「全局对局频道」public-game-period,用于验证 period.tick / period.locked / period.opened / period.payout / jackpot.hit 等公共事件(需服务端向该频道推送)。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅「公告广播频道」public-operation-notice:全站公告 notice.popout 与本游戏 jackpot.hit(连胜大奖广播)均会发往此频道;需服务端推送。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅「用户私有频道」private-user-{uuid}:请输入玩家 uuid,连接后将走 /plugin/webman/push/auth 鉴权(与移动端一致)。用于验证 bet.accepted、wallet.changed 等私有事件。',
|
||||
}
|
||||
16
web/src/lang/backend/zh-cn/test/ws.ts
Normal file
16
web/src/lang/backend/zh-cn/test/ws.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export default {
|
||||
ws_connected: 'WebSocket 已连接',
|
||||
ws_disconnected: 'WebSocket 未连接',
|
||||
config_api: '配置接口',
|
||||
ws_url: 'WebSocket 地址',
|
||||
ws_url_placeholder: '请输入 ws:// 或 wss:// 地址',
|
||||
send_message: '发送消息',
|
||||
btn_load_config: '加载测试配置',
|
||||
btn_connect: '连接',
|
||||
btn_disconnect: '断开',
|
||||
btn_send: '发送消息',
|
||||
btn_clear: '清空日志',
|
||||
log_title: 'WebSocket 日志',
|
||||
log_empty: '暂无日志,请先连接后发送消息。',
|
||||
}
|
||||
|
||||
@@ -35,33 +35,6 @@ export const getUrl = (): string => {
|
||||
return value == 'getCurrentDomain' ? window.location.protocol + '//' + window.location.host : value
|
||||
}
|
||||
|
||||
/**
|
||||
* Webman Push 在 API 上的 HTTP 根地址(私有频道 POST /plugin/webman/push/auth 等)。
|
||||
* 与 VITE_AXIOS_BASE_URL 一致;开发环境为空时用当前页面源,配合 Vite 对 /plugin 的代理。
|
||||
*/
|
||||
export function getPushHttpBase(): string {
|
||||
const api = getUrl()
|
||||
if (api) {
|
||||
return api.replace(/\/$/, '')
|
||||
}
|
||||
return window.location.protocol + '//' + window.location.host
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载 push.js 的绝对 URL:文件放在 web/public/plugin/webman/push/,随 dist 与后台同源发布,
|
||||
* 不依赖 API 站点是否暴露 /plugin/webman/push/push.js。
|
||||
*/
|
||||
export function getPushScriptUrl(): string {
|
||||
let base = import.meta.env.BASE_URL
|
||||
if (typeof base !== 'string' || base === '') {
|
||||
base = '/'
|
||||
}
|
||||
if (!base.endsWith('/')) {
|
||||
base = base + '/'
|
||||
}
|
||||
return window.location.origin + base + 'plugin/webman/push/push.js'
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据运行环境获取基础请求URL的端口
|
||||
*/
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* 后台推送频道测试:加载官方 push.js 并订阅频道、监听文档约定事件
|
||||
*/
|
||||
|
||||
import { getPushHttpBase, getPushScriptUrl } from '/@/utils/axios'
|
||||
|
||||
const DOC_EVENTS = [
|
||||
'period.tick',
|
||||
'period.locked',
|
||||
'period.opened',
|
||||
'period.payout',
|
||||
'jackpot.hit',
|
||||
'bet.accepted',
|
||||
'bet.settled',
|
||||
'wallet.changed',
|
||||
'notice.popout',
|
||||
'withdraw.review_required',
|
||||
]
|
||||
|
||||
export async function loadPushJs(): Promise<void> {
|
||||
if ((window as any).Push) {
|
||||
return
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = getPushScriptUrl()
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('load push.js failed'))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
export type PushTestLogLine = { t: number; event: string; payload: string }
|
||||
|
||||
export function startPushChannelListener(options: {
|
||||
url: string
|
||||
app_key: string
|
||||
channel: string
|
||||
/** 私有频道需走 /plugin/webman/push/auth */
|
||||
usePrivateAuth: boolean
|
||||
onLog: (line: PushTestLogLine) => void
|
||||
onConnected: (ok: boolean) => void
|
||||
}): { disconnect: () => void } {
|
||||
const PushCtor = (window as any).Push
|
||||
const cfg: anyObj = { url: options.url, app_key: options.app_key }
|
||||
if (options.usePrivateAuth) {
|
||||
cfg.auth = `${getPushHttpBase()}/plugin/webman/push/auth`
|
||||
}
|
||||
const client = new PushCtor(cfg)
|
||||
const ch = client.subscribe(options.channel)
|
||||
|
||||
const pushLog = (event: string, data: unknown) => {
|
||||
let payload = ''
|
||||
try {
|
||||
payload = typeof data === 'string' ? data : JSON.stringify(data)
|
||||
} catch {
|
||||
payload = String(data)
|
||||
}
|
||||
options.onLog({ t: Date.now(), event, payload })
|
||||
}
|
||||
|
||||
ch.on('pusher:subscription_succeeded', () => {
|
||||
options.onConnected(true)
|
||||
pushLog('pusher:subscription_succeeded', {})
|
||||
})
|
||||
|
||||
for (const ev of DOC_EVENTS) {
|
||||
ch.on(ev, (data: unknown) => {
|
||||
options.onConnected(true)
|
||||
pushLog(ev, data)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
disconnect: () => {
|
||||
try {
|
||||
client.disconnect()
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
options.onConnected(false)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="t('game.live.tip')" show-icon class="mb-12" />
|
||||
<el-alert :type="pushConnected ? 'success' : 'error'" :title="pushConnected ? t('game.live.push_connected') : t('game.live.push_disconnected')" show-icon class="mb-12" />
|
||||
<el-alert type="info" :title="t('game.live.push_disconnected')" show-icon class="mb-12" />
|
||||
<el-alert
|
||||
v-if="snapshot.runtime_enabled === false && !snapshot.maintenance_ui"
|
||||
type="warning"
|
||||
@@ -190,7 +190,7 @@
|
||||
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import createAxios, { getPushScriptUrl } from '/@/utils/axios'
|
||||
import createAxios from '/@/utils/axios'
|
||||
|
||||
interface Snapshot {
|
||||
record: anyObj | null
|
||||
@@ -219,7 +219,6 @@ interface Snapshot {
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const pushConnected = ref(false)
|
||||
const snapshot = reactive<Snapshot>({
|
||||
record: null,
|
||||
bets: [],
|
||||
@@ -256,10 +255,7 @@ const serverSkewSeconds = ref(0)
|
||||
const clockTick = ref(0)
|
||||
let clockTimer: number | null = null
|
||||
|
||||
let pushClient: any = null
|
||||
let pushChannel: any = null
|
||||
let pollTimer: number | null = null
|
||||
let pushWatchdogTimer: number | null = null
|
||||
|
||||
function formatPicks(v: unknown): string {
|
||||
if (Array.isArray(v)) return JSON.stringify(v)
|
||||
@@ -417,59 +413,6 @@ async function submitVoidPeriod(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function initPush() {
|
||||
const cfgRes = await createAxios({ url: '/admin/game.Live/pushConfig', method: 'get', showCodeMessage: false })
|
||||
if (cfgRes.code !== 1 || !cfgRes.data) {
|
||||
pushConnected.value = false
|
||||
return
|
||||
}
|
||||
const { url, app_key, channel, event } = cfgRes.data
|
||||
|
||||
try {
|
||||
await loadPushJs()
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
return
|
||||
}
|
||||
const PushCtor = (window as any).Push
|
||||
if (!PushCtor) {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
return
|
||||
}
|
||||
try {
|
||||
pushClient = new PushCtor({ url, app_key })
|
||||
pushChannel = pushClient.subscribe(channel)
|
||||
pushConnected.value = false
|
||||
pushChannel.on('pusher:subscription_succeeded', () => {
|
||||
pushConnected.value = true
|
||||
})
|
||||
startPushWatchdog()
|
||||
stopPolling()
|
||||
pushChannel.on(event, (payload: anyObj) => {
|
||||
pushConnected.value = true
|
||||
mergeLiveSnapshot(payload)
|
||||
})
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPushJs() {
|
||||
if ((window as any).Push) {
|
||||
return
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = getPushScriptUrl()
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('load push.js failed'))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
async function onCalculate() {
|
||||
if (!snapshot.record) return
|
||||
calcLoading.value = true
|
||||
@@ -528,24 +471,11 @@ onMounted(async () => {
|
||||
clockTick.value++
|
||||
}, 1000)
|
||||
await loadSnapshot()
|
||||
try {
|
||||
await initPush()
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
}
|
||||
startPolling()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
try {
|
||||
if (pushClient && typeof pushClient.disconnect === 'function') {
|
||||
pushClient.disconnect()
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
stopPolling()
|
||||
stopPushWatchdog()
|
||||
if (clockTimer !== null) {
|
||||
window.clearInterval(clockTimer)
|
||||
clockTimer = null
|
||||
@@ -568,30 +498,6 @@ function stopPolling() {
|
||||
}
|
||||
}
|
||||
|
||||
function startPushWatchdog() {
|
||||
if (pushWatchdogTimer !== null) {
|
||||
return
|
||||
}
|
||||
pushWatchdogTimer = window.setInterval(() => {
|
||||
if (!pushClient || !pushClient.connection) {
|
||||
pushConnected.value = false
|
||||
return
|
||||
}
|
||||
const state = String(pushClient.connection.state || '')
|
||||
// push.js:onOpen 后长期为 connecting,必须收到 pusher:connection_established 才变 connected。
|
||||
// 把 connecting 当「已连接」会误报绿条(Nginx 只完成握手、帧未透传时 DevTools 消息列表为空)。
|
||||
if (state === 'disconnected') {
|
||||
pushConnected.value = false
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
function stopPushWatchdog() {
|
||||
if (pushWatchdogTimer !== null) {
|
||||
window.clearInterval(pushWatchdogTimer)
|
||||
pushWatchdogTimer = null
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="tip" show-icon class="mb-12" />
|
||||
<el-alert :type="connected ? 'success' : 'warning'" :title="connected ? t('test.push.connected') : t('test.push.disconnected')" show-icon class="mb-12" />
|
||||
|
||||
<el-card shadow="never" class="mb-12">
|
||||
<el-form :inline="true" @submit.prevent>
|
||||
<el-form-item v-if="useUuid" :label="t('test.push.user_uuid')">
|
||||
<el-input v-model="userUuid" :placeholder="t('test.push.user_uuid_placeholder')" clearable style="width: 280px" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" :loading="loading" @click="connect">{{ t('test.push.btn_connect') }}</el-button>
|
||||
<el-button :disabled="!session" @click="disconnect">{{ t('test.push.btn_disconnect') }}</el-button>
|
||||
<el-button @click="clearLogs">{{ t('test.push.btn_clear') }}</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="text-muted mb-8">{{ t('test.push.channel_label') }}: {{ channelName || '-' }}</div>
|
||||
</el-card>
|
||||
|
||||
<el-card shadow="never">
|
||||
<template #header>{{ t('test.push.log_title') }}</template>
|
||||
<el-scrollbar max-height="480">
|
||||
<pre class="push-log">{{ logText }}</pre>
|
||||
</el-scrollbar>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onUnmounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
import { loadPushJs, startPushChannelListener, type PushTestLogLine } from '/@/utils/backend/pushChannelTest'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 如 /admin/test.PushGamePeriod/pushConfig */
|
||||
configApi: string
|
||||
useUuid?: boolean
|
||||
tip: string
|
||||
}>(),
|
||||
{
|
||||
useUuid: false,
|
||||
}
|
||||
)
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const connected = ref(false)
|
||||
const channelName = ref('')
|
||||
const userUuid = ref('')
|
||||
const logs = ref<PushTestLogLine[]>([])
|
||||
const session = ref<{ disconnect: () => void } | null>(null)
|
||||
|
||||
const logText = computed(() => {
|
||||
if (!logs.value.length) return t('test.push.log_empty')
|
||||
return logs.value
|
||||
.map((row) => {
|
||||
const time = new Date(row.t).toLocaleString()
|
||||
return `[${time}] ${row.event}\n${row.payload}`
|
||||
})
|
||||
.join('\n\n')
|
||||
})
|
||||
|
||||
function appendLog(line: PushTestLogLine) {
|
||||
logs.value = [line, ...logs.value].slice(0, 200)
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
logs.value = []
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
if (props.useUuid && !userUuid.value.trim()) {
|
||||
return
|
||||
}
|
||||
loading.value = true
|
||||
disconnect()
|
||||
try {
|
||||
await loadPushJs()
|
||||
const params: anyObj = {}
|
||||
if (props.useUuid) {
|
||||
params.uuid = userUuid.value.trim()
|
||||
}
|
||||
const res = await createAxios({
|
||||
url: props.configApi,
|
||||
method: 'get',
|
||||
params,
|
||||
showCodeMessage: true,
|
||||
})
|
||||
if (res.code !== 1 || !res.data) {
|
||||
connected.value = false
|
||||
return
|
||||
}
|
||||
const { url, app_key, channel } = res.data
|
||||
channelName.value = typeof channel === 'string' ? channel : ''
|
||||
try {
|
||||
const handle = startPushChannelListener({
|
||||
url,
|
||||
app_key,
|
||||
channel: channelName.value,
|
||||
usePrivateAuth: !!props.useUuid,
|
||||
onLog: appendLog,
|
||||
onConnected: (ok) => {
|
||||
connected.value = ok
|
||||
},
|
||||
})
|
||||
session.value = handle
|
||||
} catch {
|
||||
connected.value = false
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
if (session.value) {
|
||||
session.value.disconnect()
|
||||
session.value = null
|
||||
}
|
||||
connected.value = false
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
disconnect()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.push-log {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
.text-muted {
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
220
web/src/views/backend/test/components/WebSocketTestPage.vue
Normal file
220
web/src/views/backend/test/components/WebSocketTestPage.vue
Normal file
@@ -0,0 +1,220 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="tip" show-icon class="mb-12" />
|
||||
<el-alert :type="connected ? 'success' : 'warning'" :title="connected ? t('test.ws.ws_connected') : t('test.ws.ws_disconnected')" show-icon class="mb-12" />
|
||||
|
||||
<el-card shadow="never" class="mb-12">
|
||||
<el-form @submit.prevent>
|
||||
<el-form-item :label="t('test.ws.config_api')">
|
||||
<el-input :model-value="configApi" readonly />
|
||||
</el-form-item>
|
||||
<el-form-item :label="t('test.ws.ws_url')">
|
||||
<el-input v-model="wsUrl" :placeholder="t('test.ws.ws_url_placeholder')" />
|
||||
</el-form-item>
|
||||
<el-form-item :label="t('test.ws.send_message')">
|
||||
<el-input v-model="sendMessage" type="textarea" :rows="4" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" :loading="loading" @click="loadConfig">{{ t('test.ws.btn_load_config') }}</el-button>
|
||||
<el-button type="success" :disabled="!ready || connected" @click="connectWs">{{ t('test.ws.btn_connect') }}</el-button>
|
||||
<el-button :disabled="!connected" @click="disconnectWs">{{ t('test.ws.btn_disconnect') }}</el-button>
|
||||
<el-button :loading="sending" :disabled="!connected" @click="sendWs">{{ t('test.ws.btn_send') }}</el-button>
|
||||
<el-button @click="clearLogs">{{ t('test.ws.btn_clear') }}</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="text-muted mb-8">{{ connectTip || '-' }}</div>
|
||||
</el-card>
|
||||
|
||||
<el-card shadow="never">
|
||||
<template #header>{{ t('test.ws.log_title') }}</template>
|
||||
<el-scrollbar max-height="480">
|
||||
<pre class="ws-log">{{ logText }}</pre>
|
||||
</el-scrollbar>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onUnmounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 如 /admin/test.GameCurrentStatus/wsConfig */
|
||||
configApi: string
|
||||
tip: string
|
||||
}>(),
|
||||
{}
|
||||
)
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const sending = ref(false)
|
||||
const ready = ref(false)
|
||||
const connected = ref(false)
|
||||
const wsUrl = ref('')
|
||||
const connectTip = ref('')
|
||||
const sendMessage = ref('')
|
||||
/** 连接成功后自动订阅(由 wsConfig.subscribe_topics 下发) */
|
||||
const subscribeTopics = ref<string[]>([])
|
||||
const ws = ref<WebSocket | null>(null)
|
||||
const logs = ref<Array<{ t: number; event: string; payload: string }>>([])
|
||||
|
||||
const defaultSubscribeTopics = [
|
||||
'period.tick',
|
||||
'period.opened',
|
||||
'period.locked',
|
||||
'period.payout',
|
||||
'bet.accepted',
|
||||
'wallet.changed',
|
||||
'auto.spin.progress',
|
||||
] as const
|
||||
|
||||
const logText = computed(() => {
|
||||
if (!logs.value.length) return t('test.ws.log_empty')
|
||||
return logs.value
|
||||
.map((row) => {
|
||||
const time = new Date(row.t).toLocaleString()
|
||||
return `[${time}] ${row.event}\n${row.payload}`
|
||||
})
|
||||
.join('\n\n')
|
||||
})
|
||||
|
||||
function appendLog(line: { t: number; event: string; payload: string }) {
|
||||
logs.value = [line, ...logs.value].slice(0, 200)
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
logs.value = []
|
||||
}
|
||||
|
||||
async function loadConfig() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await createAxios({
|
||||
url: props.configApi,
|
||||
method: 'get',
|
||||
showCodeMessage: true,
|
||||
})
|
||||
if (res.code !== 1 || !res.data) {
|
||||
ready.value = false
|
||||
return
|
||||
}
|
||||
wsUrl.value = String(res.data.ws_url || '')
|
||||
connectTip.value = String(res.data.connect_tip || '')
|
||||
const rawTopics = res.data.subscribe_topics
|
||||
if (Array.isArray(rawTopics)) {
|
||||
subscribeTopics.value = rawTopics.filter((x: unknown): x is string => typeof x === 'string' && x.trim() !== '')
|
||||
} else {
|
||||
subscribeTopics.value = [...defaultSubscribeTopics]
|
||||
}
|
||||
const firstSample = Array.isArray(res.data.sample_messages) && res.data.sample_messages.length ? String(res.data.sample_messages[0]) : ''
|
||||
sendMessage.value = firstSample
|
||||
ready.value = wsUrl.value !== ''
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'config.loaded',
|
||||
payload: JSON.stringify(res.data, null, 2),
|
||||
})
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function connectWs() {
|
||||
if (!ready.value || !wsUrl.value) {
|
||||
return
|
||||
}
|
||||
disconnectWs()
|
||||
const socket = new WebSocket(wsUrl.value)
|
||||
ws.value = socket
|
||||
socket.onopen = () => {
|
||||
connected.value = true
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.open',
|
||||
payload: wsUrl.value,
|
||||
})
|
||||
const topics = subscribeTopics.value.length > 0 ? subscribeTopics.value : [...defaultSubscribeTopics]
|
||||
const subscribePayload = JSON.stringify({ action: 'subscribe', topics })
|
||||
socket.send(subscribePayload)
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.auto_subscribe',
|
||||
payload: subscribePayload,
|
||||
})
|
||||
}
|
||||
socket.onmessage = (event) => {
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.message',
|
||||
payload: typeof event.data === 'string' ? event.data : JSON.stringify(event.data),
|
||||
})
|
||||
}
|
||||
socket.onerror = () => {
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.error',
|
||||
payload: 'WebSocket error',
|
||||
})
|
||||
}
|
||||
socket.onclose = () => {
|
||||
connected.value = false
|
||||
ws.value = null
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.close',
|
||||
payload: wsUrl.value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function disconnectWs() {
|
||||
if (ws.value) {
|
||||
ws.value.close()
|
||||
ws.value = null
|
||||
}
|
||||
connected.value = false
|
||||
}
|
||||
|
||||
function sendWs() {
|
||||
if (!ws.value || !connected.value) {
|
||||
return
|
||||
}
|
||||
const payload = sendMessage.value.trim()
|
||||
if (!payload) {
|
||||
return
|
||||
}
|
||||
sending.value = true
|
||||
try {
|
||||
ws.value.send(payload)
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.send',
|
||||
payload,
|
||||
})
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
disconnectWs()
|
||||
clearLogs()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.ws-log {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
.text-muted {
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
11
web/src/views/backend/test/gameCurrentStatus/index.vue
Normal file
11
web/src/views/backend/test/gameCurrentStatus/index.vue
Normal file
@@ -0,0 +1,11 @@
|
||||
<template>
|
||||
<WebSocketTestPage config-api="/admin/test.GameCurrentStatus/wsConfig" :tip="t('test.gameCurrentStatus.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import WebSocketTestPage from '../components/WebSocketTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage config-api="/admin/test.PushGamePeriod/pushConfig" :tip="t('test.pushGamePeriod.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
@@ -1,10 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage config-api="/admin/test.PushOperationNotice/pushConfig" :tip="t('test.pushOperationNotice.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
@@ -1,14 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage
|
||||
config-api="/admin/test.PushPrivateUser/pushConfig"
|
||||
use-uuid
|
||||
:tip="t('test.pushPrivateUser.tip')"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
Reference in New Issue
Block a user