Answer the question
In order to leave comments, you need to log in
How to catch authorization error on custom namespace in Socket.IO 1.4.8?
UPD : for those interested , a link to the docks
Based on the requirements of a small project, I decided to use Socket.IO. I have not worked with it before, although I have known NodeJS for a long time. I wrote some code, everything works as it should, but you just need to double-check everything that is in third-party guides, because they often refer to the old version (<1.0). And at this moment there was a gag with authorization.
Authorization is implemented through middleware, all according to the current version of the official documentation:
io.use((socket, next)=>{
var sidCookie = cookie.parse(socket.headers.cookie || '')['sid'];
if(sidCookie === undefined){
return next(new Error('not authorized'));
}
//........
});
socket.on('error', function(error){
if(error == "not authorized"){
//Redirect to login page
}
});
engine.io-client: socket socket receive: type "message", data "4"not authorized"" +5ms
socket.io-parser decoded 4"not authorized" as {"type":4,"nsp":"/","data":"not authorized"} +4ms
engine.io-client: polling polling +1ms
engine.io-client: polling-xhr xhr poll +1ms
engine.io-client: polling-xhr xhr open GET: http://test.com:10480/socket.io/EIO=3&transport=polling&t=LPLiIDy&sid=FihJFgV2CDLHdmY2AAAH +0ms
engine.io-client: polling-xhr xhr data null +1ms
io.of('/test/nsp').use((socket, next)=>{
//........
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question