test ws
This commit is contained in:
parent
e976e51860
commit
1ea3392a8f
3 changed files with 55 additions and 56 deletions
53
src/app.ts
53
src/app.ts
|
|
@ -14,16 +14,14 @@ 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 { initWebSocket } from "./services/webSocket";
|
||||||
import http from "http";
|
|
||||||
export const wss = new WebSocketServer({ noServer: true,
|
|
||||||
path: "/api/v1/org-socket",
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
|
|
||||||
|
initWebSocket();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
|
|
@ -89,14 +87,14 @@ async function main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
|
// app.listen(APP_PORT, APP_HOST, () => console.log(`Listening on: http://localhost:${APP_PORT}`));
|
||||||
// app.listen(
|
app.listen(
|
||||||
// APP_PORT,
|
APP_PORT,
|
||||||
// APP_HOST,
|
APP_HOST,
|
||||||
// () => (
|
() => (
|
||||||
// console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`),
|
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] Swagger on: http://localhost:${APP_PORT}/api-docs`)
|
||||||
// ),
|
),
|
||||||
// );
|
);
|
||||||
async function runMessageQueue() {
|
async function runMessageQueue() {
|
||||||
try {
|
try {
|
||||||
await rabbitmqInit();
|
await rabbitmqInit();
|
||||||
|
|
@ -108,34 +106,7 @@ async function main() {
|
||||||
|
|
||||||
runMessageQueue();
|
runMessageQueue();
|
||||||
|
|
||||||
const server = http.createServer(app);
|
|
||||||
|
|
||||||
// การจัดการคำขออัปเกรดจาก 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");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ 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";
|
import { sendWebSocket } from "../services/webSocket";
|
||||||
|
// import { sendWebSocket } from "../services/webSocket";
|
||||||
|
|
||||||
@Route("api/v1/org/report")
|
@Route("api/v1/org/report")
|
||||||
@Tags("Report")
|
@Tags("Report")
|
||||||
|
|
@ -3541,7 +3542,7 @@ export class ReportController extends Controller {
|
||||||
const metaData = { template: "report2", reportName: "report2", data: { data } };
|
const metaData = { template: "report2", reportName: "report2", data: { data } };
|
||||||
sendWebSocket(metaData)
|
sendWebSocket(metaData)
|
||||||
|
|
||||||
// return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
|
return new HttpSuccess({ template: "report2", reportName: "report2", data: { data } });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,43 @@
|
||||||
import { WebSocket } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { wss } from "../app";
|
|
||||||
|
|
||||||
export async function sendWebSocket(data:any){
|
let wss: WebSocketServer;
|
||||||
wss.clients.forEach((client: any) => {
|
|
||||||
if (client.readyState === WebSocket.OPEN) {
|
export function initWebSocket() {
|
||||||
const message = JSON.stringify(data);
|
wss = new WebSocketServer({ port: 13002, path: "/api/v1/org-socket" });
|
||||||
console.log("📤 Sending data to client:", message);
|
|
||||||
client.send(message, (err:any) => {
|
// การจัดการคำขออัปเกรดจาก HTTP เป็น WebSocket
|
||||||
if (err) {
|
wss.on("upgrade", (request: any, socket: any, head: any) => {
|
||||||
console.error("❌ Error sending message:", err);
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue