-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from jjikky/refactor/objet
패키지 구조 변경 및 리팩토링
- Loading branch information
Showing
8 changed files
with
272 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
services: | ||
dao-signaling-server: | ||
build: . | ||
container_name: dao-signaling-server | ||
env_file: | ||
- .env | ||
networks: | ||
- backend-network | ||
ports: | ||
- '8083:8083' | ||
|
||
networks: | ||
backend-network: | ||
external: true |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const axios = require('axios'); | ||
const https = require('https'); | ||
const config = require('../common/config'); | ||
const logger = require('../common/modules/logger'); | ||
const { getResponseTimeMs } = require('../common/utils/time.util'); | ||
const handshakeMiddleware = require('../middlewares/handshake'); | ||
const { | ||
handleObjetJoin, | ||
handleOffer, | ||
handleAnswer, | ||
handleCandidate, | ||
handleDisconnect, | ||
} = require('../services/socket.service'); | ||
|
||
module.exports = socketIoLoader = (io) => { | ||
io.use(handshakeMiddleware); | ||
|
||
io.on('connection', async (socket) => { | ||
const socket_id = socket.id; | ||
const { token, lounge_id } = socket.handshake.query; | ||
const startTime = process.hrtime(); | ||
|
||
validateHandShake(token, lounge_id); | ||
|
||
try { | ||
await validateRequest(token, lounge_id, socket); | ||
logSuccess(socket, lounge_id, socket_id, startTime); | ||
} catch (err) { | ||
handleError(err, socket, lounge_id, socket_id, startTime); | ||
return; | ||
} | ||
|
||
setupSocketEvents(socket, io, socket_id); | ||
}); | ||
}; | ||
|
||
const validateRequest = async (token, lounge_id, socket) => { | ||
return axios.post( | ||
`${config.springServerUrl}/lounges/validate`, | ||
{ lounge_id }, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'x-request-id': socket.xRequestId, | ||
}, | ||
httpsAgent: new https.Agent({ | ||
rejectUnauthorized: false, | ||
}), | ||
} | ||
); | ||
}; | ||
|
||
const logSuccess = (socket, lounge_id, socket_id, startTime) => { | ||
const responseTime = getResponseTimeMs(startTime); | ||
logger.info( | ||
`[연결 성공] - 라운지 ID: ${lounge_id}, 소켓 ID: ${socket_id}, x-request-id: ${socket.xRequestId}, 응답 시간: ${responseTime}ms` | ||
); | ||
}; | ||
|
||
const setupSocketEvents = (socket, io, socket_id) => { | ||
socket.on('join_objet', async (data) => { | ||
await handleObjetJoin(io, socket, data, socket_id); | ||
}); | ||
|
||
socket.on('offer', (data) => { | ||
handleOffer(socket, data); | ||
}); | ||
|
||
socket.on('answer', (data) => { | ||
handleAnswer(socket, data); | ||
}); | ||
|
||
socket.on('candidate', (data) => { | ||
handleCandidate(socket, data); | ||
}); | ||
|
||
socket.on('disconnect', async () => { | ||
await handleDisconnect(socket, socket_id); | ||
}); | ||
}; | ||
|
||
const validateHandShake = (token, lounge_id) => { | ||
if (!token || !lounge_id) { | ||
return handleMissingParams(socket, socket_id); | ||
} | ||
}; | ||
|
||
const handleMissingParams = (socket, socket_id) => { | ||
logger.warn(`[연결 실패] - 토큰 또는 라운지 ID가 없습니다. 소켓 ID: ${socket_id}`); | ||
socket.emit('error_message', { | ||
error: '토큰 또는 라운지 ID가 없습니다', | ||
}); | ||
socket.disconnect(true); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const redisCli = require('../common/modules/redis'); | ||
|
||
class ObjetRepository { | ||
async getUsersInObjet(objetKey) { | ||
return await redisCli.lRange(objetKey, 0, -1); | ||
} | ||
|
||
async addUserToObjet(objetKey, user) { | ||
return await redisCli.rPush(objetKey, JSON.stringify(user)); | ||
} | ||
|
||
async setSocketToObjet(socketKey, objet_id) { | ||
return await redisCli.set(socketKey, String(objet_id)); | ||
} | ||
|
||
async getObjetBySocket(socketKey) { | ||
return await redisCli.get(socketKey); | ||
} | ||
|
||
async deleteSocket(socketKey) { | ||
return await redisCli.del(socketKey); | ||
} | ||
|
||
async deleteObjet(objetKey) { | ||
return await redisCli.del(objetKey); | ||
} | ||
|
||
async updateUsersInObjet(objetKey, usersInObjet) { | ||
await redisCli.del(objetKey); | ||
for (const user of usersInObjet) { | ||
await redisCli.rPush(objetKey, user); | ||
} | ||
} | ||
} | ||
|
||
module.exports = new ObjetRepository(); |
Oops, something went wrong.