socket.io client to client messaging

前端 未结 3 831
花落未央
花落未央 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:57

    Try

    users.push(socket); // without .sessionId
    
    for (var u in users)    {
       // users[u] is now the socket
       console.log(users[u].id);
       users[u].emit('message', { msg: 'New User Connected succesfully' });
    }
    
    0 讨论(0)
  • 2021-01-15 08:58

    You can now also use ...

    io.to('room').emit('event_name', data);
    

    as an alternative to io.sockets.in

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题