-
Log out event with socketIOProject using node.js/Cloning Catch-Mind 2020. 12. 17. 14:18
Delete the socket which is logout.
// globalController.js export const logout = async (req, res) => { const { user: { id }, } = req; const logoutUser = await User.findById(id); const itemIdx = sockets.findIndex( (aSocket) => aSocket.username === logoutUser.username ); if (itemIdx > -1) sockets.splice(itemIdx, 1); io.emit(events.disconnected, { username: logoutUser.username }); sendPlayerUpdate(); req.logout(); res.redirect(routes.home); };
// events.js disconnected: "disconnected",
User가 로그아웃을 할 때, sockets 배열에서 해당 유저를 삭제하고 Player 상태를 업데이트 한다.
Player board에 player 정보 생성
// sockets.js import { handledisConnected } from "./notifications"; socket.on(events.disconnected, handledisConnected);
// notifications.js export const handledisConnected = ({ username }) => { localStorage.removeItem(USERANME); const text = `${username} just left!`; const color = "rgb(255, 149, 0)"; fireNotification(text, color); };
login을 했을 때와 마찬가지로 notification을 하고 localStorage에서 USERNAME을 삭제한다.
소스 코드
github.com/zpskek/guessMind-v3/commit/c3851b3b369cf63960f50775adb61eddb1db0a46
'Project using node.js > Cloning Catch-Mind' 카테고리의 다른 글
Game end event (0) 2020.12.18 Game Start event (0) 2020.12.18 Player update (0) 2020.12.17 Erase canvas with socketIO (0) 2020.12.17 Fill the canvas with socketIO (0) 2020.12.17