Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Meteor.publish("usersonline", function(ghash) {
- if(ghash && ghash !== "") {
- userId = this.userId;
- if(userId) {
- console.log('NEW User in group: ghash: ' + ghash + ' userId: ' + userId);
- UsersOnline.update({ghash: ghash}, { $addToSet: {users: userId} }, {upsert: true});
- } else {
- console.log('NEW User (ANON) in group: ghash: ' + ghash + ' userId: ' + userId);
- UsersOnline.update({ghash: ghash}, { $inc: {users_anon: 1} }, {upsert: true});
- }
- // Build array of current subscriptions
- var currentsubs = this._session.socket.meteor_session._namedSubs;
- var usersonlinesubs = new Array();
- _.each(currentsubs, function(sub, key, list) {
- if(sub._name === 'usersonline')
- usersonlinesubs.push({ id: sub._subscriptionId, ghash: sub._params[0], userId: sub._session.userId });
- });
- //console.log(usersonlinesubs);
- // This is required, because otherwise each time the publish function is called,
- // the events re-bind and the counts will start becoming ridiculous as the functions
- // are called multiple times!
- if(this._session.socket) {
- if(this._session.socket._events.data.length === 1) {
- console.log('DA IS WAS PASSSSIIIIIIEEEEERT');
- this._session.socket.on("data", Meteor.bindEnvironment(function(data) {
- var method = JSON.parse(data).method;
- var submsg = JSON.parse(data).msg;
- var subid = JSON.parse(data).id;
- //console.log(JSON.parse(data));
- // check if there is an UNsub from an old group because of template/ghash change
- if(submsg === 'unsub') {
- _.each(usersonlinesubs, function(sub, key, list) {
- if(subid === sub.id) {
- if(sub.userId) {
- console.log('USER changed group from ' + sub.ghash + ' ! User: ' + sub.userId)
- UsersOnline.update({ghash: sub.ghash}, { $pull: {users: sub.userId} });
- } else {
- console.log('ANON User changed group from ' + sub.ghash + ' ! anon -1')
- UsersOnline.update({ghash: sub.ghash}, { $inc: {users_anon: -1} });
- }
- console.log('UsersOnline Coll after GROUP CHANGE:');
- console.log(UsersOnline.find().fetch());
- }
- });
- }
- // If a user is logging in, dec anon. Don't need to add user to set,
- // because when a user logs in, they are re-subscribed to the collection,
- // so the publish function will be called again.
- // Similarly, if they logout, they re-subscribe, and so the anon count
- // will be handled when the publish function is called again - need only
- // to take out the user ID from the users array.
- if(method === 'login') {
- console.log('logged in! anon: -1');
- UsersOnline.update({ghash: ghash}, { $inc: {users_anon: -1} });
- // If a user is logging out, remove from set
- } else if(method === 'logout') {
- console.log('logout! userId: ' + userId);
- UsersOnline.update({ghash: ghash}, { $pull: {users: userId} });
- }
- }, function(e) {
- console.log(e);
- }));
- this._session.socket.on("close", Meteor.bindEnvironment(function() {
- console.log('Connection close in group ' + ghash + '!');
- if(userId === null || userId === undefined) {
- console.log('Connection closed! anon: -1');
- UsersOnline.update({ghash: ghash}, { $inc: {users_anon: -1} });
- } else {
- console.log('Connection closed by user ' + userId);
- UsersOnline.update({ghash: ghash}, { $pull: {users: userId} });
- }
- console.log('UsersOnline Coll after CLOSE:');
- console.log(UsersOnline.find().fetch());
- }, function(e) {
- console.log("close error", e);
- }));
- }
- }
- console.log(UsersOnline.find().fetch());
- console.log('==============================================');
- }
- return UsersOnline.find({});
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement