Reputation: 11
I'm trying to group all my socket.io connection into groups. I want 1 group for each sails.js session. My first goal is authentificate all tabs in a same time.
So I tried to do this with onConnect
in config/sockets.js
like that :
onConnect: function(session, socket) {
// By default: do nothing
// This is a good place to subscribe a new socket to a room, inform other users that
// someone new has come online, or any other custom socket.io logic
if (typeof session.socket == 'undefined'){
session.socket = [];
}
session.socket.push(socket.id);
session.save();
console.log(session, socket);
},
// This custom onDisconnect function will be run each time a socket disconnects
onDisconnect: function(session, socket) {
// By default: do nothing
// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
if(Array.isArray(session.socket)){
var i = session.socket.indexOf(socket.id);
if(i != -1) {
session.socket.splice(i, 1);
session.save();
}
}
console.log(session, socket);
},
But I realize that session doesn't save my modifications.
I tried a session.save
but sailsjs doesn't know req
!
Session.set(sessionKey, req.session, function (err) {
I want to access to sails.js sesion but I don't know how to do it.
I tried to search a solution but now, after 6 hours of search I think it's time to requiered some help !
Thanks and sorry for my poor english (I'm french).
Upvotes: 1
Views: 660
Reputation: 24948
There appears to be a bug in the implementation of onConnect
and onDisconnect
in Sails v0.9.x. You can work around it for now by adding the following line before a call to session.save
in those methods:
global.req = {}; global.req.session = session;
then changing session.save()
to:
session.save(function(){delete global.req;});
That will provide the missing req
var as a global, and then delete the global (for safety) after the session is saved.
Note that this issue only affects sessions in the onConnect
and onDisconnect
methods; inside of controller code session.save
should work fine.
Thanks for pointing this out!
Upvotes: 1