Question

I have a web application which serves as the Admin Panel. It has a live chat option which connects with writers(employees) on the other end. The web application uses the following PHP code to check for online writers.

$q = 0;
$lasttime = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
while ($q<5){
    sleep(3);
    $wresult = $db->query("SELECT writer_alias FROM tblwriter WHERE writer_isactive=1 AND (UNIX_TIMESTAMP(NOW())-last_activity)<10");
    if ($wresult->num_rows){ break; }
    ++$q;
}
if ($wresult->num_rows){
    while ($row = $wresult->fetch_object()){ $writers[] = $row; } 
    $wresult->free(); 
}
echo json_encode(
    array(
        "writers" => $writers,
        "now" => time()
    )
);

On the application the following javascript code handles the PHP response and calls ajax again to complete the loop.

function UpdateCHAT(){
    $.ajax({ 
        type: "POST", 
        url: "liveserver.php", 
        data: {update:"1",timestamp:lastime}, 
        success: 
            function(data1){
                if (data1 == null){ 
                    $(".onlinechat i").removeClass("icon-white"); 
                }else{ 
                    lastime = user_signin = Number(data1.now);
                    if (!data1.writers.length){ 
                        $(".onlinechat i").removeClass("icon-white"); 
                    }else{
                        $(".onlinechat i").removeClass("icon-white");
                        $.each(data1.writers,function(j) {
                            $("#writer_"+data1.writers[j].writer_alias).find("i").addClass("icon-white");
                        }); 
                    }
                }
            }, 
        dataType: "json",
        timeout: 60000,         
        complete: 
            function(){
                UpdateCHAT(); 
            }
        }
    );
}

Everything is working just fine except the fact that I cannot think of a way to know for offline writers since the PHP code is designed to check for the online writers, but this means if a writer is online once, he will remain online (on the application) until the PHP code dies and return empty writers.

Hope I am able to explain my point. This question is more to do with idea rather than piece of code. Any input is appreciated.

Thanks.

Était-ce utile?

La solution

Make the PHP code always return after some time (like 2 minutes) with an empty result set (ie. no new chat lines).

JS will then do a new request immediately. If it doesn't, well, then the user is offline. Keep a last_request timestamp, if it's older than 2+e minutes the user is offline.


You could try to detect when the connection closes in PHP. Set ignore_user_abort(true) so you are in control of when your script dies. Then use connection_aborted() to check if the client closed. If he did you know he left.

A potential problem occurs if the user has two windows open: a close on one doesn't mean he left, but this may turn out to be acceptable; the user will blip for just a while.


Another solution is to use a separate ping request that just tells you "yep, I'm still here". If you haven't gotten one of those in a while the user is probably offline.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top