BigPizzaV3/CodexPlusPlus · error

未连接

Error message

未连接

What it means

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.

Solutions

  1. Ensure connect()/ensureSocket() is awaited before calling request() (e.g. gate UI until OPEN).
  2. Add a reconnect-with-backoff wrapper that retries request() after the socket reopens.
  3. Check socket.readyState in the UI and disable actions while not OPEN.
  4. On this error, trigger ensureAppServer()/reconnect rather than surfacing a raw failure.

Example fix

// before
const res = await request("/api/threads");
// after
await ensureSocket();
const res = await request("/api/threads").catch(async err => {
  if (err.message === "未连接") { await reconnect(); return request("/api/threads"); }
  throw err;
});
Defensive patterns

Strategy: retry

Validate before calling

if (!socket || socket.readyState !== WebSocket.OPEN) { await ensureSocket(); }

Type guard

function isSocketOpen(s) { return !!s && s.readyState === WebSocket.OPEN; }

Try / catch

try { return await request(path, body); } catch (e) { if (e.message === "未连接") { await reconnect(); return request(path, body); } throw e; }

Prevention

When it happens

Trigger: Calling request(path, body) before connect completes, after socket.close(), after the relay dropped the connection, or while readyState is CONNECTING/CLOSING.

Common situations: 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.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/c231f21c451d7f24. Report an issue: GitHub.

Appendix: source

Thrown at apps/codex-plus-mobile-relay/src/main.rs:858

        return;
      }
      if (response.type === "appServerClosed") {
        appServerConnected = false;
        for (const item of pendingRpc.values()) item.reject(new Error(response.error || "app-server 连接已关闭"));
        pendingRpc.clear();
        setStatus(response.error || "app-server 连接已关闭", !!response.error);
        return;
      }
      const id = String(response.id ?? "");
      const resolver = pending.get(id);
      if (resolver) { pending.delete(id); resolver.resolve(response); return; }
    } catch (error) {
      setStatus(`解密/解析失败:${error.message}`, true);
    }
  };
}
async function request(path, body = "", timeoutMs = 30000) {
  if (!socket || socket.readyState !== WebSocket.OPEN) throw new Error("未连接");
  const id = `${Date.now()}-${requestId++}`;
  const packet = await encrypt({ type: "httpRequest", id, method: body ? "POST" : "GET", path, headers: {}, body });
  socket.send(JSON.stringify(packet));
  return await new Promise((resolve, reject) => {
    pending.set(id, { resolve, reject });
    setTimeout(() => { if (pending.delete(id)) reject(new Error("请求超时")); }, timeoutMs);
  });
}
async function ensureAppServer() {
  if (appServerConnected && appServerSessionId) return;
  appServerSessionId = appServerSessionId || `mobile-${Date.now()}-${Math.random().toString(16).slice(2)}`;
  const id = `${Date.now()}-${requestId++}`;
  const packet = await encrypt({ type: "appServerConnect", id, sessionId: appServerSessionId });
  socket.send(JSON.stringify(packet));
  await new Promise((resolve, reject) => {
    pending.set(id, { resolve, reject });
    setTimeout(() => { if (pending.delete(id)) reject(new Error("app-server 连接超时")); }, 30000);
  });

View on GitHub (pinned to b1ed92e5e4)