[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/examples/ -> postoffice_client_generated.php (source)

   1  <?php
   2  // $Id: postoffice_client_generated.php,v 1.2 2004/11/16 09:43:14 harryf Exp $
   3  header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
   4  header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
   5  header( "Cache-Control: no-cache, must-revalidate" ); 
   6  header( "Pragma: no-cache" );
   7  
   8  function path() {
   9      $basePath = explode('/',$_SERVER['SCRIPT_NAME']);
  10      $script = array_pop($basePath);
  11      $basePath = implode('/',$basePath);
  12      if ( isset($_SERVER['HTTPS']) ) {
  13          $scheme = 'https';
  14      } else {
  15          $scheme = 'http';
  16      }
  17      echo $scheme.'://'.$_SERVER['SERVER_NAME'].$basePath;
  18  }
  19  
  20  ?>
  21  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  22  <html>
  23  <head>
  24  <title> PostOffice Server Demo with generated client </title>
  25  <script type="text/javascript" src="<?php path(); ?>/postoffice_server.php?client"></script>
  26  <script type="text/javascript">
  27  
  28  // Simple example use - synchronous call
  29  function add() {
  30      // The math client object
  31      var m = new math();
  32      
  33      var x = document.getElementById("x").value;
  34      var y = document.getElementById("y").value;
  35      
  36      // Call the remote procedure...
  37      var result = m.add(x,y);
  38  
  39      // Display the result
  40      if ( result ) {
  41          echo(result);
  42      }
  43  }
  44  
  45  // Taking some short cuts - synchronous call
  46  function subtract() {
  47      var m = new math();
  48      var result = m.subtract(document.getElementById("x").value,document.getElementById("y").value);
  49  
  50      // This should be more cunning...
  51      if ( result || result === 0 ) {
  52          echo(result);
  53      }
  54  }
  55  
  56  // Define a custom error function for synchronous calls
  57  // (by default errors will be echoed)
  58  function divide() {
  59      var m = new math();
  60      
  61      // Define a custom error function for application errors
  62      m.applicationErrorFunc = function(e) {
  63          echo('Division by zero: infinity');
  64      }
  65      
  66      var result = m.divide(document.getElementById("x").value,document.getElementById("y").value);
  67      if ( result ) {
  68          echo(result);
  69      }
  70  }
  71  
  72  // Callback handler for asynchronous calls to remote math object
  73  // Note the naming of functions here...
  74  var MathHandler = {
  75  
  76      // Called with result of math.add() method
  77      add: function(result) {
  78          echo(result);
  79      },
  80      
  81      /*
  82      // Optionally define error handling function
  83      // if not defined, math.applicationErrorFunc is called instead
  84      addError: function(e) {
  85          alert(e.message);
  86      },
  87      */
  88      
  89      // Called with result of math.subtract() method
  90      subtract: function(result) {
  91          echo(result);
  92      },
  93      
  94      // Called with result of math.divide() method
  95      divide: function(result) {
  96          echo(result);
  97      },
  98      
  99      // Called on error with math.divide() method
 100      divideError: function(e) {
 101          alert(e.message);
 102      }
 103  }
 104  
 105  function addAsync() {
 106  
 107      // Create the math object, passing it the callback handler
 108      // This automatically switches to async mode
 109      var m = new math(MathHandler);
 110      
 111      // Set request timeout as required
 112      // m.timeout = 1000;
 113      
 114      var x = document.getElementById("x").value;
 115      var y = document.getElementById("y").value;
 116      
 117      // Simply call the method and continue
 118      m.add(x,y);
 119  }
 120  
 121  function subtractAsync() {
 122  
 123      // Create the math object (starts in sync mode)
 124      var m = new math();
 125      
 126      // The alternative approach to switch to async mode
 127      m.Async(MathHandler);
 128      
 129      m.subtract(document.getElementById("x").value,document.getElementById("y").value);
 130      
 131  }
 132  
 133  function divideAsync() {
 134  
 135      var m = new math(MathHandler);
 136      m.divide(document.getElementById("x").value,document.getElementById("y").value);
 137  
 138  }
 139  
 140  // Now use the colors client... (sync)
 141  function listColors() {
 142  
 143      var c = new colors();
 144      var result = c.listcolors();
 145  
 146      if ( result ) {
 147          clear();
 148          echo ('<h2>A Short List of Colors</h2>',true);
 149          for (var i=0;i<result.length;i++) {
 150              echo ('<br>',true);
 151              for (prop in result[i]) {
 152                  if ( prop == 'toPHP' || prop == 'var_dump' ) {
 153                      continue;
 154                  }
 155                  echo (result[i][prop]+' : ',true);
 156              }
 157          } 
 158      }
 159      
 160  }
 161  
 162  // Handler for asynchronous calls to colors
 163  var ColorsHandler = {
 164      listcolors: function(result) {
 165          clear();
 166          echo ('<h2>A Short List of Colors</h2>',true);
 167          for (var i=0;i<result.length;i++) {
 168              echo ('<br>',true);
 169              for (prop in result[i]) {
 170                  if ( prop == 'toPHP' || prop == 'var_dump' ) {
 171                      continue;
 172                  }
 173                  echo (result[i][prop]+' : ',true);
 174              }
 175          }
 176      },
 177      listcolorsError: function(e) {
 178          alert(e.message);
 179      }
 180  }
 181  
 182  // Synchronous call to colors.listcolors()...
 183  function listColorsAsync() {
 184      var c = new colors();
 185      c.Async(ColorsHandler);
 186      c.listcolors();
 187  }
 188  
 189  function echo(string) {
 190      if ( !arguments[1] ) {
 191          clear();
 192      }
 193      document.getElementById("results").innerHTML += string;
 194  }
 195  
 196  function clear() {
 197      document.getElementById("results").innerHTML = "";
 198  }
 199  
 200  -->
 201  </script>
 202  </head>
 203  <body>
 204  <h1> PostOffice Server Demo with generated client </h1>
 205  <p> Server provided by JPSpan_Server_PostOffice and Javascript client is also generated.</p>
 206  <form id="mathForm">
 207  X: <input id="x" type="text" value="2" size="2"><br>
 208  Y: <input id="y" type="text" value="2" size="2"><br>
 209  <input type="button" onClick="add()" value="Add [Sync]"> : <input type="button" onClick="addAsync()" value="Add [Async]"><br>
 210  <input type="button" onClick="subtract()" value="Subtract [Sync]"> : <input type="button" onClick="subtractAsync()" value="Subtract [Async]"><br>
 211  <input type="button" onClick="divide()" value="Divide [Sync]"> : <input type="button" onClick="divideAsync()" value="Divide [Async]"> (see what happens if you set Y to zero)<br>
 212  </form>
 213  <p>
 214  <a href="javascript:listColors()">colors.listcolors</a> [sync] : <a href="javascript:listColorsAsync()">colors.listcolors</a> [async]
 215  </p>
 216  <h2>Response from Server</h2>
 217  <div id="results">
 218  </div>
 219  </body>
 220  </html>


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6