在socket.io中发送自定义数据和握手数据?

在socket.io中发送自定义数据和握手数据?,第1张

在socket.io中发送自定义数据和握手数据?

正如下面的许多评论所指出的那样,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版

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

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

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 startedAuth:  { foo: 'bar', t: '1355859917678' }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5126798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存