Update all clients using Socket.io?

前端 未结 4 1817
故里飘歌
故里飘歌 2020-12-02 04:39

Is it possible to force all clients to update using socket.io? I\'ve tried the following, but it doesn\'t seem to update other clients when a new client connects:

S

4条回答
  •  感动是毒
    2020-12-02 05:12

    I found that using socket.broadcast.emit() will only broadcast to the current "connection", but io.sockets.emit will broadcast to all the clients. here the server is listening to "two connections", which are exactlly 2 socket namespaces

    io.of('/namespace').on('connection', function(){
        socket.broadcast.emit("hello");
    });
    io.of('/other namespace').on('connection',function(){/*...*/});
    

    i have try to use io.sockets.emit() in one namespace but it was received by the client in the other namespace. however socket.broadcast.emit() will just broadcast the current socket namespace.

提交回复
热议问题