event.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var statusService = require('../service/statusService');
  2. var logger = require('pomelo-logger').getLogger(__filename);
  3. var Event = function(app) {
  4. this.app = app;
  5. this.statusService = app.get('statusService');
  6. };
  7. module.exports = Event;
  8. Event.prototype.bind_session = function(session) {
  9. if(!session.uid) {
  10. return;
  11. }
  12. this.statusService.add(session.uid, session.frontendId, function(err) {
  13. if(!!err) {
  14. logger.error('statusService add user failed: [%s] [%s], err: %j', session.uid, session.frontendId, err);
  15. return;
  16. }
  17. });
  18. };
  19. Event.prototype.close_session = function(session) {
  20. if(!session.uid) {
  21. return;
  22. }
  23. // don't remove entry if another session for the same user on the same frontend remain
  24. var currentUserSessions = this.app.get('sessionService').getByUid(session.uid);
  25. if (currentUserSessions !== undefined) {
  26. logger.debug('at least another session exists for this user on this frontend: [%s] [%s]', session.uid, session.frontendId);
  27. return;
  28. }
  29. this.statusService.leave(session.uid, session.frontendId, function(err) {
  30. if(!!err) {
  31. logger.error('failed to kick user in statusService: [%s] [%s], err: %j', session.uid, session.frontendId, err);
  32. return;
  33. }
  34. });
  35. };