-
항해 99 5기 TIL_81항해 99 2022. 4. 1. 17:16
▶ Today I Learned
<실전 프로젝트>
[방에 사람이 있는지 없는지 일정 주기로 체크하기]
setInterval()라는 함수를 이용하면 가능하다
function checkLeftUser (id) { console.log("socket id socket id socket id",socket.id); console.log("users[roomId] users[roomId] users[roomId]", users[roomId]); console.log("users[roomId].includes(id) 다ㅏㅏㅏㅏㅏㅏ",users[roomId].includes(id)) } setInterval(checkLeftUser, 5000, socket.id)
socket.js 코드
socket.on("disconnecting", async () => { const data = { roomId, userId, time: time, categoryId: categoryId, date: date, }; console.log("socket disconnecting이 실행된다. 이다음은 await RoomController.delete.participant(data);이다ㅏㅏㅏ") console.log("socket time은 여기다ㅏㅏㅏㅏㅏㅏㅏㅏㅏ", time); console.log("socket data socket data socket data socket data ", data); await RoomController.delete.participant(data); if(users[roomId]) { users[roomId] = users[roomId].filter((id) => id !== socket.id); } const userInfo = socketToUser[socket.id]; socket.broadcast.to(roomId).emit("user left", { socketId: socket.id, userInfo, }); delete socketToNickname[socket.id]; delete socketToUser[socket.id]; }); // 정말로 방에서 나갔는지 확인 function checkLeftUser (id) { console.log("socket id socket id socket id",socket.id); console.log("users[roomId] users[roomId] users[roomId]", users[roomId]); console.log("users[roomId].includes(id) 다ㅏㅏㅏㅏㅏㅏ",users[roomId].includes(id)) } setInterval(checkLeftUser, 5000, socket.id)
console.log 결과
users[roomId] users[roomId] users[roomId] [ 'J1Xr6mKUIrxIa9YsAAAB', '6v9YKWn9Fsb56PZzAAAD' ] users[roomId].includes(id) 다ㅏㅏㅏㅏㅏㅏ true ... socket disconnecting이 실행된다. 이다음은 await RoomController.delete.participant(data);이다ㅏㅏㅏ socket time은 여기다ㅏㅏㅏㅏㅏㅏㅏㅏㅏ 1 ... users[roomId] users[roomId] users[roomId] [ '6v9YKWn9Fsb56PZzAAAD' ] users[roomId].includes(id) 다ㅏㅏㅏㅏㅏㅏ false
뒤로 가기로 방을 나갔을 땐 socket.id도 특정 방에서 삭제되지 않았다..
즉, socket disconnecting가 되기 전까지는 socket.id가 방에서 삭제되지 않는다는 것.
socket.io의 새로운 특성을 알았다.
참고자료:
https://offbyone.tistory.com/241
https://ko.javascript.info/settimeout-setinterval
https://yonghyunlee.gitlab.io/node/node-schedule/
▶ 느낀 점
우여곡절이 많았지만 결국 서비스 1차 배포를 완료하게 되었다.
내일 정리하여 여러 사람들에게 선보일 생각을 하니
막상 이러한 결과물을 만들어 내는데 일조했다는 것이 뿌듯했다.
개발자는 세상을 만들어가는 사람들이다.
나와 모두의 기술로 원하는 것을 구현해내는 그 매력에, 나는 개발자를 시작하게 된 것 같다.
스트레스도 받고 때론 힘들어도 문제를 해결하고자 고민하는 과정도 나름 재밌고 해결했을 떄의 보람은 정말 크다.
앞으로도 열심히 해서 세상을 만들어나가는 사람이 되자.
▶ 공부 시 참고 링크들
https://yohanpro.com/posts/js/node-path
https://jokergt.tistory.com/81
onDelete: "CASCADE" 에 대한 결론
https://brunch.co.kr/@dan-kim/26
카카오톡 로그인에 쓰이는 패스포트
https://www.npmjs.com/package/passport
카카오톡 로그인 기본적인 방법
https://devlog-wjdrbs96.tistory.com/232
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_83 (0) 2022.04.03 항해 99 5기 TIL_82 (0) 2022.04.02 항해 99 5기 TIL_80 (0) 2022.03.31 항해 99 5기 TIL_79 (0) 2022.03.30 항해 99 5기 TIL_78 (0) 2022.03.29