private void DoWork(object state)
        {
            LastInteraction = DateTime.Now;
            if (ApiSocketServer.Sessions != null && ApiSocketServer.Sessions.Count == 0)
            {
                return;
            }

            foreach (var session in ApiSocketServer.Sessions)
            {
                // if last interaction is more than 3 minutes
                if ((DateTime.Now - session.Value.LastInteraction).TotalMinutes >= 3 ||

                    // if session is created more than 15 minutes
                    (DateTime.Now - session.Value.Created).TotalMinutes >= 15 ||

                    //  if session is created more than minute ago and still do not have open connection
                    ((DateTime.Now - session.Value.Created).TotalMinutes > 1 && session.Value.WebSocket.State != System.Net.WebSockets.WebSocketState.Open))
                {
                    ApiSocketServer.CloseSession(session.Value);
                }
            }

            foreach (var session in DashboardSocketsServer.Sessions)
            {
                //  if session is created more than minute ago and still do not have open connection
                if (((DateTime.Now - session.Value.Created).TotalMinutes > 1 && session.Value.WebSocket.State != System.Net.WebSockets.WebSocketState.Open))
                {
                    DashboardSocketsServer.CloseSession(session.Value);
                }
            }
        }
Exemplo n.º 2
0
        public IActionResult GetDashboardJavascript(string recompile = "0")
        {
            Response.ContentType = "text/javascript";

            if (this.Context.Admin == null || this.Context.Admin.GetStatus() == AdminStatusDM.NotActive)
            {
                return(this.Content("console.error('ccsocket:: auth error');"));
            }

            string js_extension = string.Empty;

            if (string.IsNullOrEmpty(DashboardJS) || recompile.Equals("1"))
            {
                string path    = this.HostingEnvironment.WebRootPath + @"/js/compiled/dashboard.js";
                string direct  = this.HostingEnvironment.WebRootPath + @"/js/compiled/direct.js";
                var    baseUrl = $"{(this.Request.Scheme.Equals("https") ? "wss" : "ws")}://{this.Request.Host.Value.ToString()}{this.Request.PathBase.Value.ToString()}";
                DashboardJS =
                    (new Microsoft.Ajax.Utilities.Minifier().MinifyJavaScript(System.IO.File.ReadAllText(path)))
                    .Replace("[HOST]", baseUrl)
                    .Replace("\"[EVENTS]\"", DashboardSocket.PrintEvents())
                    + ";"
                    + (new Microsoft.Ajax.Utilities.Minifier().MinifyJavaScript(System.IO.File.ReadAllText(direct)));
            }

            // close all previous sessions for this admin!!

            var sessions = (from d in DashboardSocketsServer.Sessions where d.Value.Admin.username.Equals(this.Context.Admin.username) select d.Value.Key).ToList();

            foreach (var session in sessions)
            {
                DashboardSocketsServer.CloseSession(session);
            }

            DashboardSessionSocket socket = new DashboardSessionSocket(this.Context);

            DashboardSocketsServer.AddSession(socket);
            js_extension = DashboardJS.Replace("[SGUID]", socket.Key);

            return(this.ReturnContent(js_extension));
        }