Send additional data on socket connection

前端 未结 7 492
清酒与你
清酒与你 2020-12-24 10:12

How to best send additional data upon socket connection?

Client:

socket.on(\'connect\',function(){ 
//I\'d like set some values and pass them up to t         


        
7条回答
  •  既然无缘
    2020-12-24 10:47

    You should send your data either in connect or on create:

    var s = io('http://216.157.91.131:8080/', { query: "foo=bar" });
    s.connect();
    
    var c = io.connect('http://216.157.91.131:8080/', { query: "foo=bar" });
    

    With the new version of socket.io, on server is where the things have been changed:

    var io = require('socket.io')(server);
    
    io.use(function(socket, next) {
      var handshakeData = socket.request;
      console.log("middleware:", handshakeData._query['foo']);
      next();
    });
    

提交回复
热议问题