From 334203da06228f3c2221e2dba9dfb7e9023635a0 Mon Sep 17 00:00:00 2001 From: newtypersh Date: Fri, 13 Jun 2025 22:26:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feature:=20=EC=9B=B9=EC=86=8C=EC=BC=93=20?= =?UTF-8?q?=EC=9E=84=EC=8B=9C=20=EC=BD=94=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- src/ws-study-status-server.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/ws-study-status-server.ts diff --git a/package.json b/package.json index 89da04a..a86ea5a 100644 --- a/package.json +++ b/package.json @@ -32,13 +32,14 @@ "date-fns": "^4.1.0", "date-fns-tz": "^3.2.0", "dotenv": "^16.4.7", - "express": "^5.0.0-0", + "express": "^5.0.1", "express-session": "^1.18.1", "http-status-codes": "^2.3.0", "mqtt": "^5.11.1", "passport": "^0.7.0", "passport-kakao": "^1.0.1", "prisma": "^6.1.0", + "socket.io": "^4.8.1", "swagger-jsdoc": "^6.2.8", "swagger-ui-express": "^5.0.1", "ws": "^8.18.1" diff --git a/src/ws-study-status-server.ts b/src/ws-study-status-server.ts new file mode 100644 index 0000000..881b03c --- /dev/null +++ b/src/ws-study-status-server.ts @@ -0,0 +1,33 @@ +import express, {Express} from 'express'; +import {createServer} from 'http'; +import {Server as SocketIOServer, Socket} from 'socket.io'; + +const app: Express = express(); +const httpServer = createServer(app); +const io = new SocketIOServer(httpServer, { + cors: {origin: '*'}, +}); + +// REST API 예시 라우트 +app.get('/api/hello', (req, res) => { + res.json({message: 'Hello from REST API!'}); +}); + +let timer = 0; +const status = '게임중'; + +// 1초마다 타이머 값 증가 및 브로드캐스트 +setInterval(() => { + timer += 1; + io.emit('timer', {status, timer}); +}, 1000); + +io.on('connection', (socket: Socket) => { + // 클라이언트가 새로 접속하면 현재 상태 전송 + socket.emit('timer', {status, timer}); +}); + +const PORT = 3000; // REST와 웹소켓 모두 3000번 포트에서 운영 +httpServer.listen(PORT, () => { + console.log(`서버가 ${PORT} 포트에서 REST와 WebSocket을 함께 운영 중`); +}); From fbfdde288f321a22483eb096e8b7dc0997353cac Mon Sep 17 00:00:00 2001 From: newtypersh Date: Fri, 13 Jun 2025 22:34:18 +0900 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20express=20=EB=B2=84=EC=A0=84=20?= =?UTF-8?q?=EB=8B=AC=EB=9D=BC=EC=A7=84=20=EA=B2=83=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a86ea5a..ca306b3 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "date-fns": "^4.1.0", "date-fns-tz": "^3.2.0", "dotenv": "^16.4.7", - "express": "^5.0.1", + "express": "^5.0.0-0", "express-session": "^1.18.1", "http-status-codes": "^2.3.0", "mqtt": "^5.11.1", From 349b4687ab11aaac04920a7aec40f9ee258effe8 Mon Sep 17 00:00:00 2001 From: newtypersh Date: Fri, 13 Jun 2025 22:36:20 +0900 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=EC=9B=B9=EC=86=8C=EC=BC=93=20impo?= =?UTF-8?q?rt=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app.ts b/src/app.ts index b6d4691..39155b5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -18,6 +18,7 @@ import cookieParser from 'cookie-parser'; import {prisma} from './db.config.js'; import {RegisterRoutes} from './routers/tsoaRoutes.js'; import {authRouter} from './routers/auth.router.js'; +import './ws-study-status-server.js'; dotenv.config();