sessionRemote.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Remote session service for frontend server.
  3. * Set session info for backend servers.
  4. */
  5. var utils = require('../../../util/utils');
  6. module.exports = function(app) {
  7. return new Remote(app);
  8. };
  9. var Remote = function(app) {
  10. this.app = app;
  11. };
  12. Remote.prototype.bind = function(sid, uid, cb) {
  13. this.app.get('sessionService').bind(sid, uid, cb);
  14. };
  15. Remote.prototype.unbind = function(sid, uid, cb) {
  16. this.app.get('sessionService').unbind(sid, uid, cb);
  17. };
  18. Remote.prototype.push = function(sid, key, value, cb) {
  19. this.app.get('sessionService').import(sid, key, value, cb);
  20. };
  21. Remote.prototype.pushAll = function(sid, settings, cb) {
  22. this.app.get('sessionService').importAll(sid, settings, cb);
  23. };
  24. /**
  25. * Get session informations with session id.
  26. *
  27. * @param {String} sid session id binded with the session
  28. * @param {Function} cb(err, sinfo) callback funtion, sinfo would be null if the session not exist.
  29. */
  30. Remote.prototype.getBackendSessionBySid = function(sid, cb) {
  31. var session = this.app.get('sessionService').get(sid);
  32. if(!session) {
  33. utils.invokeCallback(cb);
  34. return;
  35. }
  36. utils.invokeCallback(cb, null, session.toFrontendSession().export());
  37. };
  38. /**
  39. * Get all the session informations with the specified user id.
  40. *
  41. * @param {String} uid user id binded with the session
  42. * @param {Function} cb(err, sinfo) callback funtion, sinfo would be null if the session does not exist.
  43. */
  44. Remote.prototype.getBackendSessionsByUid = function(uid, cb) {
  45. var sessions = this.app.get('sessionService').getByUid(uid);
  46. if(!sessions) {
  47. utils.invokeCallback(cb);
  48. return;
  49. }
  50. var res = [];
  51. for(var i=0, l=sessions.length; i<l; i++) {
  52. res.push(sessions[i].toFrontendSession().export());
  53. }
  54. utils.invokeCallback(cb, null, res);
  55. };
  56. /**
  57. * Kick a session by session id.
  58. *
  59. * @param {Number} sid session id
  60. * @param {String} reason kick reason
  61. * @param {Function} cb callback function
  62. */
  63. Remote.prototype.kickBySid = function(sid, reason, cb) {
  64. this.app.get('sessionService').kickBySessionId(sid, reason, cb);
  65. };
  66. /**
  67. * Kick sessions by user id.
  68. *
  69. * @param {Number|String} uid user id
  70. * @param {String} reason kick reason
  71. * @param {Function} cb callback function
  72. */
  73. Remote.prototype.kickByUid = function(uid, reason, cb) {
  74. this.app.get('sessionService').kick(uid, reason, cb);
  75. };