diff --git a/app/hooks/useWebSocket.ts b/app/hooks/useWebSocket.ts index 27e309f9c16..2ee187f72e5 100644 --- a/app/hooks/useWebSocket.ts +++ b/app/hooks/useWebSocket.ts @@ -19,34 +19,17 @@ export function useWebSocket() { const setOnlineUsers = useWebSocketStore((state) => state.setOnlineUsers); const setupWebSocket = useCallback(() => { - // 如果已经有连接或正在重连,不要创建新连接 - if ( - wsRef.current?.readyState === WebSocket.OPEN || - wsRef.current?.readyState === WebSocket.CONNECTING - ) { - return; - } - - // 清理现有连接 - if (wsRef.current) { - wsRef.current.close(); - wsRef.current = null; - } - - // 清理重连定时器 - if (reconnectTimeoutRef.current) { - clearTimeout(reconnectTimeoutRef.current); - reconnectTimeoutRef.current = null; - } - try { const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; - const ws = new WebSocket(`${protocol}//${window.location.host}/ws`); + const host = window.location.host; + console.log(`Connecting to WebSocket at ${protocol}//${host}/ws`); + + const ws = new WebSocket(`${protocol}//${host}/ws`); wsRef.current = ws; ws.onopen = () => { - console.log("WebSocket connected"); - reconnectAttemptsRef.current = 0; // 重置重连次数 + console.log("WebSocket connected successfully"); + reconnectAttemptsRef.current = 0; ws.send(JSON.stringify({ type: "getOnline" })); }; diff --git a/docker-compose.yml b/docker-compose.yml index 935b126a394..a2546c84fa8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: container_name: chatgpt-next-web image: yidadaa/chatgpt-next-web ports: - - 3000:3000 + - "3000:3000" environment: - OPENAI_API_KEY=$OPENAI_API_KEY - GOOGLE_API_KEY=$GOOGLE_API_KEY diff --git a/server.ts b/server.ts index aa22e2a4039..05cab280854 100644 --- a/server.ts +++ b/server.ts @@ -13,12 +13,6 @@ const handle = app.getRequestHandler(); app.prepare().then(() => { const server = createServer(async (req, res) => { try { - if (req.url === '/ws') { - res.writeHead(400); - res.end(); - return; - } - const parsedUrl = parse(req.url!, true); await handle(req, res, parsedUrl); } catch (err) { @@ -31,6 +25,7 @@ app.prepare().then(() => { setupWebSocket(server); server.listen(port, hostname, () => { - console.log(`> Ready on http://${hostname}:${port}`); + console.log(`> Server listening at http://${hostname}:${port}`); + console.log(`> WebSocket server is ready`); }); }); \ No newline at end of file