socket.io client to client messaging

前端 未结 3 832
花落未央
花落未央 2021-01-15 08:22

I\'m having trouble getting basic client to client (or really client->server->client) working with socket.io. Heres the code I have right now:

io.sockets.on(         


        
3条回答
  •  再見小時候
    2021-01-15 08:59

    Socket.io has the concept of rooms where, once a socket has joined a room, it will receive all message sent to a room, so you don't need to track who's in the room, deal with disconnections, etc...

    On connection, you'd use:

    socket.join('room')
    

    And to send a message to everyone in that room:

    io.sockets.in('room').emit('event_name', data)
    

    More info on the socket.io wiki: https://github.com/LearnBoost/socket.io/wiki/Rooms

提交回复
热议问题