socket.io with express

后端 未结 4 862
借酒劲吻你
借酒劲吻你 2021-02-01 07:44

i have a project and I\'m using socket.io with express ,

so what i need (i tried) is broadcasting a message but from an express action. is this possible i don\'t know ho

4条回答
  •  孤街浪徒
    2021-02-01 08:19

    As long as I understand,

    Why not use the socket message type as an event instead of a http get or post? On the client side you would send a message via the websocket with let's say an event property.

    So in your case:

    
    

    And on the server side:

    var io = io.listen(server);
    
    io.on('connection', function (client) {
      client.on('message', function (message) {
        if (message.event == 'homepage loaded') {
          client.broadcast(...);
        }
      });
    });
    

提交回复
热议问题