feat: socketio event

This commit is contained in:
Methapon2001 2023-12-07 17:25:56 +07:00
parent 0a1265b78c
commit 25752dac19
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
9 changed files with 200 additions and 8 deletions

View file

@ -2,6 +2,8 @@ import "dotenv/config";
import express from "express";
import swaggerUi from "swagger-ui-express";
import cors from "cors";
import { createServer } from "http";
import { Server } from "socket.io";
import { RegisterRoutes } from "./routes";
import errorHandler from "./middlewares/exception";
@ -9,6 +11,7 @@ import rabbitmq from "./rabbitmq";
import swaggerSpecs from "./swagger.json";
import { handler as amqHandler } from "./rabbitmq/handler";
import { setInstance } from "./lib/websocket";
const PORT = +(process.env.PORT || 80);
@ -31,7 +34,24 @@ app.use((_req, res, _next) => {
res.sendFile(`${process.cwd()}/static/index.html`);
});
app.listen(PORT, "0.0.0.0", () =>
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
},
});
setInstance(io);
io.on("connection", (socket) => {
console.log("User Connected");
socket.on("disconnected", () => {
console.log("User Disconnected");
});
});
server.listen(PORT, "0.0.0.0", () =>
console.log(`[APP] Application is running on http://localhost:${PORT}`),
);