最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP reload chat-box? - Stack Overflow

programmeradmin2浏览0评论

I have made a chat box like thing in PHP. The only problem is that it doesn't automatically reload. Is there a way to make it reload in PHP, or would I have to move everything around so I can use AJAX? Also, one of my users is constantly online. I have set it to be that they are offline if they have pressed a key or clicked in the last 3 minutes and to set them offline when they leave the page. They are using Firefox 9.0. Is this because of the use of onunload? What would be a solution?

I have made a chat box like thing in PHP. The only problem is that it doesn't automatically reload. Is there a way to make it reload in PHP, or would I have to move everything around so I can use AJAX? Also, one of my users is constantly online. I have set it to be that they are offline if they have pressed a key or clicked in the last 3 minutes and to set them offline when they leave the page. They are using Firefox 9.0. Is this because of the use of onunload? What would be a solution?

Share Improve this question asked Feb 20, 2012 at 15:24 CoffeeRainCoffeeRain 4,5224 gold badges33 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You could use a meta refresh tag, that could be generated by PHP, but its not really a PHP-specific feature.

<meta http-equiv="refresh" content="600">

This will refresh the content after 600 seconds. You could throttle that time more accurately based on your applications demands.

Ajax is probably the preferred method for checking user inactivity. Here is a similar post on checking user activity - How can I detect with JavaScript/jQuery if the user is currently active on the page?

UPDATE

As for the second question of how to handle exit of a user in Firefox, its like you said that onunload can be inconsistent. This is where the use of ajax shines again. If your app stops receiving ajax updates from the client, you can do some sort of cleanup to mark them as inactive.

We had a similar situation in an ASP.Net MVC app. We ended up using Application Variables to store recent activity and state of users. A php example can be found here.

Hope this helps!

You could add some JavaScript to automatically refresh the entire page, but using AJAX would be far better for the end user.

the way Facebook does it, is that client "Alice" has a pending request for new messages open for about 1 minute. if another client "Bob" writes Alice a message, the server can plete Alice's pending request. Alice then instantly gets the new message and opens a new pending request.

you will have to timeout pending requests after about a minute or the browser does it. different browsers have different timeout settings, so just use something small. after a timeout you open a new pending request to not miss anything.

i got some code to play around with: listen.php (client calls this to create pending request, waiting for message nr $mnr of the event $eid.

define('WAIT_MAX', 55); // wait max 55 sec
define('WAIT_INT', 1);  // wait 1 sec per call

$start = time();
while ($start + WAIT_MAX > time())
{
    // check if an event occured
    $res = mysql_query('SELECT * FROM event WHERE `eid`="'.$eid.'" AND `mnr`="'.$mnr.'"');

    if (mysql_num_rows($res) > 0)
    {
        // event occured
        $row = mysql_fetch_assoc($res);
        $msg = $row['msg'];

        echo "<event eid=\"$eid\" mnr=\"$mnr\" msg=\"$msg\" />\n";
        die();
        //die('event occured: '.$msg."<br>\nNext mnr=".($mnr+1));
        //break;
    }
    else
    {
        // no event occured
        mysql_free_result($res);
        sleep(WAIT_INT);
    }
}
die(WAIT_MAX . ' seconds passed and no event occured.');

Bob sends its message to yell.php providing $msg

if (isset($_GET['msg']))
    $msg = $_GET['msg'];
else
    $msg = 'no message given, just firing the event.';

mysql_query('INSERT INTO `event` (`eid`, `mnr`, `msg`) VALUES ("'.$eid.'", "'.$mnr.'", "'.$msg.'")');

some init.php to make it work:

$eid = -1;
$mnr = -1;

if (isset($_GET['eid']) && isset($_GET['mnr']))
{
    $eid = max(0, (int)$_GET['eid']);
    $mnr = max(0, (int)$_GET['mnr']);
}
elseif (isset($_GET['eid']))
{
    $eid = max(0, (int)$_GET['eid']);
    $mnr = 1;
}
else
    die('no eid given');

use and modify this code as you like.

发布评论

评论列表(0)

  1. 暂无评论