使用ajax JSON将聊天消息内容轮询到屏幕

使用ajax JSON将聊天消息内容轮询到屏幕,ajax,codeigniter,chat,polling,Ajax,Codeigniter,Chat,Polling,嗨,我成功地设计了一个用户之间的聊天信息系统,它工作得很好。目前我遇到的唯一问题是实时地将数据从聊天表轮询到浏览器 消息使用ajax显示,setInterval在我检查控制台时起作用。问题在于,它不会将新条目捕获到表中以供显示,因此用户必须不断刷新页面以查看新内容 请帮忙。我的代码如下。请原谅我的文件命名约定,因为我稍后会更改它。 PS这是使用codeigniter框架开发的 **Chats.php - Controller** public function ajax_get_cha

嗨,我成功地设计了一个用户之间的聊天信息系统,它工作得很好。目前我遇到的唯一问题是实时地将数据从聊天表轮询到浏览器

消息使用ajax显示,setInterval在我检查控制台时起作用。问题在于,它不会将新条目捕获到表中以供显示,因此用户必须不断刷新页面以查看新内容

请帮忙。我的代码如下。请原谅我的文件命名约定,因为我稍后会更改它。 PS这是使用codeigniter框架开发的

**Chats.php - Controller**

    public function ajax_get_chat_messages(){
    echo $this->_get_chat_messages();
    }
    function _get_chat_messages($recipient = null)
    {
    $user_id = $this->session->userdata('user_id');
    $recipient = $this->input->post('recipient');
    $data['recipient'] = $this->User_model->get_users($user_id);
    $data['chats_count'] = $this->Chats_model->get_chat_messages_count($recipient);
    $content = $this->Chats_model->get_chat_messages_count($recipient);
    $data['chats'] = $this->Chats_model->get_chat_messages($user_id);
    $result = array('status' =>'ok', 'content'=>$content);
    return json_encode($result);
    }

**Model - Chats_model.php**

    public function get_chat_messages_count($recipient = null){
    $session = $this->session->userdata('user_id');
    $this->db->select('*');
    $this->db->from('chat_messages');
    $this->db->join('users', 'users.user_id = chat_messages.user_id'); 
    $this->db->where(array('chat_messages.user_id' => $session));
    $this->db->where(array('chat_messages.recipient' => $recipient)); 
    $this->db->or_where(array('chat_messages.user_id' => $recipient));
    $this->db->where(array('chat_messages.recipient' => $session));     
    $this->db->where_in(array('chat_messages.chat_id' => $session ,   $recipient));
    $query = $this->db->get();  
    return $query->result_array(); 
    }

**View - chats_view.php**

    <script type="text/javascript">
    var user = "<div class='timeline-item' id='view'><ul><?php foreach($chats_count as $chat){echo '<li>'; echo $chat['username']; echo '</li>'; }?></ul></div>";
    </script>
    <div class="wrapper wrapper-content">
    <div class="row animated fadeInRight">
    <div class="col-lg-12">
    <div class="ibox float-e-margins">
    <div class="ibox-title">                            
    <h5>Chat</h5>                       
    <div class="ibox-tools" >                   
    </div>
    </div>  
    <div class="ibox-content inspinia-timeline" id="view1" > 
    </div>   
    </div>
    </div>   
    </div>
    </div>
    </div>
    </div>
    </div>

**JS - chat2.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;
    }
    });

    function get_chat_messages(){
    $.post(base_url +"user/chats/ajax_get_chat_messages",{user: user}, function(data) {
    if(data)
    {
    var current_content = $("#view1").html();  
    $("#view1").html(user);
    console.log(user);   
    }
    else
    {                             
    }, "json");
    }

    get_chat_messages();
    });





   Images attached showing the table structure, the chat page on browser and console data.
    [Chat page on browser, only showing username for testing purposes][1]
    [Chat Table][2]
    [Console Data, only showing username for testing purposes][3]  

PHP是一种需求吗?因为Nodejs和socket.io做得很好…我不熟悉node或socket,所以你应该看看。。。io非常强大,可以在5分钟内将数据从服务器推送到客户端。您可以设计一个基本的聊天室