Skip to content

[Dev Log] socket.io 룸 생성과 입장 및 메세지 전달 프로세스 #376

Description

@seanswim

오늘은 어떻게 프로젝트에 기여했나요?
DM에서 메세지가 소켓에 연결된 모든 사람에게 전달되는 오류를 해결중이다.

소켓에서 메세지를 전체로 보내는 이유는 아래의 코드 때문이다.

socket.broadcast() ==> 해당 메소드는 '나'를 제외한 나머지의 사람들에게 전체 메세지를 보낸다.
io.emit() ==> 해당 메소드는 소켓에 접속해있는 모든이들에게 메세지를 보낸다.

우리 어플에 필요한 기능은 소수의 클라이언트끼리의 대화이므로 '룸'을 설정해줘야 한다.
프로세스는 아래와 같다.

  1. 클라이언트에서 서버에게 룸 입장 요청

CLIENT ==> socket.emit("join-room") ==> SERVER ==> socket.on("join-room", () => socket.join(room))

  1. 클라이언트는 서버에게 메세지를 보낸다. 이때 별도의 룸 설정은 필요하지 않다. 룸 별로 메세지를 다시 보내는 건 서버의 역할

CLIENT ==> socket.emit("send-message", message) ==> SERVER ==> socket.on("send-message", () => io.to(room).emit(~))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions