import SocketIO , { Socket } from 'socket.io' ; import socketService from '../services/socket' ; import roomCodeOnSocket from '../models/roomCodeOnSocket' ; import socketsInRoom from '../models/socketsInRoom' ; import roomGroup from '../models/roomGroup' ; import validationUtil from '../utils/validation' ; import SocketErrorMessage from '../@types/socketErrorMessage' ; import { UserType , SendChatType } from '../@types/dataType' ; const enterChatroom = async ( socket , io , userData ) => { const { roomCode , nickname , language , imageLink } = userData ; if ( ! validationUtil . isValidRoomCode ( roomCode ) ) return socketService . emitSocketError ( socket , SocketErrorMessage . ROOM_CODE ) ; if ( ! validationUtil . isValidNickname ( nickname ) ) return socketService . emitSocketError ( socket , SocketErrorMessage . NICKNAME ) ; if ( ! validationUtil . isValidLanguage ( language ) ) return socketService . emitSocketError ( socket , SocketErrorMessage . LANGUAGE ) ; if ( ! validationUtil . isValidImageLink ( imageLink ) ) return socketService . emitSocketError ( socket , SocketErrorMessage . IMAGELINK ) ; socket . join ( roomCode ) ; try { await socketService . insertSocketInfoIntoDB ( socket . id , roomCode , nickname , language , imageLink ) ; const participants = await socketService . getParticipants ( roomCode ) ; const participantsIncludeNickname = { participants , type : 'enter' , diffNickname : nickname } ; io . to ( roomCode ) . emit ( 'receive participants list' , participantsIncludeNickname ) ; } catch ( err ) { return socketService . emitSocketError ( socket , SocketErrorMessage . SERVER ) ; } } ; const sendChat = async ( socket , io , sendChat ) => { const { Korean , English } = sendChat ; if ( ! validationUtil . isValidMessage ( sendChat ) ) return socketService . emitSocketError ( socket , SocketErrorMessage . CHAT ) ; try { const { roomCode , receiveChat } = await socketService . createReceiveChat ( socket . id , Korean , English ) ; io . to ( roomCode ) . emit ( 'receive chat' , receiveChat ) ; } catch ( err ) { return socketService . emitSocketError ( socket , SocketErrorMessage . SERVER ) ; } } ; const disconnect = async ( socket , io ) => { try { const roomCode = await roomCodeOnSocket . getRoomCode ( socket . id ) ; const socketInfo = await socketsInRoom . getSocket ( roomCode , socket . id ) ; const { nickname } = JSON . parse ( socketInfo ) ; if ( roomCode !== null ) { await socketService . removeSocket ( socket . id , roomCode ) ; if ( await socketsInRoom . isRoomEmpty ( roomCode ) ) { await roomGroup . removeRoom ( roomCode ) ; return ; } const participants = await socketService . getParticipants ( roomCode ) ; const participantsIncludeNickname = { participants , type : 'leave' , diffNickname : nickname } ; io . to ( roomCode ) . emit ( 'receive participants list' , participantsIncludeNickname ) ; } } catch ( err ) { throw new Error ( err ) ; } } ; const socketError = async ( socket , io ) => { await disconnect ( socket , io ) ; } ; const socketController = { enterChatroom , sendChat , disconnect , socketError , } ; export default socketController ;