栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号