Sending messages to all browsers with socket io

后端 未结 1 997
甜味超标
甜味超标 2021-02-01 10:41

I am messing around with socket.io and node.js, and was wondering how to accomplish the following: I have a simple form that sends a text string to the server, and the server se

1条回答
  •  被撕碎了的回忆
    2021-02-01 11:35

    Change

    socket.emit('get_message',data);
    

    To

    socket.broadcast.emit('get_message',data);
    

    To broadcast it to all connected users, except to the socket where you are calling the broadcast on.

    If you also want to include that socket you can do

    io.sockets.emit('get_message', data);
    

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