test websocket
This commit is contained in:
parent
43ba731850
commit
9e14183ac7
3 changed files with 2334 additions and 1923 deletions
|
|
@ -22,6 +22,7 @@
|
|||
"@types/node": "^20.11.5",
|
||||
"@types/node-cron": "^3.0.11",
|
||||
"@types/swagger-ui-express": "^4.1.6",
|
||||
"@types/ws": "^8.5.14",
|
||||
"nodemon": "^3.0.3",
|
||||
"prettier": "^3.2.2",
|
||||
"ts-node": "^10.9.2",
|
||||
|
|
@ -47,6 +48,7 @@
|
|||
"swagger-ui-express": "^5.0.0",
|
||||
"tsoa": "^6.0.1",
|
||||
"typeorm": "^0.3.19",
|
||||
"typeorm-cli": "^1.0.7"
|
||||
"typeorm-cli": "^1.0.7",
|
||||
"ws": "^8.18.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
51
src/app.ts
51
src/app.ts
|
|
@ -10,6 +10,14 @@ import { RegisterRoutes } from "./routes";
|
|||
import logMiddleware from "./middlewares/logs";
|
||||
import axios from "axios";
|
||||
|
||||
import http from "http";
|
||||
import { WebSocket, WebSocketServer } from "ws"; // ✅ Import WebSocket
|
||||
|
||||
// ✅ สร้าง WebSocket Server โดยใช้ HTTP Server เดียวกัน
|
||||
export const wss = new WebSocketServer({ noServer: true });
|
||||
|
||||
|
||||
|
||||
async function main() {
|
||||
await AppDataSource.initialize();
|
||||
|
||||
|
|
@ -18,7 +26,7 @@ async function main() {
|
|||
app.use(
|
||||
cors({
|
||||
origin: "*",
|
||||
}),
|
||||
})
|
||||
);
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
|
@ -56,7 +64,7 @@ async function main() {
|
|||
accept: "application/pdf",
|
||||
},
|
||||
responseType: "arraybuffer",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
console.log("Response:", apiResponse.data);
|
||||
|
|
@ -75,16 +83,49 @@ async function main() {
|
|||
RegisterRoutes(app);
|
||||
|
||||
app.use(error);
|
||||
app.use(cors());
|
||||
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
|
||||
const APP_PORT = +(process.env.APP_PORT || 3000);
|
||||
|
||||
app.listen(
|
||||
// ✅ สร้าง HTTP Server และเชื่อม Express
|
||||
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,
|
||||
() => (
|
||||
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] Application is running on: http://localhost:${APP_PORT}`
|
||||
),
|
||||
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`),
|
||||
console.log(
|
||||
`[WS] WebSocket Server is running on ws://localhost:${APP_PORT}`
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue