mirror of
https://github.com/andwati/dokploy.git
synced 2026-12-06 01:28:46 +00: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:
@@ -1,5 +1,4 @@
|
||||
import type http from "node:http";
|
||||
import { validateRequest } from "@dokploy/server/index";
|
||||
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
||||
import { WebSocketServer } from "ws";
|
||||
import { appRouter } from "../api/root";
|
||||
@@ -13,11 +12,13 @@ export const setupDrawerLogsWebSocketServer = (
|
||||
path: "/drawer-logs",
|
||||
});
|
||||
|
||||
// Set up tRPC WebSocket handler
|
||||
applyWSSHandler({
|
||||
wss: wssTerm,
|
||||
router: appRouter,
|
||||
createContext: createTRPCContext as any,
|
||||
});
|
||||
|
||||
server.on("upgrade", (req, socket, head) => {
|
||||
const { pathname } = new URL(req.url || "", `http://${req.headers.host}`);
|
||||
|
||||
@@ -31,13 +32,8 @@ export const setupDrawerLogsWebSocketServer = (
|
||||
}
|
||||
});
|
||||
|
||||
wssTerm.on("connection", async (ws, req) => {
|
||||
const _url = new URL(req.url || "", `http://${req.headers.host}`);
|
||||
const { user, session } = await validateRequest(req);
|
||||
|
||||
if (!user || !session) {
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
});
|
||||
// Return cleanup function
|
||||
return () => {
|
||||
wssTerm.close();
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user