Skip to content

Commit 033b052

Browse files
Merge pull request #57 from Front-line-dev/refactor/server-socket-io
refactor: socket 코드 부분 리팩토링
2 parents 937eee3 + ef21ab8 commit 033b052

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/backend/sockets/chat.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import Games from '@game/Games';
22

3-
export const onSendChat = (socket, { message }) => {
3+
function onSendChat({ message }) {
4+
const socket = this;
45
const roomID = Games.findRoomID(socket.id);
56
if (!roomID) return;
67

78
const { nickname } = Games.findUserInfo(socket.id);
89
socket.in(roomID).emit('send chat', { message, nickname });
9-
};
10+
}
11+
12+
export default function onChat(socket) {
13+
socket.on('send chat', onSendChat);
14+
}

src/backend/sockets/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import io from 'socket.io';
2-
import { onJoinPlayer } from './waitingRoom';
3-
import { onSendChat } from './chat';
2+
import onWaitingRoom from './waitingRoom';
3+
import onChat from './chat';
44

55
const socketIO = io();
66

77
socketIO.on('connection', (socket) => {
88
console.log('a user connected');
99

10-
socket.on('join player', (params) => onJoinPlayer(socket, { ...params }));
11-
socket.on('send chat', (params) => onSendChat(socket, { ...params }));
10+
onWaitingRoom(socket);
11+
onChat(socket);
1212
});
1313

1414
export default socketIO;

src/backend/sockets/waitingRoom.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Games from '@game/Games';
22

3-
export const onJoinPlayer = (socket, { roomID }) => {
3+
function onJoinPlayer({ roomID }) {
4+
const socket = this;
45
if (!roomID || !Games.isEnterableRoom(roomID)) return;
56

67
Games.enterUser({ socketID: socket.id, roomID });
@@ -11,4 +12,8 @@ export const onJoinPlayer = (socket, { roomID }) => {
1112
roomID,
1213
players: [],
1314
});
14-
};
15+
}
16+
17+
export default function onWaitingRoom(socket) {
18+
socket.on('join player', onJoinPlayer);
19+
}

0 commit comments

Comments
 (0)