Advertisement
Guest User

Checking number of clients per group

a guest
Sep 10th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Meteor.publish("usersonline", function(ghash) {
  2.         if(ghash && ghash !== "") {
  3.             userId = this.userId;
  4.             if(userId) {
  5.                 console.log('NEW User in group: ghash: ' + ghash + ' userId: ' + userId);
  6.                 UsersOnline.update({ghash: ghash}, { $addToSet: {users: userId} }, {upsert: true});
  7.             } else {
  8.                 console.log('NEW User (ANON) in group: ghash: ' + ghash + ' userId: ' + userId);
  9.                 UsersOnline.update({ghash: ghash}, { $inc: {users_anon: 1} }, {upsert: true});
  10.             }
  11.  
  12.             // Build array of current subscriptions
  13.             var currentsubs = this._session.socket.meteor_session._namedSubs;
  14.             var usersonlinesubs = new Array();
  15.             _.each(currentsubs, function(sub, key, list) {
  16.                 if(sub._name === 'usersonline')
  17.                     usersonlinesubs.push({ id: sub._subscriptionId, ghash: sub._params[0], userId: sub._session.userId });
  18.             });
  19.             //console.log(usersonlinesubs);
  20.            
  21.             // This is required, because otherwise each time the publish function is called,
  22.             // the events re-bind and the counts will start becoming ridiculous as the functions
  23.             // are called multiple times!
  24.             if(this._session.socket) {
  25.                 if(this._session.socket._events.data.length === 1) {
  26.                     console.log('DA IS WAS PASSSSIIIIIIEEEEERT');
  27.                     this._session.socket.on("data", Meteor.bindEnvironment(function(data) {
  28.                         var method = JSON.parse(data).method;
  29.                         var submsg = JSON.parse(data).msg;
  30.                         var subid = JSON.parse(data).id;
  31.  
  32.                         //console.log(JSON.parse(data));
  33.  
  34.                         // check if there is an UNsub from an old group because of template/ghash change
  35.                         if(submsg === 'unsub') {
  36.                             _.each(usersonlinesubs, function(sub, key, list) {
  37.                                 if(subid === sub.id) {
  38.                                     if(sub.userId) {
  39.                                         console.log('USER changed group from ' + sub.ghash + ' ! User: ' + sub.userId)
  40.                                         UsersOnline.update({ghash: sub.ghash}, { $pull: {users: sub.userId} });
  41.                                     } else {
  42.                                         console.log('ANON User changed group from ' + sub.ghash + ' ! anon -1')
  43.                                         UsersOnline.update({ghash: sub.ghash}, { $inc: {users_anon: -1} });
  44.                                     }
  45.                                     console.log('UsersOnline Coll after GROUP CHANGE:');
  46.                                     console.log(UsersOnline.find().fetch());
  47.                                 }
  48.                             });
  49.                         }
  50.  
  51.                         // If a user is logging in, dec anon. Don't need to add user to set,
  52.                         // because when a user logs in, they are re-subscribed to the collection,
  53.                         // so the publish function will be called again.
  54.                         // Similarly, if they logout, they re-subscribe, and so the anon count
  55.                         // will be handled when the publish function is called again - need only
  56.                         // to take out the user ID from the users array.
  57.                         if(method === 'login') {
  58.                             console.log('logged in! anon: -1');
  59.                             UsersOnline.update({ghash: ghash}, { $inc: {users_anon: -1} });
  60.                         // If a user is logging out, remove from set
  61.                         } else if(method === 'logout') {
  62.                             console.log('logout! userId: ' + userId);
  63.                             UsersOnline.update({ghash: ghash}, { $pull: {users: userId} });
  64.                         }
  65.                     }, function(e) {
  66.                         console.log(e);
  67.                     }));
  68.  
  69.                     this._session.socket.on("close", Meteor.bindEnvironment(function() {
  70.                         console.log('Connection close in group ' + ghash + '!');
  71.                         if(userId === null || userId === undefined) {
  72.                             console.log('Connection closed! anon: -1');
  73.                             UsersOnline.update({ghash: ghash}, { $inc: {users_anon: -1} });
  74.                         } else {
  75.                             console.log('Connection closed by user ' + userId);
  76.                             UsersOnline.update({ghash: ghash}, { $pull: {users: userId} });
  77.                         }
  78.                         console.log('UsersOnline Coll after CLOSE:');
  79.                         console.log(UsersOnline.find().fetch());
  80.                     }, function(e) {
  81.                         console.log("close error", e);
  82.                     }));
  83.                 }
  84.             }
  85.             console.log(UsersOnline.find().fetch());
  86.             console.log('==============================================');
  87.         }
  88.         return UsersOnline.find({});
  89.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement