feat: enable websocket
This commit is contained in:
parent
8cf13b8690
commit
b2ac1878b9
4 changed files with 294 additions and 39 deletions
|
|
@ -1,43 +1,69 @@
|
|||
// import { WebSocketServer } from "ws";
|
||||
import { Server } from "socket.io";
|
||||
|
||||
// let wss: WebSocketServer;
|
||||
let io: Server;
|
||||
|
||||
// export function initWebSocket() {
|
||||
// wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" });
|
||||
export function initWebSocket() {
|
||||
if (io) return;
|
||||
|
||||
// // การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
||||
// wss.on("upgrade", (request: any, socket: any, head: any) => {
|
||||
// console.log("🔹 Handling upgrade request...");
|
||||
// wss.handleUpgrade(request, socket, head, (ws: any) => {
|
||||
// console.log("🔹 WebSocket connection established");
|
||||
// wss.emit("connection", ws, request);
|
||||
// });
|
||||
// });
|
||||
io = new Server({ cors: { origin: "*" }, path: "/api/v1/org-socket" });
|
||||
|
||||
// wss.on("connection", (ws: any) => {
|
||||
// console.log("✅ Client connected to WebSocket");
|
||||
io.use(async (socket, next) => {
|
||||
const token = socket.handshake.auth.token;
|
||||
|
||||
// ws.on("close", () => {
|
||||
// console.log("❌ Client disconnected");
|
||||
// });
|
||||
const res = await fetch(`${process.env.AUTH_REALM_URL}/protocol/openid-connect/userinfo`, {
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
}).catch((e) => console.error(e));
|
||||
|
||||
// ws.on("error", (error: any) => {
|
||||
// console.error("WebSocket error:", error);
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
if (res?.ok) {
|
||||
socket.data.user = await res.json();
|
||||
}
|
||||
|
||||
// export async function sendWebSocket(data: any) {
|
||||
// if (!wss) initWebSocket();
|
||||
// wss.clients.forEach((client: any) => {
|
||||
// if (client.readyState === WebSocket.OPEN) {
|
||||
// const message = JSON.stringify(data);
|
||||
// console.log("📤 Sending data to client:", message);
|
||||
// client.send(message, (err: any) => {
|
||||
// if (err) {
|
||||
// console.error("❌ Error sending message:", err);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
next();
|
||||
});
|
||||
|
||||
io.on("connection", (ws) => {
|
||||
console.log("✅ Client connected to WebSocket");
|
||||
|
||||
ws.on("close", () => {
|
||||
console.log("❌ Client disconnected");
|
||||
});
|
||||
|
||||
ws.on("error", (error: any) => {
|
||||
console.error("WebSocket error:", error);
|
||||
});
|
||||
});
|
||||
|
||||
io.listen(13002);
|
||||
}
|
||||
|
||||
export async function sendWebSocket(
|
||||
event: string,
|
||||
data: any,
|
||||
opts?: {
|
||||
roles?: string[];
|
||||
userId?: string[];
|
||||
},
|
||||
) {
|
||||
if (!io) initWebSocket();
|
||||
|
||||
for (let [id, session] of io.of("/").sockets) {
|
||||
const user: {
|
||||
sub: string;
|
||||
name: string;
|
||||
given_name: string;
|
||||
family_name: string;
|
||||
preferred_username: string;
|
||||
email: string;
|
||||
role: string[];
|
||||
} = session.data.user;
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
if (
|
||||
user.role?.some((v) => opts?.roles?.includes(v)) ||
|
||||
opts?.userId?.some((v) => user.sub === v)
|
||||
) {
|
||||
io.to(id).emit(event, JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue