CompleteEcho.htm

<!DOCTYPE html>
<html>

<head>
 <meta charset="utf-8" />
 <title>HLL Command Center</title>
 <meta name="description"  content="HTML5 Websockets for the HLL high level logic open source project - Tutorial" />
 <meta name="keywords"     content="high level logic, hll, open source, artificial intelligence, agents" />
 <meta name="author"       content="Roger F. Gay" />
 <meta name="owner"        content="rogerfgay@yahoo.com" />
 <meta name="copyright"    content="%a9 2011 Roger F. Gay" />
 <meta name="license"      content="Open source, use as you like; please acknowledge source." />
 <meta name="website"      content="http://highlevellogic.blogspot.se/" />
 <meta name="created"      content="20110801" />
 <meta name="changed"      content="20130930" />
 <meta name="generator"    content="skill, knowledge, and intelligence" />

<style type="text/css">
</style>

<script type="text/javascript">

  var wsUri = "ws://isr.servequake.com/echo";
  var output;
  var websocket;
  var connected=false;

  function init() {
    output = document.getElementById("output");
    doConnect();
  }
  function doConnect() {

   if (connected) {
     writeToScreen("<span style='color: red;'>You're already connected!</span>");
   } else if (typeof MozWebSocket != "undefined") {
     websocket = new MozWebSocket(wsUri);
   } else if (window.WebSocket) {
     websocket = new WebSocket(wsUri);
   } else {
     writeToScreen('<strong style="color: red;">ERROR: This browser does not support WebSockets.</strong>');
     return;
   }
   websocket.onopen = function(evt) { onOpen(evt) };
   websocket.onclose = function(evt) { onClose(evt) };
   websocket.onmessage = function(evt) { onMessage(evt) };
   websocket.onerror = function(evt) { onError(evt) };
  }
  function onOpen(evt) {
    connected = true;
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks!");
  }
  function onClose(evt) {
    writeToScreen("DISCONNECTED");
  }
  function onMessage(evt) {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
  }
  function onError(evt) {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
  }
  function doSend(message) {
    writeToScreen("SENT: " + message);
    websocket.send(message);
  }
  function writeToScreen(message) {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
  }
  window.onload = init;
</script>
</head>
<body>
<h2>WebSocket Test</h2>
<div id="output"></div>
</body>
</html>

No comments:

Post a Comment