123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using System.Collections.Concurrent;
- using WingInterfaceLibrary.DTO.Consultation;
- using WingInterfaceLibrary.DTO.User;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.LiveConsultation;
- using WingLiveConsultationService.Utilities;
- using WingServerCommon.Interfaces.Cache;
- using WingInterfaceLibrary.Interface.DBInterface;
- using WingInterfaceLibrary.DB.Request;
- using WingInterfaceLibrary.Request.DBRequest;
- using WingInterfaceLibrary.DTO.LiveRoom;
- using WingServerCommon.Log;
- using WingServerCommon.Config;
- using WingServerCommon.Config.Parameters;
- namespace WingLiveConsultationService
- {
- internal class LiveConsultationHeartRateManager
- {
- private int _rateSeconds => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "HeartRateSeconds").Value;//秒
- private int _networkErrTimes => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "NetworkErrTimes").Value;//3次间隔无心跳,网络不佳
- private int _leaveRoomTimes => EnvironmentConfigManager.GetParammeter<IntParameter>("LiveConsultation", "LeaveRoomTimes").Value;//10次间隔无心跳,离开房间
- private ConcurrentDictionary<string, LiveConsultationHeartRateDTO> _clientTokens = new ConcurrentDictionary<string, LiveConsultationHeartRateDTO>();
- private Action<string, string, string, bool> OnClientJoined;
- private Action<string, string, string> OnClientNetworkErr;
- private Action<string, string, string> OnClientLeave;
- public LiveConsultationHeartRateManager(Action<string, string, string, bool> handleClientJoined, Action<string, string, string> handleClientNetworkErr, Action<string, string, string> handleClientLeave)
- {
- OnClientJoined = handleClientJoined;
- OnClientNetworkErr = handleClientNetworkErr;
- OnClientLeave = handleClientLeave;
- StartCheckClients();
- }
- /// <summary>
- /// 加入检测集合
- /// </summary>
- /// <param name="token"></param>
- /// <param name="clientId"></param>
- /// <param name="name"></param>
- /// <param name="consultationCode"></param>
- public void AddOrUpdate(string token, string clientId, string name, string consultationCode)
- {
- var uniqueId = GetUniqueId(token, consultationCode);
- _clientTokens.AddOrUpdate(uniqueId, (k) =>
- {
- var v = new LiveConsultationHeartRateDTO(token, clientId, name, consultationCode);
- OnClientJoined.Invoke(consultationCode, v.ClientId, name, false);
- return v;
- }, (k, v) =>
- {
- var sendMessage = v.LivingTimes >= _networkErrTimes;
- v.Activate();
- OnClientJoined.Invoke(consultationCode, v.ClientId, name, sendMessage);
- return v;
- });
- }
- private string GetUniqueId(string token, string consultationCode)
- {
- var uniqueId = $"{consultationCode}_{token}";
- return uniqueId;
- }
- /// <summary>
- /// 会诊心跳有效期验证
- /// </summary>
- private void StartCheckClients()
- {
- Task.Run(async () =>
- {
- Logger.WriteLineInfo($"LiveConsultationHeartRateManager start check clients");
- try
- {
- while (true)
- {
- await Task.Delay(1 * 1000 * _rateSeconds);
- foreach (var tokenInfo in _clientTokens.Values)
- {
- tokenInfo.DeActivate();
- var uniqueId = GetUniqueId(tokenInfo.Token, tokenInfo.ConsultationCode);
- try
- {
- if (tokenInfo.LivingTimes == _networkErrTimes)
- {
- OnClientNetworkErr.Invoke(tokenInfo.ConsultationCode, tokenInfo.ClientId, tokenInfo.Name);
- }
- else if (tokenInfo.LivingTimes == _leaveRoomTimes)
- {
- if (!_clientTokens.Values.Any(x => x.ClientId == tokenInfo.ClientId && x.ConsultationCode == tokenInfo.ConsultationCode && x.LivingTimes < _leaveRoomTimes))
- {
- OnClientLeave.Invoke(tokenInfo.ConsultationCode, tokenInfo.ClientId, tokenInfo.Name);
- }
- _clientTokens.TryRemove(uniqueId, out _);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"check user[{tokenInfo.Name}] token {tokenInfo.Token} err, {ex}");
- }
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"LiveConsultationHeartRateManager err, ex:{ex}");
- }
- Logger.WriteLineInfo($"LiveConsultationHeartRateManager finished");
- });
- }
- }
- internal class LiveConsultationHeartRateDTO
- {
- /// <summary>
- /// 登录Token
- /// </summary>
- /// <value></value>
- public string Token { get; set; }
- /// <summary>
- /// 用户or设备编码
- /// </summary>
- /// <value></value>
- public string ClientId { get; set; }
- /// <summary>
- /// 用户or设备名称
- /// </summary>
- /// <value></value>
- public string Name { get; set; }
- /// <summary>
- /// 会诊编码
- /// </summary>
- /// <value></value>
- public string ConsultationCode { get; set; }
- /// <summary>
- /// 已检查次数,下一个心跳会重置
- /// </summary>
- /// <value></value>
- public int LivingTimes { get; private set; }
- public LiveConsultationHeartRateDTO(string token, string clientId, string name, string consultationCode)
- {
- Token = token;
- ClientId = clientId;
- Name = name;
- ConsultationCode = consultationCode;
- }
- /// <summary>
- /// Reset the living time.
- /// </summary>
- public virtual void Activate()
- {
- LivingTimes = 0;
- }
- /// <summary>
- /// Deactive the session.
- /// </summary>
- public virtual void DeActivate()
- {
- LivingTimes++;
- }
- }
- }
|