发送自定义数据与handshakeData沿socket.io?(Send custom data

2019-06-21 17:02发布

所以,我有正在运行的节点JS与socket.io作为后端和正常JavaScript作为前端的应用程序。 我的应用程序目前只是有客户端,只要它的连接发送其登录数据的登录系统。

现在,我想它会好得多有一个与handshakeData一起发送的登录数据,这样我就可以直接让用户登录而连接(而不是建立连接后)当登录数据无效分别拒绝授权。

我想这将是最好把我的附加数据在handshakeData的头部分,所以任何想法我怎么能做到这一点? (而不必如果可以修改socket.io,但如果它是唯一的方法我可以住在一起)

Answer 1:

正如很多评论人士指出以下Socket.IO API在他们的改变1.0版本。 验证现在应该通过一个中间件的功能来实现,看到“验证差别” @ http://socket.io/docs/migrating-from-0-9/#authentication-differences 。 我会包含我的原单答案的人被困在<1.0为旧文档似乎消失了。

1.0及更高版本:

客户端:

//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" });

服务器端:

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'));
});

1.0前

在第二个参数参数去()连接的客户端,这将是可用的授权方法在服务器上:您可以通过查询。

我刚刚一直在测试它。 在客户端,我有:

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

在服务器上:

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

在服务器上的输出,那么看起来像:

:!node node_app/main.js
   info  - socket.io started
Auth:  { foo: 'bar', t: '1355859917678' }


Answer 2:

这个现在已经在V1.0.0改变。 请参阅迁移文档

基本上,

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

变为:

  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();
});


Answer 3:

对于socket.io V1.2.1使用:

io.use(function (socket, next) {
  var handshake = socket.handshake;
  console.log(handshake.query);
  next();
});


Answer 4:

这是我的查询数据发送到和的NodeJS服务器server.io客户端代码。

var socket = io.connect(window.location.origin, { query: 'loggeduser=user1' });

io.sockets.on('connection', function (socket) {
    var endp = socket.manager.handshaken[socket.id].address;
    console.log("query... " + socket.manager.handshaken[socket.id].query.user);
}


Answer 5:

也许API已更改,但我做了以下获得额外的信息到服务器。

// 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();
});

版画

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

如果有人知道如何从客户端得到数据, 通过不在查询参数的握手服务器让我知道吧。

更新我这个语法跑进后面的问题

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

就是我目前使用。



Answer 6:

我发现了一个小问题看.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
}


Answer 7:

旧线程,但假设您存储JWT令牌/会话ID的会话cookie(标准的东西),这会直接默认做握手(socket.io客户端)我注意到,当传递到服务器反正。 这有什么错刚开的认证信息的握手(通过中间件或on.connection)通过饼干吗? 例如。

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()
  }
}

现在,我使用这个的一个项目,它的正常工作。



文章来源: Send custom data along with handshakeData in socket.io?