php - Detect Live Server Side changes (Long Polling, Short Polling or Web Sockets) -


justification , research

i have site requires users login in order view. whilst users logged in, keep eye on user session. this, mean know whether or not user session has expired, , therefore redirect them.

each user's session lasts 1 hour (or whatever set to), , reset if visit different page (like login systems).

at present, have following algorithm:

  1. user arrives @ private page (javascript method called isuserauthorized() executed)
  2. the isuserauthorized() javascript method makes ajax request page 'ajax.example.net/authorized'
  3. this page returns json object indicating current status of user so:

{ authorized: true, timeout: 3600000 }

  1. the javascript method sets timeout call method again in timeout milliseconds, assuming session have ended then.
  2. if session has ended redirect user, otherwise recall in method in timeout milliseconds.

there 2 reasons not current method:

  1. i have had issues time syncing between client , server clocks, weird causes issue...
  2. it leaves timeout in background of webpage, , site javascript heavy, rather not have additional timeout in order keep site smooth possible.

my question

my question therefore, can think of better way achieve this? have thought of long polling or websockets, not 100% sure how use either of these , tutorials on websockets found not good! these better solution?

i workaround time syncing issues before do, want ensure there not better ways achieve this...

in case helps, here current code:

// set authorized timeout mn.authorizedtimeout = settimeout(function(){mn.isuserauthorized});  /**  * user authorized  * checks see if current user authorized ,  * makes sure session still active  */ mn.isuserauthorized = isuserauthorized; function isuserauthorized(){     // temporary     console.log('authorising');     // set authorized var     var authorized = false;     // clear current timeout     cleartimeout(mn.authorizedtimeout);     // send request determine whether user authorized     $.ajax({         url: "//ajax.example.net/authorized",         type: "get",         datatype: "json",         cache: false,         async: false,         success: function(data){             console.log(data);             if(data.authorized){                 // if user authorized check again in timeout milliseconds                 mn.authorizedtimeout = settimeout(mn.isuserauthorized,data.timeout_milliseconds);                 // set authorized true                 authorized = true;             }else{                 // if session has expired proceed informing user                 mn.usersessionexpired();                 // set authorized false                 authorized = false;             }         }     });     // return session status boolean     return authorized; } 

updated answer:

nevertheless i'd consider better practice calculate online status serverside. can make sure there's no inconsistency time. have servertime.

for getting online status can go long polling approach. i've made example:

(function checkloginstatus(){     $.ajax({        type: 'post',       url: 'loginstatus.php',       data: {userid : 25},       success: function(data){         if(data.logged_in !== true){           //log user out         }       },        datatype: "json",        complete: checkloginstatus,        timeout: 15000      }); })(); 

this make sure new request made when 15 seconds passed , when request complete.

old answer:

if concern watch on logged in users don't need poll. i'd keep whole thing serverside. add "last_active" field users table.

whenever user interacts (visits site) update timestamp current timestamp.

to detect whether user online take current timestamp , subtract "last_active" timestamp it. if difference bigger 1 hour, know user inactive.

that's how handle it. more efficient (regarding ressources) doing ajax.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -