Skip to content

Commit

Permalink
Merge pull request #65 from jjikky/refactor/objet
Browse files Browse the repository at this point in the history
패키지 구조 변경 및 리팩토링
  • Loading branch information
jjikky authored Oct 1, 2024
2 parents bee0326 + 37cc342 commit 9269728
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 219 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const https = require('https');
const config = require('./src/common/config');
const expressLoader = require('./src/common/modules/express');
const socketIoLoader = require('./src/socket.loader');
const socketIoLoader = require('./src/handlers/socket.handler');
const logger = require('./src/common/modules/logger');

const app = express();
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
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
132 changes: 0 additions & 132 deletions src/handler/socket.handler.js

This file was deleted.

94 changes: 94 additions & 0 deletions src/handlers/socket.handler.js
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.
36 changes: 36 additions & 0 deletions src/repositories/objet.repository.js
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();
Loading

0 comments on commit 9269728

Please sign in to comment.