Javascript Ajax mysql长轮询
我正在使用mysql开发一个ajax长轮询函数,但它似乎并没有真正起作用。我的电脑过热,几分钟后网站崩溃。此外,poll.php甚至没有从data.php接收内容,相反,poll.php显示{“type”:“connect_error”) 我以前没有做过很长时间的民意调查 我有3个文件: data.phpJavascript Ajax mysql长轮询,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我正在使用mysql开发一个ajax长轮询函数,但它似乎并没有真正起作用。我的电脑过热,几分钟后网站崩溃。此外,poll.php甚至没有从data.php接收内容,相反,poll.php显示{“type”:“connect_error”) 我以前没有做过很长时间的民意调查 我有3个文件: data.php <?php session_start(); define ('DB_HOST', 'localhost'); define ('DB_USER', 'root'); define
<?php
session_start();
define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASSWORD', 'root');
define ('DB_NAME', 'story_creator');
function sqlSelect($query) {
// Create connection
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, $query);
// Check connection
if (mysqli_errno($conn)) {
echo "Failed: " . mysqli_error($conn);
}
$resultArray = array();
if ($result) {
while ($row = mysqli_fetch_array($result)) {
$resultArray[] = $row;
}
}
return $resultArray;
}
$news = sqlSelect("SELECT type FROM users_news_feed WHERE user_id = {$_SESSION['user_id']} AND date > 0;");
echo json_encode($news);
?>
如果您想让我重新创建它,但一小时内没有得到任何帮助,那么使用JavaScriptCurrent的文件在网站的页脚中的位置。祝您好运,请提供文件的完整路径$url=http://localhost/my%20projects/longpolling/data.php“
然后使用文件获取内容($url)获取内容
其次据我所知filemtime($filename);
给出上次修改文件的时间。我无法理解您为什么要检查它,因为它不会告诉您文件的响应是否已更改。请将上述注释中的url修改为$filename=http://localhost/functions/data.php“;
如果您需要,当前在站点页脚中使用Javascript的文件在哪里如果你想让我重新创建它,一个小时内你就没有得到任何帮助,请用文件信息ping我。祝你好运,请告诉我文件的完整路径$url=http://localhost/my%20projects/longpolling/data.php“
然后使用file\u get\u contents($url)
第二个据我所知的filemtime($filename)获取内容;
给出上次修改文件的时间。我无法理解您为什么要检查它,因为它不会告诉您文件的响应是否已更改。请将上述注释中的url修改为$filename=http://localhost/functions/data.php“;
<?php
$filename = 'data.php';
$lastmodif = isset($_POST['timestamp'])? $_POST['timestamp']: 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) {
usleep(10000);
clearstatcache();
$currentmodif = filemtime($filename);
}
$response = array();
$response['type'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
?>
<script>
var timestamp = null;
function waitForMsg(){
$.ajax({
type: "POST",
url: "functions/poll.php",
async: true,
cache: false,
timeout: 50000, /* Timeout in ms */
data: { 'timestamp': timestamp },
success: function(data){
// if(data == ''){
// Just console.log the data
console.log(data);
setInterval(
waitForMsg(), /* Try again after.. */
1000); /* milliseconds (15seconds) */
// }
},
complete: function(){
setInterval(waitForMsg(), 7000);
}
});
}
$(document).ready(function() {
waitForMsg();
});
</script>