我正在开发一个彗星聊天,一切都很完美,除非我重新加载页面或我第一次加载页面。在页面加载时,我的脚本复制最后发送的消息并再次显示它,因此看起来消息已经发送了两次,但实际上消息只发送了一次,并且它在MySQL数据库中只有一条记录。
这是我使用Comet,Prototype js和jquery
的代码<script type="text/javascript">
var Comet = Class.create();
Comet.prototype = {
timestamp: 0,
url: './messages_private_travail.php?id_de_l_autre_originale=<?php echo $id_de_l_autre_originale; ?>&id_message=<?php echo addslashes(@$_GET['id_message']); ?>&chat_between=<?php echo $chat_between; ?>',
noerror: true,
initialize: function() { },
connect: function()
{
this.ajax = new Ajax.Request(this.url, {
method: 'get',
parameters: { 'timestamp' : this.timestamp },
onSuccess: function(transport) {
// handle the server response
var response = transport.responseText.evalJSON();
this.comet.timestamp = response['timestamp'];
this.comet.handleResponse(response);
this.comet.noerror = true;
},
onComplete: function(transport) {
// send a new ajax request when this request is finished
if (!this.comet.noerror)
// if a connection problem occurs, try to reconnect each 5 seconds
setTimeout(function(){ comet.connect() }, 5000);
else
this.comet.connect();
this.comet.noerror = false;
}
});
this.ajax.comet = this;
},
disconnect: function()
{
},
handleResponse: function(response)
{
//$('messages_content').innerHTML += '<div>' + response['msg'] + '</div>';
// $('messages_content').innerHTML += ' ' + response['msg'] + ' ';
jQuery('#messages_content').append(' ' + response.msg + ' ');
jQuery('.slimscroll').slimscroll({ scrollBy: '430px' });
//On appel la fonction qui permet de jouer le son
jouer_son('soundmanager2/audio/beep.mp3');
//On applique la fonction qui montre que le message a ete lu par le membre
mettreajour('nombre_nouveaux_messages', 'messages_lire_marquer_comme_lu.php?id_de_l_autre_originale=<?php echo $id_de_l_autre_originale; ?>&id_message=<?php echo addslashes(@$_GET['id_message']); ?>&chat_between=<?php $chat_between = hash('sha512',"Chat Between $id_ojm_peoples AND $id_de_l_autre_originale"); echo $chat_between; ?>', '&id_ojm_peoples=<?php echo $id_ojm_peoples; ?>');
},
doRequest: function(request)
{
new Ajax.Request(this.url, {
method: 'get',
parameters: { 'msg' : request, evalScripts: true }
});
}
}
var comet = new Comet();
comet.connect();
</script>
这就是在messages_private_travail.php中处理代码的方式
<?php
require('configurations.php');
//We take the message
$msg = addslashes(trim(@$_GET['msg']));
$maintenant = time();
if ($msg != '')
{
//IF THE CONNECTED MEMBER IS EQUAL TO THE ONE the message is sent to
if($id_de_l_autre_originale==$id_ojm_peoples)
{
//We set the message as having been read
$deja_lu = 1;
}
else
{
$deja_lu = 0;
}
//We store the message in the database
$insertion = mysqli_query($connection, "INSERT INTO cometchat VALUES(NULL, '$id_ojm_peoples', '$id_de_l_autre_originale', '$msg', '$maintenant', '$deja_lu', '0' ) ") or die(mysqli_error($connection));
die();
}
//End of if the message is not empty
// infinite loop until the data file is not modified
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
####################################################################################################
//We check the last message timestamp sent
$recup_dernier_messages = mysqli_query($connection, "SELECT MAX(`sent`) AS envoye FROM cometchat WHERE
(`from`= $id_ojm_peoples AND `to`= $id_de_l_autre) OR (`from`= $id_de_l_autre AND `to`= $id_ojm_peoples ) LIMIT 1
") or die(mysqli_error($connection));
$affichage = mysqli_fetch_assoc($recup_dernier_messages);
$temps_envoye = $affichage['envoye'];
$currentmodif = $temps_envoye;
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
//On recupere encore le temps ou le dernier message a ete envoye
$recup_dernier_messages = mysqli_query($connection, "SELECT MAX(`sent`) AS envoye FROM cometchat WHERE
(`from`= $id_ojm_peoples AND `to`= $id_de_l_autre) OR (`from`= $id_de_l_autre AND `to`= $id_ojm_peoples ) LIMIT 1
") or die(mysqli_error($connection));
$affichage = mysqli_fetch_assoc($recup_dernier_messages);
$temps_envoye = $affichage['envoye'];
$currentmodif = $temps_envoye;
}
########################################################################################
//On recupere le membre a utiliser
###########################################################################################
$recup_messages = mysqli_query($connection, "SELECT * FROM cometchat WHERE
`sent`= '$currentmodif' AND ((`from`= $id_ojm_peoples AND `to`= $id_de_l_autre) OR (`from`= $id_de_l_autre AND `to`= $id_ojm_peoples )) LIMIT 1
") or die(mysqli_error($connection));
$affichage = mysqli_fetch_assoc($recup_messages);
$id_message = $affichage['id'];
$id_ojm_peoples_from = $affichage['from'];
$id_ojm_peoples_to = $affichage['to'];
$message = stripslashes($affichage['message']);
##########################################################################################
$id_a_utiliser = $id_ojm_peoples_from;
//Si le id de celui qui envoit le message est egale a celui du membre connecte
$lastactivity_nom_prenom_from = peoples_infos($id_a_utiliser, $nom_table='ojm_peoples', $champs='lastactivity');
$est_connecte_nom_prenom_from = est_connecte_is_online($lastactivity_nom_prenom_from);
$message_anchors = str_ireplace(' ', '', "id_message_ $id_message");
if($est_connecte_nom_prenom_from=='Oui')
{
$est_en_ligne='online';
}
else
{
$est_en_ligne='offline';
}
$temps_du_message_envoye = time_ago($temps_envoye, $langue='en');
$nom_prenom = nom_prenom($id_a_utiliser);
// $url_avantar_profile = url_logo_avantar_peoples($id_a_utiliser);
$url_avantar_profile = peoples_main_avantar($id_super_admin, 'id_ojm_peoples', $id_a_utiliser);
########################################################################################
//On ajoute les smileys au message
$message = smileys_emoticones($message, $url_repertoire_big_smileys='ojm_chat/smileys_to_use', $a_sticker_for_you='', $lister_smileys='', $nom_du_champs_input='');
//On remplace les urls youtube vimeo et les liens
$message =convertir_youtube_et_vimeo_et_liens($message) ;
// return a json array
$response = array();
//Si le membre connecte actuel est celui a qui le message a ete envoye on marque le message comme etant lu
if($id_ojm_peoples_to==$id_ojm_peoples)
{
//On marque le message comme etant lu car le recepteur est connecte au chat et a cette page
marquer_comme_lu_mark_as_read($id_message, $marquer_comme='lu');
}
//On verifie pour voir si le message a ete vu par le membre
$message_a_ete_lu = a_ete_lu_ou_pas_has_been_read_or_not($id_message, $a_ete='lu');
if($message_a_ete_lu=='Oui'&&$id_ojm_peoples_from==$id_ojm_peoples)
{
$mot_vu = " <i class='fa fa-fw fa-check green'></i> ";
}
else
{
$mot_vu = " <i class='fa fa-fw fa-check'></i> ";
}
//On modifie la derniere activite du membre connecte actuel
update_lastactivity();
//On met le message formate dans une base de donnees
$response['msg'] = "<div class='item'><a name='$message_anchors'></a><img src='$url_avantar_profile' alt='$nom_prenom' class='$est_en_ligne'/><p class='message'><small class='text-muted pull-right'><i class='fa fa-clock-o'></i> $temps_du_message_envoye $mot_vu </small><a href='people_profile.php?id_ojm_peoples=$id_a_utiliser' class='name'>$nom_prenom</a>". $message. " </p></div> ";
$response['id_ojm_peoples'] = $id_ojm_peoples;
$response['id_de_l_autre_originale'] = $id_de_l_autre_originale;
$response['id_a_utiliser'] = $id_a_utiliser;
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
?>
这是我的表结构
CREATE TABLE IF NOT EXISTS `ojm_chat` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`from_id` int(10) unsigned NOT NULL,
`to_id` int(10) unsigned NOT NULL,
`message` text NOT NULL,
`sent` int(10) unsigned NOT NULL DEFAULT '0',
`read_statu` tinyint(1) unsigned NOT NULL DEFAULT '0',
`is_deleted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `to` (`to_id`),
KEY `from` (`from_id`),
KEY `direction` (`is_deleted`),
KEY `read` (`read_statu`),
KEY `sent` (`sent`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
问题
我在代码中到处搜索,但仍然没有看到副本的位置。如何删除副本?感谢
答案 0 :(得分:0)
由于您说它在聊天中添加了一条额外的消息,而数据库中只有一条消息记录,我建议您加载页面,其中包含用于加载消息的消息数(-1) )。然后,由于您的脚本已经在页面加载时添加了最后一条消息,脚本本身将通过执行减号(-1)
添加您在页面加载时删除的最后一条消息因此,如果要加载您用来执行类似
的页面$limit = 10;
$load = mysqli_query($con, "SELECT * FROM ojm_chat WHERE `from_id`='$user_a' or `to_id`='$user_b` LIMIT $limit") or die(mysqli_error($con));
现在你必须这样做
$limit = 10;
$limit = $limit - 1;
$load = mysqli_query($con, "SELECT * FROM ojm_chat WHERE `from_id`='$user_a' or `to_id`='$user_b` LIMIT $limit") or die(mysqli_error($con));
这样,在加载时,脚本将不会显示您的上一条消息,但是您在加载时添加最后一条消息的ajax将为您执行此操作