Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suppose that you store the chat messages in a db?? so one a approach will look like this:</p> <p>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:</p> <pre><code>var time; $.ajax( { url: 'http://yoursite.com/chat/get_time', success: function( dataReponse ) { time = dataResponse; }, type: 'GET' } ); </code></pre> <p>based on the url <code>"http://yoursite.com/chat/get_time"</code>, you need a controller named <code>"chat"</code> with a function with the name of <code>"get_time"</code>, this function need to response the server time in milliseconds, so we do this:</p> <pre><code>function get_time() { echo time(); } </code></pre> <p>now we start to polling to the server new chat messages, we do this:</p> <pre><code>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 } } } ); } </code></pre> <p>comebacj to <code>"chat"</code> controller, we need to code the <code>"get_new_msgs"</code> function:</p> <pre><code>function get_new_msgs() { $this-&gt;load-&gt;model( 'chat_msg' ); echo json_encode( array( 'msgs' =&gt; $this-&gt;chat_msg-&gt;start_polling(), // response again the server time to update the "js time variable" 'time' =&gt; time() ) ); } </code></pre> <p>in <code>"chat_msg"</code> model, we code the <code>"start_polling"</code> function:</p> <pre><code>function start_polling() { // get the time $time = $this-&gt;input-&gt;post( 'time' ); // some crappy validation if( !is_numeric( $time ) ) { return array(); } $time = getdate( $time ); // -&gt; 2010-10-01 $time = $time['year'] '-' + $time['mon'] + '-' + $time['mday']; while( true ) { $this-&gt;db-&gt;select( 'msg' ); $this-&gt;db-&gt;from( 'chat_msg' ); $this-&gt;db-&gt;where( 'time &gt;=', $time ); $this-&gt;db-&gt;order_by( 'posted_time', 'desc' ); $query = $this-&gt;db-&gt;get(); if( $query-&gt;num_rows() &gt; 0 ) { $msgs = array(); foreach( $query-&gt;result() as $row ) { $msgs[] = $row['msg']; } return $msgs; } sleep( 1 ); } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload