mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-21 05:45:23 +02:00
refactor(websocket): streamline WebSocket server setup and client instantiation
- Removed the request validation logic from the WebSocket connection handler. - Added a cleanup function to close the WebSocket server. - Introduced a singleton pattern for the WebSocket client to manage connections more efficiently.
This commit is contained in:
@@ -27,15 +27,28 @@ const getWsUrl = () => {
|
||||
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const host = window.location.host;
|
||||
|
||||
// Use the base URL for all tRPC WebSocket connections
|
||||
return `${protocol}${host}/drawer-logs`;
|
||||
};
|
||||
|
||||
const wsClient =
|
||||
typeof window !== "undefined"
|
||||
? createWSClient({
|
||||
url: getWsUrl() || "",
|
||||
})
|
||||
: null;
|
||||
// Singleton WebSocket client instance
|
||||
let wsClientInstance: ReturnType<typeof createWSClient> | null = null;
|
||||
|
||||
const getWsClient = () => {
|
||||
if (typeof window === "undefined") return null;
|
||||
|
||||
if (!wsClientInstance) {
|
||||
wsClientInstance = createWSClient({
|
||||
url: getWsUrl() || "",
|
||||
onClose: () => {
|
||||
// Reset the instance when connection closes so it can be recreated
|
||||
wsClientInstance = null;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return wsClientInstance;
|
||||
};
|
||||
|
||||
/** A set of type-safe react-query hooks for your tRPC API. */
|
||||
export const api = createTRPCNext<AppRouter>({
|
||||
@@ -57,7 +70,7 @@ export const api = createTRPCNext<AppRouter>({
|
||||
splitLink({
|
||||
condition: (op) => op.type === "subscription",
|
||||
true: wsLink({
|
||||
client: wsClient!,
|
||||
client: getWsClient()!,
|
||||
}),
|
||||
false: splitLink({
|
||||
condition: (op) => op.input instanceof FormData,
|
||||
|
||||
Reference in New Issue
Block a user