I believe this API of socket.io
is deprecated. The solution would be to change its use
of the method Server.prototype.set
for Server.prototype.use
, that will plant a middleware like those of the express.
There is an updated version of the examples of Caio Ribeiro Pereira’s book.
I think that that example
demonstrates what you are trying to do by using Server.prototype.use
:
I found an article from socket.io
explaining the differences since version 0.9,
specifically with regard to authentication, I suggest you take a look at here.
Your code should work with this little change of set
for use
:
io.use(function(socket, next) {
var data = socket.request;
cookieSec(data, {}, function(err) {
var sessionID = data.signedCookies[KEY];
SesMemStore.get(sessionID, function(err, session) {
if (err || !session) {
return next(new Error('acesso negado'));
} else {
socket.handshake.session = session;
return next();
}
});
});
});
Of the documentation of socket.io
, freely translated into English:
Namespace#use(fn:Function):Namespace
Logs a middleware; a function that runs for every connection (Socket
) that arrives and receives as parameters the socket and a function to optionally pass to execute the next middleware
registered.
Here, Namespace#use(fn:Function):Namespace
, means that this is a method about the class Namespace
, which takes a function as argument and returns a Namespace
, for chaining. It is the default to return the this
to be able to chain methods (another example would be the superagent
: request.get(url).query(query).end(fn);
).
This may seem confusing, because the object io
in your code is an instance of the class Server
, not of class Namespace
. The idea is that you can create multiple objects Namespace
, exactly how you can create multiple objects Router
in the express
4, each with its own set of handlers and middleware.
By default, Server.prototype.use
will call Namespace.prototype.use
about the Namespace
standard, which handles the connections to /
. This is similar to the relationship between the Application
and the Router
of express
.
As for the second error, you have tried to send the headers after the content, which is impossible by HTTP. As for the socket.io / express let us know what version it is using so we can know.
– Olimon F.
^1.2.1 is the version of my socket.io. As for the late header, why this is occurring since it is a process that is not normally manipulated?
– ropbla9
@Olimonf. I forgot to mark you
– ropbla9