changeset 669:f68eee90dbcf

Press a button to send a message
author Adam Chlipala <adamc@hcoop.net>
date Sun, 22 Mar 2009 15:16:34 -0400
parents b0c1a46b1f15
children f73913d97a40
files lib/js/urweb.js src/c/urweb.c tests/channel.ur
diffstat 3 files changed, 25 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Mar 22 15:05:07 2009 -0400
+++ b/lib/js/urweb.js	Sun Mar 22 15:16:34 2009 -0400
@@ -115,9 +115,13 @@
     throw "Can't parse float: " + s;
 }
 
+function whine(msg) {
+  alert(msg);
+  throw msg;
+}
+
 function pf() {
-  alert("Pattern match failure");
-  throw "Pattern match failure";
+  whine("Pattern match failure");
 }
 
 var closures = [];
@@ -180,8 +184,7 @@
       if (isok)
         k(parse(xhr.responseText));
       else {
-        alert("Error querying remote server!");
-        throw "Error querying remote server!";
+        whine("Error querying remote server!");
       }
     }
   };
@@ -200,8 +203,7 @@
 function listener() {
   var uri = path_join(url_prefix, ".msgs");
   var xhr = getXHR();
-
-  xhr.onreadystatechange = function() {
+  var orsc = function() {
     if (xhr.readyState == 4) {
       var isok = false;
 
@@ -211,15 +213,23 @@
       } catch (e) { }
 
       if (isok) {
-        alert("Messages: " + xhr.responseText);
+        var lines = xhr.responseText.split("\n");
+        if (lines.length < 2) 
+          whine("Empty message from remote server");
+
+        for (var i = 0; i+1 < lines.length; i += 2) {
+          alert("Message(" + lines[i] + "): " + lines[i+1]);
+        }
+
+        xhr.onreadystatechange = orsc;
         requestUri(xhr, uri);
       }
       else {
-        alert("Error querying remote server for messages!");
-        throw "Error querying remote server for messages!";
+        whine("Error querying remote server for messages!");
       }
     }
   };
 
+  xhr.onreadystatechange = orsc;
   requestUri(xhr, uri);
 }
--- a/src/c/urweb.c	Sun Mar 22 15:05:07 2009 -0400
+++ b/src/c/urweb.c	Sun Mar 22 15:16:34 2009 -0400
@@ -342,6 +342,7 @@
     pthread_mutex_lock(&c->data.used.lock);
 
     if (c->data.used.sock != -1) {
+      printf("Immediate send\n");
       uw_really_send(c->data.used.sock, begin_msgs, sizeof(begin_msgs) - 1);
       uw_really_send(c->data.used.sock, pre, preLen);
       uw_really_send(c->data.used.sock, msg, len);
@@ -349,6 +350,7 @@
       close(c->data.used.sock);
       c->data.used.sock = -1;
     } else {
+      printf("Delayed send\n");
       buf_append(&c->data.used.msgs, pre, preLen);
       buf_append(&c->data.used.msgs, msg, len);
       buf_append(&c->data.used.msgs, "\n", 1);
--- a/tests/channel.ur	Sun Mar 22 15:05:07 2009 -0400
+++ b/tests/channel.ur	Sun Mar 22 15:16:34 2009 -0400
@@ -4,8 +4,11 @@
         fun onload () =
             subscribe ch;
             send ch "Hello world!"
+
+        fun haveAnother () =
+            send ch "Here's another."
     in
         return <xml><body onload={onload ()}>
-
+          <button value="Another?" onclick={haveAnother ()}/>
         </body></xml>
     end