test ws
This commit is contained in:
parent
cfcf63b9aa
commit
fa073242b8
4 changed files with 56 additions and 5 deletions
34
src/app.ts
34
src/app.ts
|
|
@ -13,7 +13,12 @@ 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 { WebSocketServer } from "ws";
|
||||||
|
import http from "http";
|
||||||
|
export const wss = new WebSocketServer({ noServer: true,
|
||||||
|
path: "/api/vi/org/socket",
|
||||||
|
});
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
|
|
@ -102,9 +107,32 @@ async function main() {
|
||||||
|
|
||||||
runMessageQueue();
|
runMessageQueue();
|
||||||
|
|
||||||
app.listen(APP_PORT, APP_HOST, () => {
|
const server = http.createServer(app);
|
||||||
console.log(`[APP] Application is running on: http://${APP_HOST}:${APP_PORT}`);
|
|
||||||
console.log(`[APP] Swagger on: http://${APP_HOST}:${APP_PORT}/api-docs`);
|
// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
||||||
|
server.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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
wss.on("connection", (ws:any) => {
|
||||||
|
console.log("✅ Client connected to WebSocket");
|
||||||
|
|
||||||
|
ws.on("close", () => {
|
||||||
|
console.log("❌ Client disconnected");
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on("error", (error:any) => {
|
||||||
|
console.error("WebSocket error:", error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(APP_PORT, APP_HOST, () => {
|
||||||
|
console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`);
|
||||||
|
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`);
|
||||||
console.log("[APP] HTTP Server is listening on current port");
|
console.log("[APP] HTTP Server is listening on current port");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ export class PermissionController extends Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
let reply = await getAsync("role_" + profile.id);
|
let reply = await getAsync("role_" + profile.id);
|
||||||
|
console.log(">>>reply",reply);
|
||||||
if (reply != null) {
|
if (reply != null) {
|
||||||
reply = JSON.parse(reply);
|
reply = JSON.parse(reply);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -84,11 +85,13 @@ export class PermissionController extends Controller {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log(">>>posMaster",posMaster);
|
||||||
const getDetail = await this.authRoleRepo.findOne({
|
const getDetail = await this.authRoleRepo.findOne({
|
||||||
select: ["id", "roleName", "roleDescription"],
|
select: ["id", "roleName", "roleDescription"],
|
||||||
where: { id: posMaster.authRoleId },
|
where: { id: posMaster.authRoleId },
|
||||||
});
|
});
|
||||||
|
console.log(">>>detail",getDetail);
|
||||||
|
|
||||||
if (!getDetail) {
|
if (!getDetail) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ import { Profile } from "../entities/Profile";
|
||||||
import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer";
|
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 { sendWebSocket } from "../services/webSocket";
|
||||||
|
|
||||||
@Route("api/v1/org/report")
|
@Route("api/v1/org/report")
|
||||||
@Tags("Report")
|
@Tags("Report")
|
||||||
|
|
@ -1369,6 +1370,7 @@ export class ReportController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HttpSuccess({ template: "report1", reportName: "report1", data: { data } });
|
return new HttpSuccess({ template: "report1", reportName: "report1", data: { data } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3537,8 +3539,10 @@ export class ReportController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const metaData = { template: "report2", reportName: "report2", data: { data } };
|
||||||
|
sendWebSocket(metaData)
|
||||||
|
|
||||||
return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
|
// return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
16
src/services/webSocket.ts
Normal file
16
src/services/webSocket.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { WebSocket } from "ws";
|
||||||
|
import { wss } from "../app";
|
||||||
|
|
||||||
|
export async function sendWebSocket(data:any){
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue