我正在为我的网站构建一个简单的消息传递系统,它看起来很慢,有时会崩溃浏览器。以下是代码。
首先,我写了一个ajax请求,以便从数据库中获取所有用户聊天
setInterval(function () {
$.ajax({
type: "GET",
url: "get_chat.php",
dataType: "html",
success: function (response) {
$(".msgView").html(response);
if (response !== lastResponse) {
var audio = new Audio('audio/solemn.mp3')
audio.play()
}
lastResponse = response
}
});
},2000);
这里是 get-chat.php
<?php
$us_id = $_SESSION['log_id'];
//echo empty($_SESSION['hash']) ? 'not set' : $_SESSION['hash'];
$hasher = $_SESSION['hash'];
$mesql =<<<EOF
SELECT from_id, message FROM messager WHERE group_hash = '$hasher';
EOF;
$meret = $db->query($mesql);
while ($merow = $meret->fetchArray(SQLITE3_ASSOC))
{
$from_id = $merow['from_id'];
$messages = $merow['message'];
$usql =<<<EOF
SELECT * FROM users WHERE userid = '$from_id';
EOF;
$uret = $db->query($usql);
while ($urow = $uret->fetchArray(SQLITE3_ASSOC)) {
$from_fname = $urow['fname'];
$from_img = $urow['profimages'];
if ($from_id != $_SESSION['log_id']) {
echo '
<div class="recMsgBubble">
<div class="recBubbleImg"><img src="'.$from_img.'"></div>
<div class="recBubbleMsg">'.$messages.'</div>
</div>';
//<div class='from_bubble'><div class='from_img'><img src='$from_img'></div><p>$messages</p></div><br>
} else {
echo '
<div class="userMsgBubble">
<div class="userBubbleImg"><img src="'.$from_img.'"></div>
<div class="userBubbleMsg">'.$messages.'</div>
</div>';
//<div class='rep_bubble'><div class='rep_img'><img src='$from_img'></div><p>$messages</p></div><br>
}
}
$csql =<<<EOF
SELECT * FROM banks WHERE bname = '$from_id';
EOF;
$cret = $db->query($csql);
while ($crow = $cret->fetchArray(SQLITE3_ASSOC)) {
$from_fname = $crow['bname'];
$from_img = $crow['banklogo'];
if ($from_id = $from_fname) {
echo '<div class="recMsgBubble">
<div class="recBubbleImg"><img src="'.$from_img.'">
</div>
<div class="recBubbleMsg">'.$messages.'</div>
</div>';
} else {
echo '<div class="userMsgBubble">
<div class="userBubbleImg"><img src="'.$from_img.'">
</div>
<div class="userBubbleMsg">'.$messages.'</div>
</div>';
}
}
}
?>
同样,为了发送聊天,使用ajax请求,如下所示
$("#msgSender").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "send_chat.php",
data: $(this).serializeArray(),
dataType: "json",
success: function(response) {
console.log(response);
},
error: function(response) {
}
});
$('#userMsgField').val("");
});
这是send_chat.php
<?php
require_once ("db.php");
$db = new MyDB();
session_start();
if (isset($_POST['userMsgField']) && !empty($_POST['userMsgField']) || isset($_POST['hash']) && !empty($_POST['hash']))
{
$my_id = $_SESSION['log_id'];
$rep_msg = $_POST['userMsgField'];
$hash = $_SESSION['hash'];
$flag = 0;
$sql =<<<EOF
SELECT * FROM connect WHERE (user_one = '$my_id' AND hash = '$hash') OR (user_two = '$my_id' AND hash = '$hash');
EOF;
$ret = $db->query($sql);
while ($row = $ret->fetchArray(SQLITE3_ASSOC))
{
$user_one = $row['user_one'];
$user_two = $row['user_two'];
if ($user_one == $my_id)
{
$to_id = $user_two;
}
else
{
$to_id = $user_one;
}
$isql =<<<EOF
INSERT INTO messager (message, group_hash, from_id, flag, to_id) VALUES (:message, :group_hash, :from_id, :flag, :to_id);
EOF;
$bsql =<<<EOF
INSERT INTO chatportal (message, group_hash, from_id, flag, to_id)
VALUES (:message, :group_hash, :from_id, :flag, :to_id);
EOF;
$stmt = $db->prepare($isql);
$bstmt = $db->prepare($bsql);
$stmt->bindValue(':message', $rep_msg, SQLITE3_TEXT);
$stmt->bindValue(':group_hash', $hash, SQLITE3_INTEGER);
$stmt->bindValue(':from_id', $my_id, SQLITE3_INTEGER);
$stmt->bindValue(':flag', $flag, SQLITE3_INTEGER);
$stmt->bindValue(':to_id', $to_id, SQLITE3_TEXT);
$bstmt->bindValue(':message', $rep_msg, SQLITE3_TEXT);
$bstmt->bindValue(':group_hash', $hash, SQLITE3_INTEGER);
$bstmt->bindValue(':from_id', $my_id, SQLITE3_INTEGER);
$bstmt->bindValue(':flag', $flag, SQLITE3_INTEGER);
$bstmt->bindValue(':to_id', $to_id, SQLITE3_TEXT);
$result = $stmt->execute();
$bresult = $bstmt->execute();
if ($reuslt && $bresult)
{
echo "GHood";
}
}
}
我认为它缓慢的原因是它试图每2秒获取一次消息。如果这是问题,请问我该如何解决?
如果不是这个问题的解决方案是什么?提前谢谢。
答案 0 :(得分:1)
如果从头开始构建PHP消息传递系统,它的速度将很慢。您可以执行以下三项操作来至少加快速度: