client.cache.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * 客户端缓存池,记录各用户的socket与相应的client.
  3. *
  4. * author: Sand Wen
  5. * since: 2016.11.19
  6. */
  7. "use strict";
  8. var log = require('../../util/log');
  9. var clientCache = null;
  10. class ClientCache {
  11. constructor() {
  12. this._clientsBySocket = new Map();
  13. this._clientsByUserId = new Map();
  14. this._clientsByTypeAndUserId = new Map();
  15. this._clientsCloudCarePCManage = new Map();
  16. }
  17. get clients() {
  18. return this._clientsByUserId;
  19. }
  20. addClient(client) {
  21. this._clientsByUserId.set(client.userId, client);
  22. this._clientsBySocket.set(client.socket, client);
  23. this._clientsByTypeAndUserId.set(client.userId+":"+client.clientType,client);
  24. if ("cloudCare_pcManage"===client.clientType){
  25. this._clientsCloudCarePCManage.set(client.userId+":"+client.clientType,client);
  26. }
  27. //log.info('Current clients: ', this.clients);
  28. }
  29. removeByUserId(userId) {
  30. var client = this.findById(userId);
  31. this._clientsByUserId.delete(userId);
  32. if(client){
  33. this._clientsBySocket.delete(client.socket);
  34. this._clientsByTypeAndUserId.delete(client.userId+":"+client.clientType);
  35. }
  36. }
  37. removeByUserSocket(socket) {
  38. var client = this.findBySocket(socket);
  39. if (client) {
  40. this._clientsByUserId.delete(client.userId);
  41. this._clientsBySocket.delete(socket);
  42. this._clientsByTypeAndUserId.delete(client.userId+":"+client.clientType)
  43. }
  44. }
  45. findById(userId) {
  46. return this._clientsByUserId.get(userId);
  47. }
  48. findByIdAndType(userId,type){
  49. return this._clientsByTypeAndUserId.get(userId+":"+type);
  50. }
  51. findByType(type){
  52. if ("cloudCare_pcManage"===type){
  53. return this._clientsCloudCarePCManage;
  54. }
  55. }
  56. findBySocket(socket) {
  57. return this._clientsBySocket.get(socket);
  58. }
  59. static clientCache() {
  60. if (clientCache === null) {
  61. clientCache = new ClientCache();
  62. }
  63. return clientCache;
  64. }
  65. }
  66. module.exports = ClientCache;