feat(online): 增强客户端连接稳定性#3895
Open
zhpy2004 wants to merge 7 commits into
Open
Conversation
- game.send() 增加 WebSocket readyState 检查,避免 CLOSING/CLOSED 时 send 抛 InvalidStateError - game.connect() 增加 10s 连接超时,避免服务端无响应时无限等待 - 断线自动重连:指数退避 1s→2s→4s→8s→15s,共 5 次,全部失败回退到 原有刷新行为;用独立 _status.reconnecting 标志驱动续连,修复纯大厅 场景下重试链中断、重连遮罩卡住的问题 - broadcast() 改为快照迭代,避免 client.send 失败触发 close 移除自身 导致迭代漏发 - selfclose 取消自动重连并清理 reconnect_info,防止退出后误重连 - 重连决策逻辑抽离为纯函数 decideReconnect (@/util/reconnect.js) 并 新增 vitest 单测(11 用例)
Member
|
不要鲁棒性( |
Collaborator
|
这个功能如果没bug我没意见 |
1937475624
approved these changes
May 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR受影响的平台
所有平台(浏览器端、电脑端 win/mac、手机端 android/ios)。改动均为联机客户端通用逻辑,不涉及平台特定代码。
诱因和背景
当前联机客户端在连接异常时存在若干稳定性问题:
game.send()仅判断game.ws是否存在。当 WebSocket 处于CLOSING(2)/CLOSED(3)状态时game.ws仍非空,直接send()会抛出InvalidStateError。game.connect()创建 WebSocket 后无超时控制,服务端无响应时用户会无限等待。game.reload()粗暴刷新页面,体验差。game.broadcast()迭代lib.node.clients时,client.send()失败会触发client.close()将自身从数组移除,导致迭代中漏发部分客户端。无关联 issue。
PR描述
game.send()连接状态检查:发送前增加game.ws.readyState === 1(OPEN)判断,避免在 CLOSING/CLOSED 状态下调用send()抛异常。game.connect()连接超时:建立连接后设置 10s 超时,超时仍未 OPEN 则关闭连接并回调失败;onopen/onerror/onclose中清除该定时器。onclose改为按1s → 2s → 4s → 8s → 15s退避重连,共 5 次,全部失败后回退到原有的game.reload()行为,保证兼容。用独立的_status.reconnecting标志驱动重试链,避免依赖online/onlineroom状态——纯大厅场景下二者在重试期间均为 false,否则重试链会中断、重连遮罩卡住。selfclose(被踢 / 房主断开)时取消自动重连并清理reconnect_info,防止退出后误重连。game.broadcast()安全迭代:改为对lib.node.clients的快照[...lib.node.clients]迭代,避免发送失败移除元素导致漏发。decideReconnect(noname/util/reconnect.js),onclose仅依据其返回的action执行副作用,并为其新增 vitest 单测。PR测试
noname/util/reconnect.test.js,11 个用例覆盖decideReconnect的全部分支:未联机/游戏正常结束 → 不处理;主动退出/无地址/重试耗尽 → 回退刷新;指数退避延迟序列(含 15s 封顶);纯大厅续连(核心修复);maxAttempts缺省等。pnpm --filter noname test(vitest):1 文件 / 11 用例全部通过。pnpm --filter noname lint(ESLint):通过。pnpm --filter noname build:打包成功,确认新增import正确解析、不破坏构建。setTimeout/game.connect/game.reload/ 重连遮罩)及端到端真机断线重连行为难以自动化,需人工联机测试验证。扩展适配
无。本 PR 仅为防御性检查与优雅降级,未改变任何对外 API 或联机消息协议。
检查清单
WIP直到本PR内容全部提交character/rank.js中添加对应的武将强度评级,并对双人武将/复姓武将添加name:xxx的参数lib.translate中加入语音文件的文字台词PR描述中写入详细文档扩展适配中写入详细文档诱因和背景中明确链接到该issue.editorconfig、eslint.config.mjs和prettier.config.mjs所规定的代码样式,并且已经通过prettier格式化过代码