test
This commit is contained in:
parent
2c13dae452
commit
e69fd7b33b
2 changed files with 43 additions and 31 deletions
56
src/app.ts
56
src/app.ts
|
|
@ -13,18 +13,16 @@ import { OrganizationController } from "./controllers/OrganizationController";
|
||||||
import logMiddleware from "./middlewares/logs";
|
import logMiddleware from "./middlewares/logs";
|
||||||
import { CommandController } from "./controllers/CommandController";
|
import { CommandController } from "./controllers/CommandController";
|
||||||
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
|
import { ProfileSalaryController } from "./controllers/ProfileSalaryController";
|
||||||
import { WebSocketServer } from "ws";
|
|
||||||
import http from "http";
|
import http from "http";
|
||||||
export const wss = new WebSocketServer({ noServer: true,
|
import { WebSocketServer } from "ws";
|
||||||
path: "/api/v1/org/socket",
|
|
||||||
});
|
export const wss = new WebSocketServer({ noServer: true, path: "/api/v1/org/socket" });
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
// สร้างเซิร์ฟเวอร์ HTTP
|
|
||||||
const server = http.createServer(app);
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
|
|
@ -64,7 +62,7 @@ async function main() {
|
||||||
console.error("Error executing function from controller:", error);
|
console.error("Error executing function from controller:", error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const cronTime_Oct = "0 0 1 10 *";
|
const cronTime_Oct = "0 0 1 10 *";
|
||||||
cron.schedule(cronTime_Oct, async () => {
|
cron.schedule(cronTime_Oct, async () => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -74,7 +72,7 @@ async function main() {
|
||||||
console.error("Error executing function from controller:", error);
|
console.error("Error executing function from controller:", error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const cronTime_Tenure = "0 0 * * *";
|
const cronTime_Tenure = "0 0 * * *";
|
||||||
cron.schedule(cronTime_Tenure, async () => {
|
cron.schedule(cronTime_Tenure, async () => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -108,38 +106,38 @@ async function main() {
|
||||||
|
|
||||||
runMessageQueue();
|
runMessageQueue();
|
||||||
|
|
||||||
// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
// สร้างเซิร์ฟเวอร์ HTTP
|
||||||
server.on("upgrade", (request:any, socket:any, head:any) => {
|
const server = http.createServer(app);
|
||||||
console.log("🔹 Handling upgrade request...");
|
|
||||||
wss.handleUpgrade(request, socket, head, (ws:any) => {
|
|
||||||
console.log("🔹 WebSocket connection established");
|
|
||||||
wss.emit("connection", ws, request);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
wss.on("connection", (ws:any) => {
|
// ✅ WebSocket Handling
|
||||||
console.log("✅ Client connected to WebSocket");
|
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", () => {
|
ws.on("close", () => {
|
||||||
console.log("❌ Client disconnected");
|
console.log("❌ WebSocket Client Disconnected");
|
||||||
});
|
|
||||||
|
|
||||||
ws.on("error", (error:any) => {
|
|
||||||
console.error("WebSocket error:", error);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ตั้งค่า Express routes
|
// ✅ อัปเกรด HTTP เป็น WebSocket
|
||||||
app.get('/', (req, res) => {
|
server.on("upgrade", (req, socket, head) => {
|
||||||
res.send('Hello from Express!');
|
wss.handleUpgrade(req, socket, head, (ws) => {
|
||||||
|
wss.emit("connection", ws, req);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(APP_PORT, APP_HOST, () => {
|
server.listen(APP_PORT, APP_HOST, () => {
|
||||||
console.log(`[APP] Application is running on: http://${APP_HOST}:${APP_PORT}`);
|
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] Swagger on: http://${APP_HOST}:${APP_PORT}/api-docs`);
|
||||||
console.log("[APP] HTTP Server is listening on current port");
|
console.log("[APP] HTTP Server is listening on current port");
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,9 @@ import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer";
|
||||||
import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee";
|
import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee";
|
||||||
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
|
import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster";
|
||||||
|
|
||||||
|
import { WebSocket } from "ws";
|
||||||
|
import { wss } from "../app"; // ✅ Import clients
|
||||||
|
|
||||||
@Route("api/v1/org/report")
|
@Route("api/v1/org/report")
|
||||||
@Tags("Report")
|
@Tags("Report")
|
||||||
@Security("bearerAuth")
|
@Security("bearerAuth")
|
||||||
|
|
@ -3537,8 +3540,19 @@ export class ReportController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
|
// ✅ ส่งข้อมูลไปยังทุกไคลเอนต์ที่เชื่อมต่อ 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 } });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue