chiark / gitweb /
clean up sse
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 11:50:38 +0000 (12:50 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 5 Jul 2020 11:50:38 +0000 (12:50 +0100)
src/imports.rs
src/sse.rs
templates/script.js

index 263002f59eab4891ce8399a7f11a04216404ea97..0d63a6eee951086b8ec93998d1db5bb75dac9681 100644 (file)
@@ -13,6 +13,7 @@ pub use std::str::FromStr;
 pub use std::iter;
 pub use std::iter::repeat_with;
 pub use std::collections::VecDeque;
+pub use std::num::Wrapping;
 
 pub use thiserror::Error;
 pub use anyhow::{Context,anyhow};
index d258fe0f1e32cdb137ea96427988124ceb6e4c94..f99fc6017977a1249648757eb39474e14ef47ecd 100644 (file)
@@ -41,6 +41,7 @@ struct UpdateReader {
   client : ClientId,
   need_flush : bool,
   init_confirmation_send : iter::Once<()>,
+  keepalives : Wrapping<u32>,
   to_send : UpdateId,
   ami : Arc<Mutex<Instance>>,
 }
@@ -66,11 +67,9 @@ impl Read for UpdateReader {
     let mut buf = orig_buf.as_mut();
 
     if self.init_confirmation_send.next().is_some() {
-      write!(buf, r#"
-data: server online
-
-"#)?;
-/*event: commsworking*/
+      write!(buf, "event: commsworking\n\
+                   data: server online {} {} G{}\n\n",
+             self.player, self.client, self.to_send)?;
     }
 
     let pu = &mut amig.updates.get(self.player)
@@ -86,21 +85,16 @@ data: server online
       if next_len > buf.len() { break }
 
       if next.client == self.client {
-        write!(buf, r#"
-event: recorded
-data: "#)?;
+        write!(buf, "event: recorded\n\
+                     data: foo\n\n")?;
         serde_json::to_writer(&mut *buf, &RecordedConfirmation {
           gen : next.gen,
           piece : next.piece,
           cseq : next.client_seq,
         })?;
-        write!(buf, r#"
-"#)?;
       } else {
-        write!(buf, r#"
-id: {}
-data: {}
-"#,
+        write!(buf, "id: {}\n\
+                     data: {}\n\n",
                &self.to_send,
                &next.json)?;
       }
@@ -109,7 +103,7 @@ data: {}
     loop {
       let generated = orig_wanted - buf.len();
       if generated > 0 {
-        eprintln!("SENDING {} to {:?} {:?}:\n{}\n",
+        eprintln!("SENDING {} to {:?} {:?}: {:?}",
                   generated, &self.player, &self.client,
                   str::from_utf8(&orig_buf[0..generated]).unwrap());
         self.need_flush = true;
@@ -124,9 +118,13 @@ data: {}
 
       amig = cv.wait_timeout(amig, UPDATE_KEEPALIVE)
         .map_err(|_| em("poison"))?.0;
-      write!(buf,r#"
-: keepalive
-"#)?;
+
+      write!(buf, "event: commsworking\n\
+                   data: server online {} {} G{} K{}\n\n",
+             self.player, self.client, self.to_send, self.keepalives)?;
+      self.keepalives += Wrapping(1);
+/*
+      write!(buf,": keepalive\n\n")?; */
     }
   }
 }
@@ -198,7 +196,7 @@ struct APIForm {
  */
 
 #[derive(Debug)]
-struct DebugReader<T : Read>(T);
+pub struct DebugReader<T : Read>(pub T);
 
 impl<T : Read> Read for DebugReader<T> {
   fn read(&mut self, buf: &mut [u8]) -> Result<usize,io::Error> {
@@ -236,9 +234,11 @@ eprintln!("updates content iad={:?} player={:?} cl={:?} updates={:?}",
     UpdateReader {
       player, client, to_send, ami,
       need_flush : false,
+      keepalives : Wrapping(0),
       init_confirmation_send : iter::once(()),
     }
   };
   let content = BufReader::with_capacity(UPDATE_READER_SIZE, content);
-  DebugReader(content)
+  //DebugReader(content)
+  content
 }
index 588760f3061974a082687018ab5e30685da602cc..5d7732126f4aabcd906966d615cbd83d9768268c 100644 (file)
@@ -197,12 +197,13 @@ function startup() {
     var k = Object.keys(j)[0];
     messages[k](j[k]);
   }
-  es.oncommsworking = function(event) {
-    status_node.innerHTML = data.value;
-  }
-  es.onrecorded = function(event) {
+  es.addEventListener('commsworking', function(event) {
+    console.log('GOTDATA');
+    status_node.innerHTML = event.data;
+  });
+  es.addEventListener('recorded', function(event) {
     xxx_recorded();
-  }
+  });
   es.onerror = function(e) {
     console.log('FOO',e,es);
     json_report_error({