开发者

Cannot run another ajax while long polling?

I'm trying to make a simple chat app on web, using long polling & php.

Mainly I have 2 ajax functions on the client,

1. update : to update the chat messages.

2. send : to send message the user enters.

The update function do a long polling, which is to wait until for 10 seconds or until new message is available. The send function writes to a database.

My problem is, while the update function is running (long polling), the send function cannot run. And only after the update function has finished running, then the send function runs. Is this an expected behaviour or there is something wrong with my code?

If u want to see the web u can access it here : http://tedhost.awardspace.us

And if u're lazy to sign up u can use username dummy0 and password 123456

Here is the code..Sorry if my code is messy..

The ajax 开发者_JAVA百科code : http://tedhost.awardspace.us/ajax.js

update_chat.php

<?php
session_start();
$filename = "wew.xt";
$user = $_SESSION["user"];
$last = $_SESSION["lmsgtime"];

if (file_exists($filename)) {
    $lama = 0;
    $mulai = time();
    do {
        $fin = fopen($filename, "r");
        $current = 0;
        fscanf($fin, "%d", $current);
        fclose($fin);
        usleep(10000);
        $akhir = time();
    } while($current <= $last && $akhir - $mulai <= 10);
}
include "con.php";
mysql_select_db($dbname, $con);
$sql = "SELECT * FROM chats WHERE time > '$last' ORDER BY time ASC";
$res = mysql_query($sql);
/*
$lama = 0;
while (mysql_num_rows($res) <= 0 && $lama <= 10000000) {
    $res = mysql_query($sql);
    usleep(500);
    $lama += 500;
}
*/
if (!$res)
    die("error");
$out = "";
while($row = mysql_fetch_array($res)) {
    $out = $out . "<div id=";
    if ($row["user"] == $user)
        $out = $out . "\"chatme\"";
    else if ($row["user"] == "sys")
        $out = $out . "\"chatsystem\"";
    else
        $out = $out . "\"chatother\"";
    $out = $out . ">";
    $out = $out . date("(h:i:s) ", $row["time"]);
    $out = $out . $row["user"];
    $out = $out . ": " . stripslashes($row["data"]);
    $out = $out . "</div>";
    $last = $row["time"];
}
$_SESSION["lmsgtime"] = $last;
echo $out;
?>

send.php

<?php
$time = time();
session_start();
include "con.php";
$data = mysql_real_escape_string($_GET["msg"]);
$user = mysql_real_escape_string($_SESSION["user"]);
mysql_select_db($dbname, $con);
$sql = "INSERT INTO chats VALUES ('$time', '$user', '$data')";
$res = mysql_query($sql);
if (!$res)
    die("err");
else
    echo "ok";
$lho = fopen("wew.xt", "w");
fprintf($lho, "%d\n", $time);
fclose($lho);
?>


If you're using the default session handler then only one script can have the session open at a time, as only one thing can write to a session file at a time otherwise corruption may occur.

It is possible that you can get around this problem by calling session_write_close as soon as possible in your scripts. In the case of scripts that only read session data you should be able to call it straight after session_start. In the case of scripts that make changes to the session, you'll have to call it immediately after the last change to the session you make.

While using session_write_close means that any subsequent changes you make to $_SESSION won't be saved, the $_SESSION array will still be accessible and contain the values last loaded from the session.

http://www.php.net/session_write_close has the relevant information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜