Socket.io - Implementing a user-socket association map for private messaging

前端 未结 4 2015
既然无缘
既然无缘 2021-02-09 23:23

I\'m trying to create a private messaging system using socket.io

In order to associate the users with their sockets, most sites are suggesting something like this:

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-10 00:04

    you can Send additional data on socket connection like this:

     client side :
    
         var c = io.connect('http://localhost:3000/', { query: "userId=value01" });
    
    
    
    server side :
     // extract userId param from connected url
     io.on('connection', function(socket) {
          var socket_param_userId = socket.handshake['query']['userId'];
          console.log(socket_param_userId);  //show value01
    });
    

提交回复
热议问题