Send custom data along with handshakeData in socket.io?

前端 未结 7 1275
迷失自我
迷失自我 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: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.

提交回复
热议问题