/// <summary> /// 构造函数 /// </summary> public QRCodeModel(QRCodeTypes qRCodeType, string targetUrl) { UUID = Guid.NewGuid(); CreateDate = DateTime.Now; IsLogin = false; TargetUrl = targetUrl; //UserName = Password = ""; QRCodeType = qRCodeType; }
/// <summary> /// 启动Timer /// </summary> /// <param name="connectionId"></param> public static void StartTimer(string connectionId, QRCodeTypes qrCodeType, string targetUrl) { var newTimer = new SessionTimer(connectionId, qrCodeType, targetUrl); if (!Timers.TryAdd(connectionId, newTimer)) { newTimer.Dispose(); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="connectionId"></param> private SessionTimer(string connectionId, QRCodeTypes qrCodeType, string targetUrl) { ConnectionId = connectionId; _timer = new Timer { Interval = Utility.ActivityTimerInterval() }; _timer.Elapsed += (s, e) => MonitorElapsedTime(qrCodeType, targetUrl); _timer.Start(); }
/// <summary> /// 获取当前UUID /// </summary> /// <returns></returns> public static string GetUUID(string connectionId, QRCodeTypes qrCodeType, string targetUrl) { try { var model = new QRCodeAction().GetValidModel(connectionId, qrCodeType, targetUrl); return(model.ToJson(connectionId, qrCodeType, targetUrl)); } catch { return("ERROR"); } }
/// <summary> /// Called when the connection reconnects to this hub instance. /// </summary> /// <returns>A <see cref="T:System.Threading.Tasks.Task" /></returns> public override Task OnReconnected() { if (!SessionTimer.Timers.ContainsKey(Context.ConnectionId)) { string qrCodeTypeStr = Context.QueryString["qrCodeType"].ToString().ToLower(); string targetUrl = Context.QueryString["TargetUrl"].ToString().ToLower(); QRCodeTypes qrCodeType = QRCodeTypes.Other; switch (qrCodeTypeStr) { case "logincode": qrCodeType = QRCodeTypes.Login; break; } SessionTimer.StartTimer(Context.ConnectionId, qrCodeType, targetUrl); } return(base.OnReconnected()); }
/// <summary> /// Called when the connection connects to this hub instance. /// </summary> /// <returns>A <see cref="T:System.Threading.Tasks.Task" /></returns> public override Task OnConnected() { string qrCodeTypeStr = Context.QueryString["qrCodeType"].ToString().ToLower(); string targetUrl = Context.QueryString["TargetUrl"].ToString().ToLower(); QRCodeTypes qrCodeType = QRCodeTypes.Other; switch (qrCodeTypeStr) { case "logincode": qrCodeType = QRCodeTypes.Login; break; } SessionTimer.StartTimer(Context.ConnectionId, qrCodeType, targetUrl); //string uuid = Context.QueryString["uuid"].ToString(); //SendQRCodeUUID(uuid); return(base.OnConnected()); }
/// <summary> /// 获取有效的QRCodeModel /// </summary> /// <param name="connectionId"></param> /// <returns></returns> public QRCodeModel GetValidModel(string connectionId, QRCodeTypes qrCodeType, string targetUrl) { lock (LockObject) { QRCodeModel model; if (QRCodeLists.ContainsKey(connectionId)) { model = QRCodeLists[connectionId]; if (model.IsValid()) //有效 //if (model.IsValid) //有效 { return(model); } QRCodeLists.Remove(connectionId); } model = new QRCodeModel(qrCodeType, targetUrl); QRCodeLists.Add(connectionId, model); return(model); } }
/// <summary> /// 重置时钟 /// </summary> public void ResetTimer() { SessionTimer timer; if (SessionTimer.Timers.TryGetValue(Context.ConnectionId, out timer)) { timer.ResetTimer(); } else { string qrCodeTypeStr = Context.QueryString["qrCodeType"].ToString().ToLower(); string targetUrl = Context.QueryString["TargetUrl"].ToString().ToLower(); QRCodeTypes qrCodeType = QRCodeTypes.Other; switch (qrCodeTypeStr) { case "logincode": qrCodeType = QRCodeTypes.Login; break; } SessionTimer.StartTimer(Context.ConnectionId, qrCodeType, targetUrl); } }
/// <summary> /// 给客户端发送消息 /// </summary> private void MonitorElapsedTime(QRCodeTypes qrCodeType, string targetUrl) { Utility.ClearExpiredUUID(); var uuid = Utility.GetUUID(ConnectionId, qrCodeType, targetUrl); //if (TimeCount >= Utility.TimerValue()) //{ // StopTimer(ConnectionId); // Notifier.SendQRCodeUUID(ConnectionId, uuid); // Notifier.SessionTimeOut(ConnectionId, TimeCount); //} //else //{ Notifier.SendQRCodeUUID(ConnectionId, uuid); Notifier.SendElapsedTime(ConnectionId, TimeCount); //} TimeCount++; if (TimeCount > 1000) { TimeCount = 0; } }
//public bool _isValid = isGQ(); //public static bool isGQ() //{ // TimeSpan ts = DateTime.Now - CreateDate; // return ts.TotalSeconds <= 180; //180秒(3分钟)之内有效 //} ///// <summary> ///// 判断是否有效,有效时间为190秒 ///// </summary> ///// <returns></returns> //public bool IsValid //{ // get // { // TimeSpan ts = DateTime.Now - CreateDate; // return ts.TotalSeconds <= 180; //180秒(3分钟)之内有效 // } // set { // _isValid = value; // } //} /// <summary> /// 转换为json字符串 /// </summary> /// <param name="connectionId"></param> /// <returns></returns> public string ToJson(string connectionId, QRCodeTypes qrCodeType, string targetUrl) { StringBuilder result = new StringBuilder(); result.Append("{"); result.AppendFormat("\"connectionid\":\"{0}\",", connectionId); result.AppendFormat("\"uuid\":\"{0}\",", UUID); result.AppendFormat("\"targetUrl\":\"{0}\",", targetUrl); //result.AppendFormat("\"userName\":\"{0}\",", UserName); //result.AppendFormat("\"password\":\"{0}\",", Password); result.AppendFormat("\"qrCodeType\":\"{0}\",", qrCodeType); result.AppendFormat("\"islogin\":{0},", IsLogin.ToString().ToLower().Equals("false") ? 0 : 1); result.AppendFormat("\"isvalid\":{0}", IsValid().ToString().ToLower().Equals("false") ? 0 : 1); //result.AppendFormat("\"createdate\":\"{0}\"", CreateDate.ToString(CultureInfo.InvariantCulture).ToLower()); //result.AppendFormat("\"nowdate\":\"{0}\"", DateTime.Now.ToString(CultureInfo.InvariantCulture).ToLower()); result.Append("}"); return(result.ToString()); //var isLogin = IsLogin.ToString().ToLower().Equals("false") ? false : true; //var isvalid = IsValid().ToString().ToLower().Equals("false") ? false : true; //var obj = new JsonRes() //{ // Connectionid = connectionId, // QRCodeType = qrCodeType, // UUID = UUID, // IsLogin = isLogin, // IsValid = isvalid, // UserName = UserName, // Password = Password //}; //var jsonStr = JsonHelper.JsonSerialize.ObjectToJsonByDCJS(obj); //return jsonStr; }