Page 1 of 1

push and pull

Posted: Wed May 11, 2005 8:00 am
by thomas777neo
Hi All

Could someone please show me a simple example of how to push data from a server '192.168.0.x' to a client machine '192.168.0.y'.

So that even while the client is busy working, another window could popup, or it could interupt the client and go to a specific url etc

I have tried this site:
http://jpspan.sourceforge.net/wiki/doku ... ppets:push

I created a client.php with this code:

Code: Select all

<!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.0 Transitional//EN&quote;>
<html>
    <head>
        <title> Server Push with XmlHttpRequest </title>
    </head>
    <script type=&quote;text/javascript&quote;>
    <!--
    var xmlhttp = new XMLHttpRequest();
    // Tell XmlHttpRequest to expect a multipart response
    xmlhttp.multipart = true;
    
    function acceptResponse(event) {
        // event.target is the instance of XmlHttpRequest
        document.getElementById('result').innerHTML = event.target.responseText
    }

    function start() {
        // Modify the URL to point at your server
        xmlhttp.open(&quote;GET&quote;,&quote;http://192.168.0.x/server.php&quote;,true);
        xmlhttp.onload = acceptResponse;
        xmlhttp.send(null);
    }
    
    function stop() {
        xmlhttp.abort()
    }
    -->
    </script>
    <body>
    <h1>XmlHttpRequest Server Push Example</h1>
    <p><a href=&quote;javascript:start()&quote;>Start</a> : <a href=&quote;javascript:stop()&quote;>Stop</a>
    <br>
    Server Clock: <span id=&quote;result&quote;></span>
    </body>
</html>
and a server.php page:

Code: Select all

require_once 'class.serverPush.php';

$SP = & new HTTP_ServerPush();
$SP->open();

while ( true ) {
    $SP->push(date('H:i:s'),'text/plain');
    sleep(1);
}
and here is the class included (class.serverPush.php):

Code: Select all

/**
* Class to implement server push connections with multipart/x-mixed-replace
* Note this is only supported by Mozilla (
* @see http://wp.netscape.com/assist/net_sites/pushpull.html
* @see http://www.xulplanet.com/tutorials/mozs ... erpush.php
*/
class HTTP_ServerPush {

    /**
    * Tracks whether the shutdown is handled by PHP or by the class's user
    * @var boolean
    * @access private
    */
    var $shutdown;
    
    /**
    * Begin the server push session
    * @var int (default = 0 [infinite]) time limit for PHP script execution
    * @var boolean (default TRUE) whether PHP should handle shutdown (and sending closing content)
    * @return void
    * @access public
    */
    function open($time_limit=0, $shutdown = TRUE) {
    
        $this->shutdown = $shutdown;
        
        $contentType = "multipart/x-mixed-replace; boundary={$GLOBALS['_HTTP_SERVERPUSH_BOUNDARY']}";
        
        header ("Content-type: $contentType");
        
        ob_implicit_flush ();
        
        set_time_limit($time_limit);
        
        if ( $shutdown ) {
            register_shutdown_function('HTTP_ServerPush_Shutdown');
        }
    }
    
    /**
    * Push some content to replace the previously send content
    * @var string content (e.g. a web page)
    * @var string content type (mime)
    * @return void
    * @access public
    */
    function push($content,$contentType = 'text/html') {
        echo "--{$GLOBALS['_HTTP_SERVERPUSH_BOUNDARY']}\r\n";
        echo "Content-type: $contentType\r\n\r\n";
        echo $content."\r\n";
    }

    /**
    * Close the HTTP server push session - you need to call this if you explicitly disable
    * shutdown behaviour when calling open
    * @return void
    * @access public
    */
    function close() {
        if ( !$this->shutdown ) {
            HTTP_ServerPush_Shutdown();
        }
    }

}
Using Mozilla on both the client and server. I run the server.php on the server. Then on the client side, I run the client.php.

I then click on the start hyperlink, which is supposed to get the server time pushed to it.

I don't know if I'm going about this in the right way. Also, if anyone knows it there is a non-javascript server push code somewhere.

Posted: Wed May 25, 2005 7:52 pm
by infolock
I think if you read this tutoral by Onlamp on XMLHttpRequest that you may find the answer.