Get the client id of the message sender in socket.io?

前端 未结 2 1743
盖世英雄少女心
盖世英雄少女心 2021-02-07 16:19

on the server side i got

socket.on(\'chat\', function (data) {
    io.sockets.socket(data.clientid).emit(\'chat\', {
        msg: data.msg,
        senderid : se         


        
2条回答
  •  滥情空心
    2021-02-07 16:48

    It turns out I can just use socket.id of the user sending through a msg to get their clientid, such as:

      socket.on('chat', function (data) {
          io.sockets.socket(data.clientid).emit('chat', {
              msg: data.msg,
              senderid : socket.id
          }); 
      });
    

    Originally I thought I could just fetch the clientid by doing :

      io.sockets.on('connection', function (socket) {
         clientid = socket.id;
    

    But clientid will be the last person who connected in this instance, not the last person who sent through a chat

提交回复
热议问题