Browse Source

现在在线统计处理

yeshijie 4 years ago
parent
commit
abea3256a6

+ 16 - 1
src/server/endpoints/v2/session.endpoint.js

@ -17,7 +17,6 @@ let Participants = require('../../models/sessions/participants');
let log = require("../../util/log.js");
let clientCache = require('../../models/socket.io/client.cache').clientCache();
const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
const APIv2 = require('../../include/endpoints').APIv2;
@ -194,6 +193,22 @@ router.get("/", function (req, res) {
    }
});
router.get("/getOnlineCountByType", function (req, res) {
    let type = req.query.type;
    let sessions = new Sessions();
    ControllerUtil.regModelEventHandler(sessions, res);
    sessions.getOnlineCountByType(type)
});
router.get("/getOnlineListByType", function (req, res) {
    let type = req.query.type;
    let sessions = new Sessions();
    ControllerUtil.regModelEventHandler(sessions, res);
    sessions.getOnlineListByType(type)
});
/**
 * 按会话类型获取会话列表
 * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4

+ 10 - 1
src/server/handlers/socket.handler.js

@ -7,6 +7,7 @@
let log = require("../util/log.js");
let clientCache = require('../models/socket.io/client.cache').clientCache();
let onlineCache = require('../models/socket.io/onlineCache').onlineCache();
let PatientClient = require('./../models/socket.io/patient.client');
let PcPatientClient = require('./../models/socket.io/pcPatient.client');
let PcDoctorClient  = require('./../models/socket.io/pcDoctor.client');
@ -104,7 +105,9 @@ class SocketHandler {
                            patientClient.password = data.password;
                            patientClient.clientType = data.clientType||"patient";
                            patientClient.sessionId = data.sessionId||"";
                            patientClient.userType = data.userType||"";
                            clientCache.addClient(patientClient);
                            onlineCache.addUser(data.userId,patientClient.userType);
                            users.login(data.userId, 10, '', '');
                            //修改居民在线状态
                            let participants = new Participants();
@ -117,6 +120,7 @@ class SocketHandler {
                            pcpatientClient.password = data.password;
                            pcpatientClient.clientType = data.clientType;
                            pcpatientClient.sessionId = data.sessionId||"";
                            pcpatientClient.userType = data.userType||"";
                            clientCache.addClient(pcpatientClient);
                            users.login(data.userId, 10, '', '');
                            //修改居民在线状态
@ -146,7 +150,9 @@ class SocketHandler {
                            doctorClient.password = data.password;
                            doctorClient.clientType = data.clientType;
                            doctorClient.sessionId =  data.sessionId||"";
                            doctorClient.userType = data.userType||"teacher";
                            clientCache.addClient(doctorClient);
                            onlineCache.addUser(data.userId,doctorClient.userType);
                            //修改医生在线状态
                            let participants = new Participants();
                            //participants.changUserRedisLoginStatus(original_login_userid,data.clientType,1,doctorClient.sessionId);
@ -352,7 +358,9 @@ class SocketHandler {
                        participants.emitSessionUsers(client.sessionId,client.userId,"offline");
                    }
                    log.info('User logout: ' + client.userId);
                    onlineCache.removeUser(client.userId,client.userType);
                    clientCache.removeByUserId(client.userId);
                }
            });
@ -362,7 +370,7 @@ class SocketHandler {
                let patientClient = clientCache.findBySocket(socket);
                log.info("客户端断开patientClient:");
                if (patientClient) {
                    log.info('User ' + patientClient.userId + ' login');
                    log.info('User ' + patientClient.userId + ' * '+patientClient.userType+' disconnect');
                    if(!patientClient.clientType||patientClient.clientType=="patient"||patientClient.clientType=="patient_system"){//新增居民微信端外层-登陆类型-20191012-huangnwenjie
                        let participants = new Participants();
                        participants.changUserRedisLoginStatus(patientClient.userId,patientClient.clientType,0,patientClient.sessionId);
@ -393,6 +401,7 @@ class SocketHandler {
                        participants.emitSessionUsers(patientClient.sessionId,patientClient.userId,"offline");
                    }*/
                    log.info("User disconnect: ", patientClient.userId);
                    onlineCache.removeUser(patientClient.userId,patientClient.userType);
                    clientCache.removeByUserSocket(socket);
                }
            });

+ 67 - 2
src/server/models/sessions/sessions.js

@ -4,6 +4,7 @@
"use strict";
let RedisClient = require('../../repository/redis/redis.client.js');
let onlineCache = require('../socket.io/onlineCache').onlineCache();
let RedisModel = require('./../redis.model.js');
let ModelUtil = require('../../util/model.util');
let Messages = require('../messages/messages');
@ -849,6 +850,70 @@ class Sessions extends RedisModel {
        })
    }
    getOnlineCountByType(type,handler){
        let self = this;
        var res = onlineCache.getCountByType(type);
        if("helper" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,helper:res});
            return;
        }else if("teacher" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,teacher:res});
            return;
        }else if("child" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,child:res});
            return;
        }else if("olderWx" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,olderWx:res});
            return;
        }else if("olderPad" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,olderPad:res});
            return;
        }else{
            //默认返回所有
            var helper = onlineCache.getCountByType("helper");
            var teacher = onlineCache.getCountByType("teacher");
            var child = onlineCache.getCountByType("child");
            var olderWx = onlineCache.getCountByType("olderWx");
            var olderPad = onlineCache.getCountByType("olderPad");
            ModelUtil.emitOK(self.eventEmitter,{status:200,data:{olderPad:olderPad,olderWx:olderWx,child:child,teacher:teacher,helper:helper}});
            return;
        }
    }
    getOnlineListByType(type,handler){
        let self = this;
        var res = onlineCache.getListByType(type);
        if("helper" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,helper:res});
            return;
        }else if("teacher" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,teacher:res});
            return;
        }else if("child" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,child:res});
            return;
        }else if("olderWx" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,olderWx:res});
            return;
        }else if("olderPad" == type){
            ModelUtil.emitOK(self.eventEmitter,{status:200,olderPad:res});
            return;
        }else{
            //默认返回所有
            var helper = onlineCache.getListByType("helper");
            var teacher = onlineCache.getListByType("teacher");
            var child = onlineCache.getListByType("child");
            var olderWx = onlineCache.getListByType("olderWx");
            var olderPad = onlineCache.getListByType("olderPad");
            ModelUtil.emitOK(self.eventEmitter,{status:200,data:{olderPad:olderPad,olderWx:olderWx,child:child,teacher:teacher,helper:helper}});
            return;
        }
    }
    /**
     * 根据用户类型获取用户的session列表
     * @param userId
@ -1675,7 +1740,7 @@ class Sessions extends RedisModel {
                         continue;
                     }
                 }*/
                logger.info("type==="+res[j].type);
                //logger.info("type==="+res[j].type);
                if (res[j].type == SESSION_TYPES.SYSTEM) {
                    if (j == res.length - 1) {
@ -1768,7 +1833,7 @@ class Sessions extends RedisModel {
    getSessionUnreadMessageCount(sessionId, userId, handler) {
        let self = this;
        let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
        logger.info("============"+messagesByTimestampKey)
        //logger.info("============"+messagesByTimestampKey)
        let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
        async.waterfall([
            // 此成员最后获取消息的时间

+ 176 - 0
src/server/models/socket.io/onlineCache.js

@ -0,0 +1,176 @@
/**
 * 客户端缓存池,记录各用户的socket与相应的client.
 * 用来统计用户在线状态
 * helper 助老员,teacher 教师,child 幼儿,olderWx 老人公众号,olderPad
 * author: ysj
 * since: 2021.07.16
 */
"use strict";
let log = require('../../util/log');
var onlineCache = null;
class OnlineCache{
    constructor() {
        this._helper = 0;
        this._teacher = 0;
        this._child = 0;
        this._olderWx = 0;
        this._olderPad = 0;
        this._helperMap = new Map();
        this._teacherMap = new Map();
        this._childMap = new Map();
        this._olderWxMap = new Map();
        this._olderPadMap = new Map();
    }
    static onlineCache() {
        if (onlineCache === null) {
            onlineCache = new OnlineCache();
        }
        return onlineCache;
    }
    getListByType(type){
        if("helper" == type){
            return this.mapToJson(this._helperMap);
        }else if("teacher" == type){
            return this.mapToJson(this._teacherMap);
        }else if("child" == type){
            return this.mapToJson(this._childMap);
        }else if("olderWx" == type){
            return this.mapToJson(this._olderWxMap);
        }else if("olderPad" == type){
            return this.mapToJson(this._olderPadMap);
        }
    }
    mapToJson(params){
        let jsonArray = {};
        for(let key of params.keys()){
            let value=params.get(key);
            jsonArray[key]=value;
        }
        let json = JSON.stringify(jsonArray);
        log.info("json",json);
        return json;
    }
    getCountByType(type){
        if("helper" == type){
            return this._helper;
        }else if("teacher" == type){
            return this._teacher;
        }else if("child" == type){
            return this._child;
        }else if("olderWx" == type){
            return this._olderWx;
        }else if("olderPad" == type){
            return this._olderPad;
        }
        return 0;
    }
    //新增用户,判断用户是否在线,在线人数统计
    addUser(userId,type) {
        var key = userId+":"+type;
        var count = this.findByIdAndType(userId,type);
        if(count&&count>0){
            log.info(count + '重复加入 ', key);
            this.setUser(userId,type,count+1);
        }else{
            log.info('第一次加入 ', key);
            this.setUser(userId,type,1);
            if("helper" == type){
                this._helper = this._helper+1;
            }
            if("teacher" == type){
                this._teacher = this._teacher+1;
            }
            if("child" == type){
                this._child = this._child+1;
            }
            if("olderWx" == type){
                this._olderWx = this._olderWx+1;
            }
            if("olderPad" == type){
                this._olderPad = this._olderPad+1;
            }
        }
    }
    setUser(userId,type,count){
        if(count<0){
            return;
        }
        if("helper" == type){
            this._helperMap.set(userId,count);
        }
        if("teacher" == type){
            this._teacherMap.set(userId,count);
        }
        if("child" == type){
            this._childMap.set(userId,count);
        }
        if("olderWx" == type){
            this._olderWxMap.set(userId,count);
        }
        if("olderPad" == type){
            this._olderPadMap.set(userId,count);
        }
    }
    findByIdAndType(userId,type) {
        if("helper" == type){
            return this._helperMap.get(userId);
        }
        if("teacher" == type){
            return this._teacherMap.get(userId);
        }
        if("child" == type){
            return this._childMap.get(userId);
        }
        if("olderWx" == type){
            return this._olderWxMap.get(userId);
        }
        if("olderPad" == type){
            return this._olderPadMap.get(userId);
        }
        return null;
    }
    //用户断线,判断用户是否在线,在线人数统计
    removeUser(userId,type){
        if(type == "" || type == undefined || type == null){
            log.info(type+"--离线--");
            return;
        }
        var key = userId+":"+type;
        var count = this.findByIdAndType(userId,type);
        if(count&&count>0){
            log.info(count + '离线 ', key);
            this.setUser(userId,type,count-1);
            if("helper" == type){
                this._helper = this._helper-1;
            }
            if("teacher" == type){
                this._teacher = this._teacher-1;
            }
            if("child" == type){
                this._child = this._child-1;
            }
            if("olderWx" == type){
                this._olderWx = this._olderWx-1;
            }
            if("olderPad" == type){
                this._olderPad = this._olderPad-1;
            }
        }
    }
}
module.exports = OnlineCache;