我使用Alasdair Boyd教程(here)在Codeigniter中创建聊天服务。每次用户输入新消息时,都会将其提交到数据库,并且消息会在屏幕上快速显示,并显示所有其他消息。
在我尝试引入codeigniter电子邮件功能之前,一切正常。当运行发送功能(运行codeigniter电子邮件)时,会发送一封电子邮件,但快速更新屏幕上消息的功能会消失。
因此,在我看来,如果我使用codeigniter电子邮件代码,它会停止在chat.js中运行以下代码行
$(document).ready(function() {
setInterval(function (){get_chat_messages();},2500);
(...)
但是,我不明白为什么。下面我提交所有代码。提前感谢您的帮助。
我的观点:
<div id = "chat_viewport"></div>
<div id="chat_input">
<input id = "chat_message" name = "chat_message" type = "text" value = "" tabindex = "1"/>
<?php echo anchor('email','Say it', array('title'=> 'Send this chat message', 'id'=>'submit_message'));?>
<div class = "clearer"></div>
</div>
我的控制器
<?php
class Email extends CI_Controller{
public function index(){
if(!$this->session->userdata('logged_in')){
redirect('frontpage');
}
$id = $this->session->userdata('user_id');
$data['chat_id'] = "1";
$this->session->set_userdata('last_chat_message_id_'.$data['chat_id'],0);
$data['user_id'] = $this->session->userdata('user_id');
$data['first_name'] = $this->User_model->get_name($id );
$data['main_content'] = 'email';
$this->load->view('layouts/main', $data);
}
function ajax_add_chat_message()
{
$chat_id = $this->input->post('chat_id');
$user_id = $this->input->post('user_id');
$chat_message_content = $this->input->post('chat_message_content',TRUE);
$this->send($chat_message_content);
$this->Email_model->add_chat_messages($chat_id, $user_id, $chat_message_content);
//grab and return all messages
echo $this->_get_chat_messages($chat_id);
}
function ajax_get_chat_messages(){
$chat_id = $this->input->post('chat_id');
echo $this->_get_chat_messages($chat_id);
}
function _get_chat_messages ($chat_id){
$last_chat_message_id = (int)$this->session->userdata('last_chat_message_id_'.$chat_id);
$chat_messages = $this->Email_model->get_chat_messages ($chat_id,$last_chat_message_id);
if ($chat_messages->num_rows()>0){
//store the last chat message id
$last_chat_message_id = $chat_messages ->row($chat_messages->num_rows() - 1)->chat_message_id;
$this->session->set_userdata('last_chat_message_id_'.$chat_id,$last_chat_message_id);
//We have some chat
$chat_messages_html = '<ul>';
foreach($chat_messages->result() as $chat_message)
{
$li_class = ($this->session->userdata('user_id')==$chat_message->user_id)? 'class = "by_current_user"' : '';
$chat_messages_html .= '<li ' . $li_class .'>'.'<span class = "chat_message_header">'. $chat_message->chat_message_timestamp .' by '. $chat_message->name . '</span><p class = "message_content">'. $chat_message->chat_message_content.'</p></li>';
}
$chat_messages_html .= '</ul>';
$result = array('status' => 'ok', 'content' => $chat_messages_html);
return json_encode($result);
}else{
$result = array('status' => 'ok', 'content' => '');
return json_encode($result);
exit();
}
}
function send($message = null){
$config = array(
'protocol' => 'smtp',
'smtp_host'=> 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' =>'patient@gmail.com',
'smtp_pass' => '12345'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from ('patient@gmail.com','Mike');
$this->email->to ('hospital@gmail.com');
$this->email->subject('This is an email test');
$this->email->message($message);
if($this->email->send())
{
echo 'Your email was sent.';
}
else
{
show_error($this->email->print_debugger());
}
return false;
}
}
我的模特
<?php
class Email_model extends CI_Model{
function add_chat_messages ($chat_id, $user_id, $chat_message_content)
{
/* $query_str = "INSERT INTO chat_messages (chat_id, user_id, chat_message_content) VALUES(?,?,?)";
$this->db->query($query_str, array($chat_id,$user_id, $chat_message_content)); */
$data = array(
'chat_id' => $chat_id,
'user_id' => $user_id ,
'chat_message_content' => $chat_message_content,);
$this->db->insert('chat_messages', $data);
}
function get_chat_messages ($chat_id,$last_chat_message_id=0){
$query_str = "SELECT cm.user_id, cm.chat_message_content, cm.chat_message_id,
DATE_FORMAT(cm.create_date,'%D of %M %Y at %H:%i:%s') as chat_message_timestamp,u.name
FROM chat_messages cm
JOIN person u ON cm.user_id = u.id
WHERE cm.chat_id = ?
and cm.chat_message_id > ?
ORDER BY cm.chat_message_id ASC";
$result = $this->db->query($query_str,array($chat_id,$last_chat_message_id));
return $result;
}
}
Javascript文件Chat.js
$(document).ready(function() {
setInterval(function (){get_chat_messages();},2500);
$("input#chat_message").keypress(function(e){
if(e.which==13){
$("a#submit_message").click();
return false;
}
});
$("a#submit_message").click(function(){
var chat_message_content = $("input#chat_message").val();
if(chat_message_content == ""){return false;}
$.post(base_url + "email/ajax_add_chat_message",{chat_message_content : chat_message_content, chat_id : chat_id, user_id : user_id}, function(data) {
if (data.status == 'ok'){
var current_content = $("div#chat_viewport").html();
$("div#chat_viewport").html(current_content + data.content);
}else {
//there eas an error
}
}, "json");
$("input#chat_message").val("");
return false;
});
function get_chat_messages(){
$.post(base_url + "email/ajax_get_chat_messages",{chat_id:chat_id}, function(data) {
if (data.status == 'ok'){
var current_content = $("div#chat_viewport").html();
$("div#chat_viewport").html(current_content + data.content);
}else {
//there eas an error
}
}, "json");
}
get_chat_messages();
});