Source code

This is the source code of the simplified library AJAX application.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<title>Very simplified library client application</title>
<!-- In the whole application, only two lines must be changed/added from a
     normal AJAX code.  This is the first one:  we include beepbeep.js. -->
<script type="text/javascript" src="beepbeep.js"></script>
<script type="text/javascript">
// Creates and sends an XML mesasge based on the list of books in the form
function createAndSend() {
  // This is the second line to be changed:  we instantiate the wrapper
  // class XMLHttpRequestBEEP instead of XMLHttpRequest or MSXML
  var req = new XMLHttpRequestBEEP();
  // We create the message from the form content
  var booksform = document.getElementById("inputbooks");
  var messageContent = "<message>\n";
  if (booksform.action[0].checked)
    messageContent += "<action>borrow</action>\n";
    messageContent += "<action>return</action>\n";
  bookArray = booksform.booklist.value.split(" ");
  messageContent += "<books>\n";
  for (var bookno in bookArray)
    messageContent += "<book>" + bookArray[bookno] + "</book>\n";
  messageContent += "</books>\n";
  messageContent += "</message>";
  // Posts the message.  For the sake of the example, we just pretend:  the
  // "true" 2nd param of send prevents the message from actually being sent"POST", "", true); 
  req.send(messageContent, true);
  // Remark how nothing in the previous code bothers with the fact we
  // are actually monitoring some form of contract
<h1>Very simplified library client application</h1>
<p>Go back to a <a href="../../tour.php">description</a> of what it does,
see the source code, etc.</p>
<form id="inputbooks" action="#">
<p>Enter a list of book IDs (numbers) separated by spaces.</p>
<input type="text" name="booklist" size="20"/>
<li><input type="radio" name="action" value="enter" checked="checked"/>
  I borrow these books</li>
<li><input type="radio" name="action" value="exit"/>
  I return these books</li>
<input type="button" value="Send the message" onclick="createAndSend()"/>
<p>Below is the monitor applet.  Normally it is invisible and runs in the
background; we display it for the sake of the example.  The display
<i>a/b/c/:d:e</i> indicates:  a/b/c:  state of properties 1/2/3
(? = unknown, T = true, F = false); d: number of messages processed,
e: total processing time (in ms).</p>
<!-- The applet does not need to be added; loading beepbeep.js adds it to
     the HTML at runtime. -->

Main menu Logo