forked from ChatGPTNextWeb/ChatGPT-Next-Web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update WebSocket handling and configuration
- Removed the deprecated useWebSocket hook and replaced it with a new implementation in the websocket directory. - Updated the Dockerfile to streamline the server command execution. - Modified the docker-compose.yml to remove the default WebSocket URL environment variable. - Changed the default theme color in next.config.mjs from "mint" to "ocean". - Enhanced sidebar component to improve online user display logic. - Added WS_URL to the server configuration for better WebSocket management.
- Loading branch information
1 parent
c738294
commit b2e1a3e
Showing
13 changed files
with
321 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function GET() { | ||
return NextResponse.json({ | ||
wsUrl: process.env.WS_URL ?? "", | ||
}); | ||
} |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// WebSocket 配置相关 | ||
export const getWebSocketConfig = () => { | ||
return { | ||
wsUrl: process.env.WS_URL || undefined, | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { NextResponse } from "next/server"; | ||
import { getWebSocketConfig } from "./config"; | ||
|
||
export async function GET() { | ||
return NextResponse.json(getWebSocketConfig()); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { WebSocketServer, WebSocket } from "ws"; | ||
import { Server } from "http"; | ||
|
||
let onlineUsers = 0; | ||
export let wss: WebSocketServer | null = null; | ||
|
||
function broadcastOnlineUsers() { | ||
if (!wss) return; | ||
const message = JSON.stringify({ type: "online", count: onlineUsers }); | ||
wss.clients.forEach((client) => { | ||
try { | ||
if (client.readyState === WebSocket.OPEN) { | ||
client.send(message); | ||
} | ||
} catch (error) { | ||
console.error("Error broadcasting to client:", error); | ||
} | ||
}); | ||
} | ||
|
||
export function setupWebSocket(server: Server) { | ||
if (wss) return; | ||
|
||
wss = new WebSocketServer({ | ||
server, | ||
path: "/ws", | ||
perMessageDeflate: false, | ||
}); | ||
|
||
console.log("WebSocket server is set up"); | ||
|
||
wss.on("connection", (ws: WebSocket) => { | ||
onlineUsers++; | ||
console.log("New connection established. Online users:", onlineUsers); | ||
|
||
ws.send(JSON.stringify({ type: "online", count: onlineUsers })); | ||
broadcastOnlineUsers(); | ||
|
||
ws.on("message", (message: string) => { | ||
try { | ||
const data = JSON.parse(message.toString()); | ||
if (data.type === "getOnline") { | ||
ws.send(JSON.stringify({ type: "online", count: onlineUsers })); | ||
} | ||
} catch (e) { | ||
console.error("Failed to parse message:", e); | ||
} | ||
}); | ||
|
||
ws.on("close", () => { | ||
onlineUsers = Math.max(0, onlineUsers - 1); | ||
console.log("Connection closed. Online users:", onlineUsers); | ||
broadcastOnlineUsers(); | ||
}); | ||
|
||
ws.on("error", (error) => { | ||
console.error("WebSocket error:", error); | ||
if (ws.readyState === WebSocket.OPEN) { | ||
ws.close(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { create } from "zustand"; | ||
|
||
interface WebSocketStore { | ||
onlineUsers: number; | ||
setOnlineUsers: (count: number) => void; | ||
} | ||
|
||
export const useWebSocketStore = create<WebSocketStore>((set) => ({ | ||
onlineUsers: 0, | ||
setOnlineUsers: (count) => set({ onlineUsers: count }), | ||
})); |
Oops, something went wrong.