Sebastian Becker: Server Push

Beitrag lesen

Hallo, Martin,

wie ist es möglich per Server Push Modus (über CGI (Perl oder noch besser PHP)) eine Textdatei automatisch "neuzuladen" ...

unabhängig von der Frage, ob die Verwendung der Push-Funktion sinnvoll ist, hier mal ein Link zu einem entsprechenden Script ...

http://www.harlequin.ch/technologien/tools/php.php bzw.
http://www.harlequin.ch/technologien/tools/search.ch.phps

Das Sitellite Content Management System arbeitet auch damit (s.u.).

Grüße,

Sebastian

---------- ServerPush.php ----------

<?php
//
// +----------------------------------------------------------------------+
// | Sitellite - Content Management System                                |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001 Simian Systems                                    |
// +----------------------------------------------------------------------+
// | This software is released under the Simian Open Software License.    |
// | Please see the accompanying file OPENLICENSE for licensing details!  |
// |                                                                      |
// | You should have received a copy of the Simian Open Software License  |
// | along with this program; if not, write to Simian Systems,            |
// | 101-314 Broadway, Winnipeg, MB, R3C 0S7, CANADA.  The Simian         |
// | Public License is also available at the following web site           |
// | address: http://www.simian.ca/license.php                          |
// +----------------------------------------------------------------------+
// | Authors: John Luxford lux@simian.ca                                |
// +----------------------------------------------------------------------+
//
// ServerPush is a class that implements Netscape's HTTP server push
// functionality, allowing incremental data to be presented to the
// visitor.
//

/*!

<package name="CGI.ServerPush">

<class name="ServerPush"
   access="public"
   date="2002-08-23 23:19:00"
   version="1.0">

<author name="John Luxford"
    email="lux@simian.ca"
    url="http://www.simian.ca/" />

<summary>ServerPush is a class that implements Netscape's HTTP server push
functionality, allowing incremental data to be presented to the
visitor.

Please note that if you are using this class for progress meter-like
purposes, for the sake of your bandwidth and for your users, do not
send a message for each item being counted, instead wrap something like
this around it:

if ($total > 1000 && $sent % floor ($total / 25) == 0) {
 $spush->send ('...', true);
}</summary>

<example>$spush = new ServerPush ('replace');

// display the message "Hello, how are you today?" one word at a time
// with a two second delay between words.
$spush->rotate (array (
 'Hello,',
 'how',
 'are',
 'you',
 'today?',
), 2);</example> !*/

class ServerPush {
 /*! <property name="key" access="public" type="string">
 <summary>The unique key used to separate pushed data blocks.  This
 value is generated on the fly for you.</summary>
 </property> !*/
 var $key;

/*! <property name="contentType" access="public" type="string">
 <summary>The HTTP content type of the pushed document.  This can
 be either multipart/mixed or multipart/x-mixed-replace, depending
 on what you tell the constructor method.</summary>
 </property> !*/
 var $contentType;

/*! <method name="ServerPush" access="public">
 <summary>Constructor method.  $type can be either 'mixed', or 'replace'.</summary>
 <param name="type" type="string" default="mixed" />
 </method> !*/
 function ServerPush ($type = 'mixed') {
  if ($type == 'mixed') {
   $this->contentType = 'multipart/mixed';
  } else {
   $this->contentType = 'multipart/x-mixed-replace';
  }
  $this->key = md5 (time ());
  ob_implicit_flush ();
  header ('Content-type: ' . $this->contentType . '; boundary=' . $this->key);
 }

/*! <method name="send" access="public">
 <summary>Sends a single block to the visitor.  If $more is false,
 send() calls end() when it is done.  $contentType is the content
 type for this specific block.</summary>
 <param name="data" type="string" />
 <param name="more" type="boolean" default="true" />
 <param name="contentType" type="string" default="text/html" />
 </method> !*/
 function send ($data, $more = true, $contentType = 'text/html') {
  echo "\n--" . $this->key . "\n";
  echo 'Content-type: ' . $contentType . "\n\n";
  echo $data . "\n";
  if (! $more) {
   $this->end ();
  }
 }

/*! <method name="end" access="public">
 <summary>Prints a closing key string, ending the transmission.</summary>
 </method> !*/
 function end () {
  echo "\n--" . $this->key . "--\n";
 }

/*! <method name="rotate" access="public">
 <summary>Sends a list of blocks with a delay of $sleep seconds
 between them.  This is the way web sites used to create basic
 animations back in the early days of the web, even before
 animated GIFs.  $data is an associative array where the keys
 are the content type of the data value.  If no content type
 is specified (ie. the array is not associative), then the
 content type will default to text/html.</summary>
 <param name="data" type="associative array" />
 <param name="sleep" type="integer" default="1" />
 </method> !*/
 function rotate ($data, $sleep = 1) {
  foreach ($data as $contentType => $d) {
   if (is_numeric ($contentType)) {
    $contentType = 'text/html';
   }
   $this->send ($d, true, $contentType);
   sleep ($sleep);
  }
  $this->end ();
 }
}

/*! </class>

</package> !*/

?>