server code:
//run with node-dev server.js
var remoteServer = io.of('/remote');
remoteServer.authorization(function(handshakeData, callback){
return callback('unknown clientType', false);
}
the server log:
You can visit your app with http://localhost:3000
info - handshake authorized l4FzYiWpHo2d8VeoB3Zo
warn - handshake error unknown clientType for /remote
client code:
//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);
});
the client log:
//error reason miss.
E:\Workspace\TZ\SmartDoor\client>node-dev client.js
error:
authorization
in Socket.IO is middleware to handle Handshake process based on HTTP Header data that is sent with first HTTP Switch Protocols header in case of WebSockets.
This data contains normal header stuff as well as cookies and is used to restore session sometimes.
Although authorization
middleware happens before actual WebSockets handshake process has been accomplished, and this middleware should not be used for application logic authorization, but only for networking and http stuff (like cookies for restoring session, banned IP, etc).
So I will recommend of not using this middleware for application logic as it is actually protocol specific as well (Socket.IO uses many protocols and communication layers like WebSockets, XHR Long Polling, AJAX, etc). But do your authentication logic after connection is successfully established.