From d6e0eb4d366178e7ddcbdd62b2733da4ab074c28 Mon Sep 17 00:00:00 2001 From: HyunJun Mun <137624597+Mouon@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:28:22 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=ED=8C=8C=EC=9D=BC=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1,=20=EC=9D=B4=EC=8A=88=20=EC=97=85=EB=A1=9C=EB=93=9C?= =?UTF-8?q?=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 684ff8b..b03365f 100644 --- a/server.js +++ b/server.js @@ -14,7 +14,7 @@ let webServer; let socketServer; let expressApp; let mediasoupRouter; - +let io; const roomManager = new RoomManager(); (async () => { @@ -106,6 +106,25 @@ async function runExpressApp() { next(); } }); + + expressApp.post('/api/broadcast', (req, res) => { + const { studyroomId, memberId, event, data } = req.body; + console.log(`Broadcasting event: ${event} for studyroom: ${studyroomId}, member: ${memberId}`); + + if (!io) { + console.error('Socket.IO not initialized'); + return res.status(500).send('Internal server error: Socket.IO not initialized'); + } + + try { + io.to(studyroomId.toString()).emit(event, { memberId, response: data }); + res.status(200).send('Broadcast successful'); + } catch (error) { + console.error('Error broadcasting:', error); + res.status(500).send('Internal server error: ' + error.message); + } + }); + } async function runWebServer() { @@ -141,7 +160,7 @@ async function runSocketServer() { path: '/socket.io', log: false, }); - + io = socketServer; socketServer.on('connection', async (socket) => { console.log('client connected'); @@ -369,6 +388,20 @@ async function runSocketServer() { callback({ error: 'Failed to resume consumer' }); } }); + + socket.on('fileUploaded', (data) => { + const { memberId, response } = data; + console.log(`Member ${memberId} uploaded a file:`, response); + // UI 업데이트 또는 알림 표시 로직 추가 + alert(`Member ${memberId} uploaded a new file: ${response.dataName}`); + }); + + socket.on('issueUploaded', (data) => { + const { memberId, response } = data; + console.log(`Member ${memberId} uploaded a issue:`, response); + // UI 업데이트 또는 알림 표시 로직 추가 + alert(`Member ${memberId} uploaded a new issue: ${response.dataName}`); + }); } catch (error) { console.error('Error in socket connection:', error); @@ -489,5 +522,6 @@ module.exports = { runExpressApp, runWebServer, runSocketServer, - runMediasoupWorker + runMediasoupWorker, + io }; \ No newline at end of file