12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * 客户端缓存池,记录各用户的socket与相应的client.
- *
- * author: Sand Wen
- * since: 2016.11.19
- */
- "use strict";
- var log = require('../../util/log');
- var clientCache = null;
- class ClientCache {
- constructor() {
- this._clientsBySocket = new Map();
- this._clientsByUserId = new Map();
- this._clientsByTypeAndUserId = new Map();
- }
- get clients() {
- return this._clientsByUserId;
- }
- addClient(client) {
- this._clientsByUserId.set(client.userId, client);
- this._clientsBySocket.set(client.socket, client);
- this._clientsByTypeAndUserId.set(client.userId+":"+client.clientType,client);
- //log.info('Current clients: ', this.clients);
- }
- removeByUserId(userId) {
- var client = this.findById(userId);
- this._clientsByUserId.delete(userId);
- if(client){
- this._clientsBySocket.delete(client.socket);
- this._clientsByTypeAndUserId.delete(client.userId+":"+client.clientType);
- }
- }
- removeByUserSocket(socket) {
- var client = this.findBySocket(socket);
- if (client) {
- this._clientsByUserId.delete(client.userId);
- this._clientsBySocket.delete(socket);
- this._clientsByTypeAndUserId.delete(client.userId+":"+client.clientType)
- }
- }
- findById(userId) {
- return this._clientsByUserId.get(userId);
- }
- findByIdAndType(userId,type){
- return this._clientsByTypeAndUserId.get(userId+":"+type);
- }
- findBySocket(socket) {
- return this._clientsBySocket.get(socket);
- }
- static clientCache() {
- if (clientCache === null) {
- clientCache = new ClientCache();
- }
- return clientCache;
- }
- }
- module.exports = ClientCache;
|