This commit is contained in:
AdisakKanthawilang 2025-03-05 17:29:07 +07:00
parent 205034ab4d
commit dd02c81008
2 changed files with 53 additions and 58 deletions

View file

@ -13,24 +13,19 @@ import { OrganizationController } from "./controllers/OrganizationController";
import logMiddleware from "./middlewares/logs";
import { CommandController } from "./controllers/CommandController";
import { WebSocketServer } from "ws";
import http from "http";
// import { WebSocketServer } from "ws";
// import http from "http";
export const wss = new WebSocketServer({ noServer: true,
path: "/api/vi/org/socket",
});
// export const wss = new WebSocketServer({ noServer: true,
// path: "/api/v1/org/socket",
// });
async function main() {
await AppDataSource.initialize();
const app = express();
// สร้างเซิร์ฟเวอร์ HTTP
const server = http.createServer(app);
// socket.instance = new Server(server, {
// cors: { origin: "*" },
// path: "/api/v1/org/socket",
// });
// const server = http.createServer(app);
app.use(
cors({
@ -82,14 +77,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(`[APP] Application is running on: http://localhost:${APP_PORT}`),
// console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
// ),
// );
app.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`)
),
);
async function runMessageQueue() {
try {
await rabbitmqInit();
@ -102,36 +97,36 @@ async function main() {
runMessageQueue();
// การจัดการคำขออัปเกรดจาก 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);
});
});
// 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");
// wss.on("connection", (ws:any) => {
// console.log("✅ Client connected to WebSocket");
ws.on("close", () => {
console.log("❌ Client disconnected");
});
// ws.on("close", () => {
// console.log("❌ Client disconnected");
// });
ws.on("error", (error:any) => {
console.error("WebSocket error:", error);
});
});
// ws.on("error", (error:any) => {
// console.error("WebSocket error:", error);
// });
// });
// ตั้งค่า Express routes
app.get('/', (req, res) => {
res.send('Hello from Express!');
});
// // ตั้งค่า Express routes
// app.get('/', (req, res) => {
// res.send('Hello from Express!');
// });
server.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");
});
// server.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");
// });
}