12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- /**
- * 客户端缓存池,记录各用户的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();
- }
- get clients(){
- return this._clientsByUserId;
- }
- addClient(client){
- this._clientsByUserId.set(client.userId, client);
- this._clientsBySocket.set(client.socket, client);
- //log.info('Current clients: ', this.clients);
- }
- removeByUserId(userId){
- var socket = this.findById(userId);
- this._clientsByUserId.delete(userId);
- this._clientsBySocket.delete(socket);
- }
- removeByUserSocket(socket){
- var userId = this.findBySocket(socket);
- this._clientsByUserId.delete(userId);
- this._clientsBySocket.delete(socket);
- }
- findById(userId){
- return this._clientsByUserId.get(userId);
- }
- findBySocket(socket){
- return this._clientsBySocket.get(socket);
- }
- static clientCache(){
- if(clientCache === null){
- clientCache = new ClientCache();
- }
- return clientCache;
- }
- }
- module.exports = ClientCache;
|