问题
I have a website and i want to find number of online users in my website(All of users logged to their account or not): And when they get offline or close page the number of online users get update: How can i do that?is there any library to do that?
回答1:
You can add an is_online
BooleanField to your User model and create your own custom authenticate
backends for your User model that after authentication sets the is_online
to true.
When a user goes to close the web page you can have a event listener to make an Ajax request where you will then set that users is_online
column to false in DB and logs the user out.
If you don't want to remove the user from sessions you might need additional logic in other places to turn that attribute to true when they come back to the website.
// OR
window.addEventListener("beforeunload", function(e){
// Do something
}, false);
You can then find your online users by either doing a model.objects.filter(is_online=True)
or you can create a custom method on the Model itself that can contain other business logic and or potentially send a email or something.
来源:https://stackoverflow.com/questions/44978609/number-of-online-people-in-django