不能再跑阿贾克斯,而长轮询? [英] Cannot run another ajax while long polling?

查看:98
本文介绍了不能再跑阿贾克斯,而长轮询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让网络上的一个简单的聊天应用程序,使用长轮询和放大器; PHP的。照片

主要是我有2个AJAX功能在客户端上,
1.更新:更新聊天消息
。 2.发送:用户输入发送短信
。 更新功能做一个长轮询,这要等到10秒钟,直到新的消息提供。 发送函数写入到数据库。

我的问题是,在更新功能时运行(长轮询),发送功能无法运行。而只有在更新功能运行完毕后,再发送函数运行。这是一个预期的行为,或有什么问题我的code?

如果妳想看的网页ü在这里可以访问: http://tedhost.awardspace.us
如果你就像慵懒注册ü可以使用的用户名dummy0,密码为123456

下面是code..Sorry如果我的code是凌乱..

阿贾克斯code: http://tedhost.awardspace.us/ajax.js
update_chat.php

 < PHP
在session_start();
$文件名=wew.xt;
$ USER = $ _SESSION [用户];
$去年= $ _SESSION [lmsgtime];

如果(file_exists($文件名)){
    $喇嘛= 0;
    $ mulai =时间();
    做 {
        $ =翅片的fopen($文件名,R);
        $电流= 0;
        的fscanf($翅,%D,$电流);
        fclose函数($翅);
        usleep(10000);
        $ akhir =时间();
    }而($电流I = $最后&安培;&安培; $ akhir  -  $ mulai&其中; = 10);
}
包括con.php;
mysql_select_db($数据库名,$ CON);
$ SQL =SELECT * FROM聊天WHERE时间>$最后按时间ASC;
$水库=的mysql_query($ SQL);
/ *
$喇嘛= 0;
而(mysql_num_rows($水库)< = 0&功放;&安培; $喇嘛< = 10000000){
    $水库=的mysql_query($ SQL);
    usleep(500);
    $喇嘛+ = 500;
}
* /
如果(!$水库)
    死亡(错误);
从$ =;
而($行= mysql_fetch_array($水库)){
    从$ = $出去。 &所述;股利的id =;
    如果($行[用户] == $用户)
        从$ = $出去。 \chatme \;
    否则,如果($行[用户] ==SYS)
        从$ = $出去。 \chatsystem \;
    其他
        从$ = $出去。 \chatother \;
    从$ = $出去。 >中;
    从$ = $出去。日期((H:我:S),$行[时间]);
    从$ = $出去。 $行[用户];
    从$ = $出去。 :。函数stripslashes($行[数据]);
    从$ = $出去。 < / DIV>中;
    $去年= $行[时间];
}
$ _SESSION [lmsgtime] = $去年;
回声$输出;
?>
 

send.php

 < PHP
$时间=时间();
在session_start();
包括con.php;
$数据= mysql_real_escape_string($ _ GET [味精]);
$ USER = mysql_real_escape_string($ _ SESSION [用户]);
mysql_select_db($数据库名,$ CON);
$ SQL =INSERT INTO聊天VALUES('$时间','$用户','$数据');
$水库=的mysql_query($ SQL);
如果(!$水库)
    死亡(犯错);
其他
    回声确定;
$ LHO =的fopen(wew.xt,瓦特);
fprintf中($ LHO,%D \ N,$时间);
fclose函数($ LHO);
?>
 

解决方案

如果您使用的是默认的会话处理程序那么只有一个脚本可以有会话同时打开,因为只有一件事可以写在一个会话文件一时间,否则可能会发生损坏。

这是可能的,你可以通过调用session_write_close尽快在脚本中解决这个问题。在该只读会话数据的脚本的情况下,你应该能够在session_start后直接调用它。在该修改会话脚本的情况下,你就必须在最后一次更改为您做出会议之后立即调用它。

在使用session_write_close意味着您对$ _SESSION任何后续更改将不会被保存,$ _SESSION数组仍然可以访问,并包含在会议最后加载的值。

http://www.php.net/session_write_close 有相关信息。

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);
?>

解决方案

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.

这篇关于不能再跑阿贾克斯,而长轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆