sessionRemote.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 userstate id.
  40. *
  41. * @param {String} uid userstate 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 {Function} cb callback function
  61. */
  62. Remote.prototype.kickBySid = function(sid, cb) {
  63. this.app.get('sessionService').kickBySessionId(sid, cb);
  64. };
  65. /**
  66. * Kick sessions by userstate id.
  67. *
  68. * @param {Number|String} uid userstate id
  69. * @param {Function} cb callback function
  70. */
  71. Remote.prototype.kickByUid = function(uid, cb) {
  72. this.app.get('sessionService').kick(uid, cb);
  73. };