public function onIdle(SocketServer $server) { $clients = $server->getClients(); foreach ($clients as $client) { if ($client->buffer) { $client->send(array('type' => 'shell', 'result' => $client->buffer)); $client->buffer = ""; } if ($client->process && !$client->process->isRunning() && !$client->buffer) { $client->process = false; $client->send(array('type' => 'shell', 'finished' => true)); } } usleep(1000 * 10); }
function real_time_js_and_php( ) { ?> <!-- tut 1 --> <script type="text/javascript"> setInterval(function(){ // inside here, set any data that you need to send to the server var some_serialized_data = jQuery('form.my_form').serialize(); // then fire off an ajax call jQuery.ajax({ url: '/yourPhpScriptForUpdatingData.php', success: function(response){ // put some javascript here to do something with the // data that is returned with a successful ajax response, // as available in the 'response' param available, // inside this function, for example: $('#my_html_element').html(response); }, data: some_serialized_data }); }, 1000); // the '1000' above is the number of milliseconds to wait before running // the callback again: thus this script will call your server and update // the page every second. </script> <!-- tut 2 --> <script type="text/javascript"> jQuery(function($){ setInterval(function(){ $.get( '/getrows.php', function(newRowCount){ $('#rowcounter').html( newRowCount ); }); },5000); // 5000ms == 5 seconds }); </script> <!-- tut 3 js.node--> <!-- src: http://coenraets.org/blog/2012/10/real-time-web-analytics-with-node-js-and-socket-io/ --> <!-- HELPFUL src: http://www.dreamincode.net/forums/topic/319049-creating-real-time-web-updates-with-ajax-php-sql-etc/ AJAX PUSH Socket connections sockets. node.js database check sockets push --> <?php // Assume this SocketServer is dealing with incomming // Socket connections and making them available as // "Client" objects through: getClients(). $server = new SocketServer(); while ($server->isRunning()) { // Go through each of the clients currently connected // to the server. foreach ($server->getClients() as $client) { // If the client has sent a new message to the server // receive it and push it down to all the other clients. // Then save it in the database. if ($client->hasNewData()) { $message = $client->receive(); foreach ($server->getClients() as $recipient) { $recipient->send($message); } DB::get()->saveMessage($message); } } // Sleep for a few milliseconds, just so the process doesn't // freeze up the server or eat all it's resources. usleep(150000); } ?> <!-- NOT HELPFUL tut 4 http://www.webdesignerforum.co.uk/topic/65897-php-mysql-real-time-chat-application-without-javascript/ usleep(250000); isset --> <!-- tut 5 sockets push http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/patterns/pushpull.html http://socketo.me/docs/push --> <?php }