How to know when user closes browser? Chat application

前端 未结 6 1894
离开以前
离开以前 2020-12-20 02:15

I have a simple chat client set up that allows users to login with a username and stores the messages they write in an sql database. Every 3 seconds, the database simply pri

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 03:02

    put online users in a table that will have a field named like 'lastSeen' update this field every few seconds with ajax call..

    ajax call be made someting like this :

    window.setInterval(function() {
        $.ajax({      
          url: _URL_ENGINE + "/updateLastSeen/?userid=" + userID,
          success: function(data) {
    
          }
        }); 
    }, 2000); // 2000 means 2 seconds
    

    now to query the list of online players u can query them like

    select * from players WHERE lastSeen > DATE_SUB(NOW(), interval 40 SECOND) 
    

    hope this helps

提交回复
热议问题