我用节点js
创建了一个实时聊天蛋糕3但是,从客户端,发件人需要刷新页面才能看到消息
而不是没有重新加载就更新聊天的接收者
目前我的client.js看起来像这样
var socket = io.connect('http://localhost:8082'); //create connection
$('#form_message').submit(function () {
var message = $('#message').val();
var avatar = $('#avatar').val(); // champs caché contenant l'avatar
socket.emit('message', {message: message, avatar: avatar}); // Transmet le message aux autres
});
// message envoyé
socket.on('message', function(data) {
$('#list_conv').prepend('<div class="messagemoi"><img src="' + data.avatar + '"alt="image utilisateur" class="img-thumbail"/><p>' + data.message +'</p><span class="date_message">' + data.date + '</span></div>');
})
// retour du server
socket.on('messagerepondu', function(data) {
$('#list_conv').prepend('<div class="messagemoi other"><img src="' + data.avatar + '"alt="image utilisateur" class="img-thumbail right"/><p>' + data.message +'</p><span class="date_message">' + data.date + '</span></div>');
})
我的控制器看起来像这样
public function add()
{
$data = array(
'user_id' => $this->Auth->user('username'), // expediteur
'destinataire' => $this->request->data['destinataire'],
'message' => $this->linkify_tweet($this->request->data['message']), // message
'conv' => $conversation,
//evenement abonnement
'nom_session' => $this->Auth->user('username'),//nom de session
'avatar_session' => $this->Auth->user('avatarprofil'),
'new_conv' => $new_conv
);
$message = $this->Messagerie->patchEntity($message, $data);
if ($this->Messagerie->save($message))
{
if($this->testnotifmessage($this->request->data['destinataire']) == "oui")
{
//évènenement
$event = new Event('Model.Messagerie.afterAdd', $this, ['message' => $message]);
$this->eventManager()->dispatch($event);
}
// fin évènement
}
else
{
$this->Flash->error(__('Message non envoyé.'));
}
//return $this->redirect('http://localhost/instatux/conversation-1768986576');
}
}
如何将消息放入发送方div中,通过cakephp控制器在数据库中插入消息而不重新加载?