{"record":{"id":"c231f21c451d7f24","repo":"BigPizzaV3/CodexPlusPlus","slug":"error-c231f2","errorCode":null,"errorMessage":"未连接","messagePattern":"未连接","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/codex-plus-mobile-relay/src/main.rs","lineNumber":858,"sourceCode":"        return;\n      }\n      if (response.type === \"appServerClosed\") {\n        appServerConnected = false;\n        for (const item of pendingRpc.values()) item.reject(new Error(response.error || \"app-server 连接已关闭\"));\n        pendingRpc.clear();\n        setStatus(response.error || \"app-server 连接已关闭\", !!response.error);\n        return;\n      }\n      const id = String(response.id ?? \"\");\n      const resolver = pending.get(id);\n      if (resolver) { pending.delete(id); resolver.resolve(response); return; }\n    } catch (error) {\n      setStatus(`解密/解析失败：${error.message}`, true);\n    }\n  };\n}\nasync function request(path, body = \"\", timeoutMs = 30000) {\n  if (!socket || socket.readyState !== WebSocket.OPEN) throw new Error(\"未连接\");\n  const id = `${Date.now()}-${requestId++}`;\n  const packet = await encrypt({ type: \"httpRequest\", id, method: body ? \"POST\" : \"GET\", path, headers: {}, body });\n  socket.send(JSON.stringify(packet));\n  return await new Promise((resolve, reject) => {\n    pending.set(id, { resolve, reject });\n    setTimeout(() => { if (pending.delete(id)) reject(new Error(\"请求超时\")); }, timeoutMs);\n  });\n}\nasync function ensureAppServer() {\n  if (appServerConnected && appServerSessionId) return;\n  appServerSessionId = appServerSessionId || `mobile-${Date.now()}-${Math.random().toString(16).slice(2)}`;\n  const id = `${Date.now()}-${requestId++}`;\n  const packet = await encrypt({ type: \"appServerConnect\", id, sessionId: appServerSessionId });\n  socket.send(JSON.stringify(packet));\n  await new Promise((resolve, reject) => {\n    pending.set(id, { resolve, reject });\n    setTimeout(() => { if (pending.delete(id)) reject(new Error(\"app-server 连接超时\")); }, 30000);\n  });","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/apps/codex-plus-mobile-relay/src/main.rs#L840-L876","documentation":"request() sends an encrypted httpRequest packet over the shared WebSocket, but only if the socket exists and is in WebSocket.OPEN state. Before any connection is established, or after the socket dropped/closed, it fails fast with \"未连接\" instead of queueing the request.","triggerScenarios":"Calling request(path, body) before connect completes, after socket.close(), after the relay dropped the connection, or while readyState is CONNECTING/CLOSING.","commonSituations":"User submits a form immediately on page load before the WebSocket handshake finishes; laptop woke from sleep and the socket is dead; relay server restarted; network blip closed the socket without a reconnect running yet.","solutions":["Ensure connect()/ensureSocket() is awaited before calling request() (e.g. gate UI until OPEN).","Add a reconnect-with-backoff wrapper that retries request() after the socket reopens.","Check socket.readyState in the UI and disable actions while not OPEN.","On this error, trigger ensureAppServer()/reconnect rather than surfacing a raw failure."],"exampleFix":"// before\nconst res = await request(\"/api/threads\");\n// after\nawait ensureSocket();\nconst res = await request(\"/api/threads\").catch(async err => {\n  if (err.message === \"未连接\") { await reconnect(); return request(\"/api/threads\"); }\n  throw err;\n});","handlingStrategy":"retry","validationCode":"if (!socket || socket.readyState !== WebSocket.OPEN) { await ensureSocket(); }","typeGuard":"function isSocketOpen(s) { return !!s && s.readyState === WebSocket.OPEN; }","tryCatchPattern":"try { return await request(path, body); } catch (e) { if (e.message === \"未连接\") { await reconnect(); return request(path, body); } throw e; }","preventionTips":["Gate the UI (disable buttons) until the socket reports OPEN.","Implement auto-reconnect with exponential backoff.","Await a shared ensureSocket() promise before any request call."],"tags":["websocket","connection","state"],"backgroundTag":"connection-refused","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}