在 socket.io 中发送自定义数据和握手数据? [英] Send custom data along with handshakeData in socket.io?

查看:21
本文介绍了在 socket.io 中发送自定义数据和握手数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个运行 node js 的应用程序,socket.io 作为后端,普通 javascript 作为前端.我的应用程序有一个登录系统,目前它只是让客户端在连接后立即发送其登录数据.

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 client send its login data as soon as it's connected.

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

Now I figured it would be much nicer to have the login data sent along with the handshakeData, so I can directly have the user logged in while connecting (instead of after establishing a connection) respectively refuse authorization when the login data is invalid.

我认为最好将我的附加数据放在握手数据的标题部分,那么我有什么想法可以做到吗?(如果可能的话,不必修改 socket.io,但如果这是我可以忍受的唯一方法)

I'm thinking it would be best to put my additional data in the header part of the handshakeData, so any ideas how I could do that? (Without having to modify socket.io if possible, but if it's the only way I can live with it)

推荐答案

正如下面的许多评论指出的那样,Socket.IO API 在其 1.0 版本中发生了变化.现在应该通过中间件功能完成身份验证,请参阅身份验证差异"@http://socket.io/docs/migrating-from-0-9/#authentication-differences.由于旧文档似乎已经消失,我会为任何坚持使用 <1.0 的人提供我的原始答案.

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.

客户端:

//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 之前

您可以将查询:param 中的第二个参数传递给客户端的 connect() ,该参数将在授权方法中的服务器上可用.

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

在服务器上:

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

更新

3.x 及更高版本

您可以在客户端使用 auth 参数作为 connect() 的第二个参数传递身份验证负载.

Update

3.x and later

You can pass an authentication payload using the auth param as the second argument to connect() in the client side.

客户端:

io.connect("http://127.0.0.1:3000/", {
    auth: {
      token: "AuthToken",
    },
  }),

在服务器端,您可以使用 socket.handshake.auth.token

In server side you can access it using socket.handshake.auth.token

服务器端:

io.use(function(socket, next){
    console.log(socket.handshake.auth.token)
    next()
});

这篇关于在 socket.io 中发送自定义数据和握手数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆