Dynamic rooms with Socket.io and Node

前端 未结 1 1155
星月不相逢
星月不相逢 2020-12-23 02:04

I\'m trying to use the new \"room\" feature in Socket.io v.7 to create dynamic chat rooms, but I\'m having problems getting static rooms to work in my example. Based on the

相关标签:
1条回答
  • 2020-12-23 02:43

    You don't seem to be listening to these events

    socket.broadcast.to(room).emit('new fan');
    io.sockets.in(room).emit('new non-fan');
    

    on the client side you need:

    socket.on('new fan', function (data) {
        console.log('new fan');
    });
    

    You're also not sending the message to the clients. Inside:

    socket.on('message', function(data) { }) 
    

    on the server, you need to do :

    io.sockets.in(room).emit('message', data);
    
    0 讨论(0)
提交回复
热议问题