This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-03-06 16:42:19 +07:00
parent 4ac7528c5c
commit cfcf63b9aa
2 changed files with 3 additions and 48 deletions

View file

@ -13,12 +13,8 @@ import { OrganizationController } from "./controllers/OrganizationController";
import logMiddleware from "./middlewares/logs";
import { CommandController } from "./controllers/CommandController";
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
import http from "http";
import { WebSocketServer } from "ws";
export const wss = new WebSocketServer({ noServer: true, path: "/api/v1/org/socket" });
async function main() {
await AppDataSource.initialize();
@ -106,34 +102,7 @@ async function main() {
runMessageQueue();
// สร้างเซิร์ฟเวอร์ HTTP
const server = http.createServer(app);
// ✅ WebSocket Handling
wss.on("connection", (ws) => {
console.log("✅ WebSocket Client Connected");
// clients.add(ws); // เก็บ client ไว้
// ✅ ตั้ง Heartbeat (ping/pong)
ws.on("message", (message) => {
console.log("📩 Received:", message.toString());
ws.send("📡 Server Received: " + message.toString());
});
ws.on("close", () => {
console.log("❌ WebSocket Client Disconnected");
});
});
// ✅ อัปเกรด HTTP เป็น WebSocket
server.on("upgrade", (req, socket, head) => {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
});
server.listen(APP_PORT, APP_HOST, () => {
app.listen(APP_PORT, APP_HOST, () => {
console.log(`[APP] Application is running on: http://${APP_HOST}:${APP_PORT}`);
console.log(`[APP] Swagger on: http://${APP_HOST}:${APP_PORT}/api-docs`);
console.log("[APP] HTTP Server is listening on current port");

View file

@ -32,9 +32,6 @@ import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer";
import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee";
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
import { WebSocket } from "ws";
import { wss } from "../app"; // ✅ Import clients
@Route("api/v1/org/report")
@Tags("Report")
@Security("bearerAuth")
@ -3540,19 +3537,8 @@ export class ReportController extends Controller {
}
}
}
// ✅ ส่งข้อมูลไปยังทุกไคลเอนต์ที่เชื่อมต่อ WebSocket
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
if (client.readyState === 1) {
client.send(
JSON.stringify({ template: "report2", reportName: "report2", data: { data } }),
);
}
}
});
// return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
}
/**