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": "^20.11.5",
|
||||||
"@types/node-cron": "^3.0.11",
|
"@types/node-cron": "^3.0.11",
|
||||||
"@types/swagger-ui-express": "^4.1.6",
|
"@types/swagger-ui-express": "^4.1.6",
|
||||||
|
"@types/ws": "^8.5.14",
|
||||||
"nodemon": "^3.0.3",
|
"nodemon": "^3.0.3",
|
||||||
"prettier": "^3.2.2",
|
"prettier": "^3.2.2",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
|
|
@ -47,6 +48,7 @@
|
||||||
"swagger-ui-express": "^5.0.0",
|
"swagger-ui-express": "^5.0.0",
|
||||||
"tsoa": "^6.0.1",
|
"tsoa": "^6.0.1",
|
||||||
"typeorm": "^0.3.19",
|
"typeorm": "^0.3.19",
|
||||||
"typeorm-cli": "^1.0.7"
|
"typeorm-cli": "^1.0.7",
|
||||||
|
"ws": "^8.18.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
53
src/app.ts
53
src/app.ts
|
|
@ -10,6 +10,14 @@ import { RegisterRoutes } from "./routes";
|
||||||
import logMiddleware from "./middlewares/logs";
|
import logMiddleware from "./middlewares/logs";
|
||||||
import axios from "axios";
|
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() {
|
async function main() {
|
||||||
await AppDataSource.initialize();
|
await AppDataSource.initialize();
|
||||||
|
|
||||||
|
|
@ -18,7 +26,7 @@ async function main() {
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
origin: "*",
|
origin: "*",
|
||||||
}),
|
})
|
||||||
);
|
);
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.urlencoded({ extended: true }));
|
app.use(express.urlencoded({ extended: true }));
|
||||||
|
|
@ -56,7 +64,7 @@ async function main() {
|
||||||
accept: "application/pdf",
|
accept: "application/pdf",
|
||||||
},
|
},
|
||||||
responseType: "arraybuffer",
|
responseType: "arraybuffer",
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log("Response:", apiResponse.data);
|
console.log("Response:", apiResponse.data);
|
||||||
|
|
@ -75,16 +83,49 @@ async function main() {
|
||||||
RegisterRoutes(app);
|
RegisterRoutes(app);
|
||||||
|
|
||||||
app.use(error);
|
app.use(error);
|
||||||
|
app.use(cors());
|
||||||
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
|
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
|
||||||
const APP_PORT = +(process.env.APP_PORT || 3000);
|
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_PORT,
|
||||||
APP_HOST,
|
APP_HOST,
|
||||||
() => (
|
() => (
|
||||||
console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`),
|
console.log(
|
||||||
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
|
`[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