长轮询时无法运行另一个ajax?
我正在尝试使用长轮询和长轮询在网络上制作一个简单的聊天应用程序。 php.
主要是我在客户端有2个ajax函数,
1.更新:更新聊天消息。
2. send :发送用户输入的消息。
更新函数执行长轮询,即等待 10 秒或直到有新消息可用。 send 函数写入数据库。
我的问题是,当更新功能运行(长轮询)时,发送功能无法运行。并且只有在更新函数运行完成后,发送函数才会运行。这是预期的行为还是我的代码有问题?
如果您想查看网络,可以在这里访问:http://tedhost.awardspace.us
如果你懒得注册,你可以使用用户名 dummy0 和密码 123456
这是代码..抱歉,如果我的代码很乱..
ajax代码: 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);
?>
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 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);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用默认的会话处理程序,则一次只能有一个脚本打开会话,因为一次只能写入一个会话文件,否则可能会发生损坏。
您可以通过在脚本中尽快调用 session_write_close 来解决此问题。对于仅读取会话数据的脚本,您应该能够在 session_start 之后直接调用它。对于对会话进行更改的脚本,您必须在对会话进行最后一次更改后立即调用它。
虽然使用 session_write_close 意味着您对 $_SESSION 所做的任何后续更改都不会被保存,但 $_SESSION 数组仍然可以访问并包含上次从会话加载的值。
http://www.php.net/session_write_close 有相关信息。
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.