We use Redis to store the user's information, when the user logs in, we will go to Redis to write this user information.
At the same time, every time the user requests, we go to update the online user's information, update the user's last online date, and delete the expired user's information.
In this way, not real-time, in order to achieve real-time,
We added a JS timer on the page. Every 1s (this time can be set as appropriate)
Sends a request to the server side. Delete the expired user and pull the current online user information.
During this pull, there is a problem when we send the request with a cookie, so that the session status is updated on the server side.
That is, the session will never expire, so that we designed the 30-minute user expired, re-jump to the landing page, the function is affected.
Then we need to solve, in this pull process, update session.
Analyze the code, see the API that this request is walking, and enable the session in the API.
So long as, this interface call does not go this session, is OK.
Modified Code by Analysis:
Public Override void Init () {this. Postauthenticaterequest + = (sender, e) + ={if (HttpContext.Current.Request.RawUrl.Contains ( "/api/sysmanage/useronline/postlistuserbydepartment")) {return ;} HttpContext.Current.SetSessionStateBehavior (System.Web.SessionState.SessionStateBehavior.Required);}; Base . Init ();}
Note: This place session saves the place and the online user information to save is separate, does the online user clears the comparison time must be consistent with the session expiration time.
This makes online statistics more accurate.
Encountered pits: implementation of online user statistics