Send custom data along with handshakeData in socket.io?

前端 未结 7 1245
迷失自我
迷失自我 2020-11-28 02:18

So I have an application running node js with socket.io as a backend and normal javascript as frontend. My application has a login system which currently simply has the clie

相关标签:
7条回答
  • 2020-11-28 02:37

    For socket.io v1.2.1 use this:

    io.use(function (socket, next) {
      var handshake = socket.handshake;
      console.log(handshake.query);
      next();
    });
    
    0 讨论(0)
  • 2020-11-28 02:38

    Old thread but assuming you store your jwt token/session id in session cookies (standard stuff) this gets passed to the server by default anyway when doing handshake (socket.io-client) I've noticed. Is there anything wrong with just getting the auth information for the handshake (via middleware or on.connection) via cookie? eg.

    io.on('connection', function(socket) {
      // assuming base64url token
      const cookieStr = socket.handshake.headers.cookie
      const matchRes =
        cookieStr == null
          ? false
          : cookieStr.match(/my-auth-token=([a-zA-Z0-9_.-]+)/)
      if (matchRes) {
        // verify your jwt...
        if ( tokenIsGood(matchRes[1]) {
          // handle authenticated new socket
        } else {
          socket.emit('AUTH_ERR_LOGOUT')
          socket.disconnect()
        }
      } else {
        socket.emit('AUTH_ERR_LOGOUT')
        socket.disconnect()
      }
    }
    

    I'm using this now for a project and it's working fine.

    0 讨论(0)
  • 2020-11-28 02:41

    This has now been changed in v1.0.0. See the migration docs

    basically,

    io.set('authorization', function (handshakeData, callback) {
      // make sure the handshake data looks good
      callback(null, true); // error first, 'authorized' boolean second 
    });
    

    becomes :

      io.use(function(socket, next) {
      var handshakeData = socket.request;
      // make sure the handshake data looks good as before
      // if error do this:
        // next(new Error('not authorized');
      // else just call next
      next();
    });
    
    0 讨论(0)
  • 2020-11-28 02:48

    Perhaps the api has changed but I did the following to get extra info to the server.

    // client
    io.connect('localhost:8080', { query: 'foo=bar', extra: 'extra'});
    
    // server
    io.use(function(sock, next) {
      var handshakeData = sock.request;
      console.log('_query:', handshakeData._query);
      console.log('extra:', handshakeData.extra);
      next();
    });
    

    prints

    _query: { foo: 'bar',
      EIO: '3',
      transport: 'polling',
      t: '1424932455409-0' }
    extra: undefined
    

    If anyone knows how to get data from a client to the server through the handshake that is not in the query params let me know please.

    Update I ran into issues later with this syntax

    io.connect('localhost:8080?foo=bar');
    

    is what I'm currently using.

    0 讨论(0)
  • 2020-11-28 02:54

    As a lot of comments have pointed out below the Socket.IO API changed in their 1.0 release. Authentication should now be done via a middleware function, see 'Authentication differences' @ http://socket.io/docs/migrating-from-0-9/#authentication-differences. I'll include my orginal answer for anyone stuck on <1.0 as the old docs seem to be gone.

    1.0 and later:

    Client Side:

    //The query member of the options object is passed to the server on connection and parsed as a CGI style Querystring.
    var socket = io("http://127.0.0.1:3000/", { query: "foo=bar" });
    

    Server Side:

    io.use(function(socket, next){
        console.log("Query: ", socket.handshake.query);
        // return the result of next() to accept the connection.
        if (socket.handshake.query.foo == "bar") {
            return next();
        }
        // call next() with an Error if you need to reject the connection.
        next(new Error('Authentication error'));
    });
    

    Pre 1.0

    You can pass a query: param in the second argument to connect() on the client side which will be available on the server in the authorization method.

    I've just been testing it. On the client I have:

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

    On the server:

    io.set('authorization', function (handshakeData, cb) {
        console.log('Auth: ', handshakeData.query);
        cb(null, true);
    });
    

    The output on the server then looked like:

    :!node node_app/main.js
       info  - socket.io started
    Auth:  { foo: 'bar', t: '1355859917678' }
    
    0 讨论(0)
  • 2020-11-28 02:55

    I found a little problem to see the .loggeduser

    io.sockets.on('connection', function (socket) {
        var endp = socket.manager.handshaken[socket.id].address;
        console.log("query... " + socket.manager.handshaken[socket.id].query.loggeduser);
                                                                             // ↑ here
    }
    
    0 讨论(0)
提交回复
热议问题