socket.io@0.9.9 socket.io-client@0.9.9 express@3.0.0rc1 服务器代码: //run with node-dev server.jsvar remoteServer = io.of('/remote');remoteServer.authorization(function(handshakeData, callback){ return callback('unknown clientType',
> socket.io-client@0.9.9
> express@3.0.0rc1
服务器代码:
//run with node-dev server.js var remoteServer = io.of('/remote'); remoteServer.authorization(function(handshakeData, callback){ return callback('unknown clientType', false); }
服务器日志:
You can visit your app with http://localhost:3000 info - handshake authorized l4FzYiWpHo2d8VeoB3Zo warn - handshake error unknown clientType for /remote
客户代码:
//run with node-dev client.js var io = require('socket.io/node_modules/socket.io-client'); var client = io.connect('http://localhost:3000/remote'); client.on('connect_failed', function(reason){ console.log('connect_failed:', reason); }); //will call this because it's Namespace authorization client.on('error', function(reason){ console.log('error:', reason); });
客户日志:
//error reason miss. E:\Workspace\TZ\SmartDoor\client>node-dev client.js error:Socket.IO中的授权是处理基于HTTP头数据的握手过程的中间件,该数据在WebSockets的情况下与第一个HTTP Switch Protocols头一起发送.
此数据包含正常的标题内容以及Cookie,有时用于恢复会话.
虽然授权中间件发生在实际的WebSockets握手过程完成之前,但是这个中间件不应该用于应用程序逻辑授权,而只能用于网络和http的东西(比如用于恢复会话的cookie,禁止的IP等).
所以我建议不要将这个中间件用于应用程序逻辑,因为它实际上也是协议特定的(Socket.IO使用许多协议和通信层,如WebSockets,XHR Long Polling,AJAX等).但是在成功建立连接后进行身份验证逻辑.