我有这个Jquery / Php进程供用户聊天,如果我向在线用户发送消息并且我最小化聊天窗口或者关闭或者刷新页面我有一些问题,聊天窗口每个都弹出时间,我希望在窗口附近好好关闭,当我最小化酒吧的心情留在那里,即使我刷新页面,以保持最小化在酒吧。
下一个问题有时当我发送消息并且我点击输入消息未显示,但是当我刷新页面时,是否存在。
Ps:剧本不是我的,我在youtube上找到了。
Jquery Chat脚本:
<?php
if(isset($_POST['conversacom'])){
include_once "../defines.php";
require_once('../classes/BD.class.php');
BD::conn();
$mensagens = array();
$id_conversa = (int)$_POST['conversacom'];
$online = (int)$_POST['online'];
$pegaConversas = BD::conn()->prepare("SELECT * FROM `mensagens` WHERE (`id_de` = ? AND `id_para` = ?) OR (`id_de` = ? AND `id_para` = ?) ORDER BY `id` DESC LIMIT 10");
$pegaConversas->execute(array($online, $id_conversa, $id_conversa, $online));
while($row = $pegaConversas->fetch()){
$fotouser = '';
if($online == $row['id_de']){
$janela_de = $row['id_para'];
}elseif($online == $row['id_para']){
$janela_de = $row['id_de'];
$pegaFoto = BD::conn()->prepare("SELECT `photo` FROM `mls_users` WHERE `id` = '$row[id_de]'");
$pegaFoto->execute();
while($usr = $pegaFoto->fetch()){
$fotouser = ($usr['photo'] == '') ? 'default.jpg' : $usr['photo'];
}
}
$emotions = array(':)', ':@', '8)', ':D', ':3', ':(', ';)');
$imgs = array(
'<img src="emotions/nice.png" width="14"/>',
'<img src="emotions/angry.png" width="14"/>',
'<img src="emotions/cool.png" width="14"/>',
'<img src="emotions/happy.png" width="14"/>',
'<img src="emotions/ooh.png" width="14"/>',
'<img src="emotions/sad.png" width="14"/>',
'<img src="emotions/right.png" width="14"/>'
);
$msg = str_replace($emotions, $imgs, $row['mensagem']);
$mensagens[] = array(
'id' => $row['id'],
'mensagem' => utf8_encode($msg),
'fotoUser' => $fotouser,
'id_de' => $row['id_de'],
'id_para' => $row['id_para'],
'janela_de' => $janela_de
);
}
die( json_encode($mensagens) );
}
?>
历史Php文件:
<?php
if(isset($_POST['mensagem'])){
include_once "../defines.php";
require_once('../classes/BD.class.php');
BD::conn();
$mensagem = utf8_decode( strip_tags(trim(filter_input(INPUT_POST, 'mensagem', FILTER_SANITIZE_STRING))) );
$de = (int)$_POST['de'];
$para = (int)$_POST['para'];
if($mensagem != ''){
$insert = BD::conn()->prepare("INSERT INTO `mensagens` (id_de, id_para, mensagem, time, lido) VALUES (?,?,?,?,?)");
$arrayInsert = array($de, $para, $mensagem, time(), 0);
if($insert->execute($arrayInsert)){
echo 'ok';
}else{
echo 'no';
}
}
}
?>
提交Php文件:
<?php
if(isset($_GET)){
include_once "../defines.php";
require_once('../classes/BD.class.php');
BD::conn();
$userOnline = (int)$_GET['user'];
$timestamp = ($_GET['timestamp'] == 0) ? time() : strip_tags(trim($_GET['timestamp']));
$lastid = (isset($_GET['lastid']) && !empty($_GET['lastid'])) ? $_GET['lastid'] : 0;
$usersOn = array();
$agora = date('Y-m-d H:i:s');
$expira = date('Y-m-d H:i:s', strtotime('+1 min'));
$upOnline = BD::conn()->prepare("UPDATE `mls_users` SET `limite` = ? WHERE `id` = ?");
$upOnline->execute(array($expira, $userOnline));
$pegaOnline = BD::conn()->prepare("SELECT * FROM `mls_users` WHERE `id` != '$userOnline' ORDER BY `id` DESC");
$pegaOnline->execute();
while($onlines = $pegaOnline->fetch()){
$photo = ($onlines['photo'] == '') ? 'default.jpg' : $onlines['photo'];
$blocks = explode(',', $onlines['blocks']);
if(!in_array($userOnline, $blocks)){
if($agora >= $onlines['limite']){
$usersOn[] = array('id' => $onlines['id'], 'nome' => utf8_encode($onlines['username']), 'foto' => $photo, 'status' => 'off');
}else{
$usersOn[] = array('id' => $onlines['id'], 'nome' => utf8_encode($onlines['username']), 'foto' => $photo, 'status' => 'on');
}
}
}
if(empty($timestamp)){
die(json_encode(array('status' => 'erro')));
}
$tempoGasto = 0;
$lastidQuery = '';
if(!empty($lastid)){
$lastidQuery = ' AND `id` > '.$lastid;
}
if($_GET['timestamp'] == 0){
$verifica = BD::conn()->prepare("SELECT * FROM `mensagens` WHERE `lido` = 0 ORDER BY `id` DESC");
}else{
$verifica = BD::conn()->prepare("SELECT * FROM `mensagens` WHERE `time` >= $timestamp".$lastidQuery." AND `lido` = 0 ORDER BY `id`DESC");
}
$verifica->execute();
$resultados = $verifica->rowCount();
if($resultados <= 0){
while($resultados <= 0){
if($resultados <= 0){
//durar 30 segundos verificando
if($tempoGasto >= 30){
die(json_encode(array('status' => 'vazio', 'lastid' => 0, 'timestamp' => time(), 'users' => $usersOn)));
exit;
}
//descansar o script por um segundo
sleep(1);
$verifica = BD::conn()->prepare("SELECT * FROM `mensagens` WHERE `time` >= $timestamp".$lastidQuery." AND `lido` = 0 ORDER BY `id`DESC");
$verifica->execute();
$resultados = $verifica->rowCount();
$tempoGasto += 1;
}
}
}
$novasMensagens = array();
if($resultados >= 1){
$emotions = array(':)', ':@', '8)', ':D', ':3', ':(', ';)');
$imgs = array(
'<img src="emotions/nice.png" width="14"/>',
'<img src="emotions/angry.png" width="14"/>',
'<img src="emotions/cool.png" width="14"/>',
'<img src="emotions/happy.png" width="14"/>',
'<img src="emotions/ooh.png" width="14"/>',
'<img src="emotions/sad.png" width="14"/>',
'<img src="emotions/right.png" width="14"/>'
);
while($row = $verifica->fetch()){
$fotouser = '';
$janela_de = 0;
if($userOnline == $row['id_de']){
$janela_de = $row['id_para'];
}elseif($userOnline == $row['id_para']){
$janela_de = $row['id_de'];
$pegaUsr = BD::conn()->prepare("SELECT `photo` FROM `mls_users` WHERE `id` = '$row[id_de]'");
$pegaUsr->execute();
while($usr = $pegaUsr->fetch()){
$fotouser = ($usr['photo'] == '') ? 'default.jpg' : $usr['photo'];
}
}
$msg = str_replace($emotions, $imgs, $row['mensagem']);
$novasMensagens[] = array(
'id' => $row['id'],
'mensagem' => utf8_encode($msg),
'fotouser' => $fotouser,
'id_de' => $row['id_de'],
'id_para' => $row['id_para'],
'janela_de' => $janela_de
);
}
}
$ultimaMsg = end($novasMensagens);
$ultimoId = $ultimaMsg['id'];
die(json_encode(array('status' => 'resultados', 'timestamp' => time(), 'lastid' => $ultimoId, 'dados' => $novasMensagens, 'users' => $usersOn)));
}
?>
Stream Php文件:
-server
-Xmx512m
-Djava.awt.headless=true
答案 0 :(得分:0)
jQuery.ajax({
type: 'POST',
url: 'sys/submit.php',
data: {mensagem: texto, de: userOnline, para: para},
success: function(retorno){
if(retorno = 'ok'){
jQuery('.msg').val('');
}else{
alert("Ocorreu um erro ao enviar a mensagem");
}
}
});
}
});