draxu26480 2012-10-18 00:06
浏览 45

jquery / ajax long-polling php mysql codeigniter [关闭]

TO THE PEOPLE WHO VOTED THIS NOT A REAL QUESTION THAT IS ABSOLUTELY IDIOTIC.

I have searched and searched with Google over a month now and I can't find an accurate example that actually works of ajax long polling with php and mysql. Preferably would like to find an example ajax long polling with mysql and codeigniter.

Anyone comes across this question and has a good example please let me know.

Anyone who reads this that thinks they know the ajax long polling I'm looking for please email me or let me know on here. I have chat app almost done utilizing Codeigniter but I am having trouble with jquery/ajax long polling part that is client side. I have not posted this chat app because last time I posted another chat app I have another developer on here complained there was too much code I posted. I am ready to send this code to anyone who can actually provide ajax long polling code that works.

Thanks so much.

  • 写回答

1条回答 默认 最新

  • duanjiao5543 2012-10-18 01:45
    关注

    I suppose that you store the chat messages in a db?? so one a approach will look like this:

    the most important thing is that you need to do at the very first time is deliver to the user the server time, this is the key to get the new chat messages, so first, we do this:

    var time;
    $.ajax( {
         url: 'http://yoursite.com/chat/get_time',
         success: function( dataReponse ) {
             time = dataResponse;
        },
        type: 'GET'
    } );
    

    based on the url "http://yoursite.com/chat/get_time", you need a controller named "chat" with a function with the name of "get_time", this function need to response the server time in milliseconds, so we do this:

    function get_time() {
        echo time();
    }
    

    now we start to polling to the server new chat messages, we do this:

    function getNewMsgs() {
        $.ajax( {
            url: 'http://yoursite.com/chat/get_new_msgs',
            type: 'POST',
            // send the time
            data: { time: time },
            success: function( dataResponse ) {
                try {
                    dataResponse = JSON.parse( dataResponse );
                    // update the time
                    time = dataResponse.time;
                    // show the new messages
                    dataResponse.msgs.forEach( function( msg ) {
                        console.log( msg );
                    } );
                            // repeat
                            setTimeout( function() {
                                 getNewMsgs();
                            }, 1000 );
                } catch( e ) {
                    // may fail is the connection is lost/timeout for example, so dataResponse
                    // is not a valid json string, in this situation you can start this process again
                }
            }
        } );
    }
    

    comebacj to "chat" controller, we need to code the "get_new_msgs" function:

    function get_new_msgs() {
    
        $this->load->model( 'chat_msg' );
        echo json_encode( array(
            'msgs' => $this->chat_msg->start_polling(),
            // response again the server time to update the "js time variable"
            'time' => time() 
        ) );
    }
    

    in "chat_msg" model, we code the "start_polling" function:

    function start_polling() {
        // get the time
        $time = $this->input->post( 'time' );
        // some crappy validation
        if( !is_numeric( $time ) ) {
            return array();
        }
    
        $time = getdate( $time );
        // -> 2010-10-01
        $time = $time['year'] '-' + $time['mon'] + '-' + $time['mday'];
    
        while( true ) {
    
            $this->db->select( 'msg' );
            $this->db->from( 'chat_msg' );
            $this->db->where( 'time >=', $time );
            $this->db->order_by( 'posted_time', 'desc' );
            $query = $this->db->get();
    
            if( $query->num_rows() > 0 ) {
                $msgs = array();
                foreach( $query->result() as $row ) {
                    $msgs[] = $row['msg'];
                }
                return $msgs;
            }
    
            sleep( 1 );
        }
    }
    

    get warning, I wrote this code in my mind, I do not have a web server to my disposition at this moment to test this code.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退