how to get socket.id of a connection on client side?

后端 未结 4 1502
情歌与酒
情歌与酒 2021-01-31 06:05

Im using the following code in index.js

io.on(\'connection\', function(socket){
console.log(\'a user connected\');
console.log(socket.id);
});

4条回答
  •  后悔当初
    2021-01-31 06:25

    To get client side socket id for Latest socket.io 2.0 use the code below

     let socket = io(); 
     //on connect Event 
     socket.on('connect', () => {
         //get the id from socket
         console.log(socket.id);
     });
    

提交回复
热议问题