half baked bollocks

This commit is contained in:
Sam W 2019-02-18 18:29:53 +00:00
parent fdf781ff02
commit 19479263b8
2 changed files with 15 additions and 8 deletions

View File

@ -43,6 +43,7 @@ impl<T: Adaptor, U: Driver> Server<T, U> {
let rx = self.adaptor.run(); let rx = self.adaptor.run();
let tx = self.driver.run(); let tx = self.driver.run();
let webaddr = ([127, 0, 0, 1], 3000).into(); let webaddr = ([127, 0, 0, 1], 3000).into();
//let (websocket_tx, websocket_rx) =
log::info!("Webserver starting on: {}", webaddr); log::info!("Webserver starting on: {}", webaddr);
let web = WebServer::run(webaddr); let web = WebServer::run(webaddr);
loop { loop {

View File

@ -1,5 +1,7 @@
use crate::TallyState;
use futures::stream; use futures::stream;
use futures::sync::oneshot::{channel, Sender}; use futures::sync::mpsc::Receiver;
use futures::sync::oneshot;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
use typed_html::dom::DOMTree; use typed_html::dom::DOMTree;
@ -22,12 +24,12 @@ fn build_template() -> DOMTree<String> {
} }
pub struct WebServer { pub struct WebServer {
shutdown_chan: Sender<()>, shutdown_chan: oneshot::Sender<()>,
shutdown_mutex: Arc<Mutex<()>>, shutdown_mutex: Arc<Mutex<()>>,
} }
impl WebServer { impl WebServer {
pub fn run(addr: std::net::SocketAddr) -> WebServer { pub fn run(addr: std::net::SocketAddr, tally_rx: Receiver<TallyState>) -> WebServer {
// todo: not have the damn js inline in the func // todo: not have the damn js inline in the func
const JS: &'static str = r#" const JS: &'static str = r#"
var sock = new WebSocket("ws://" + window.location.host + "/ws"); var sock = new WebSocket("ws://" + window.location.host + "/ws");
@ -37,7 +39,7 @@ sock.onmessage = function (e) {
sock.onopen = function (e) { sock.onopen = function (e) {
sock.send("Hello!"); sock.send("Hello!");
}"#; }"#;
let (shutdown_tx, shutdown_rx) = channel::<()>(); let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
let shutdown_mutex = Arc::new(Mutex::new(())); let shutdown_mutex = Arc::new(Mutex::new(()));
let thread_shutdown_mutex = Arc::clone(&shutdown_mutex); let thread_shutdown_mutex = Arc::clone(&shutdown_mutex);
@ -48,9 +50,13 @@ sock.onopen = function (e) {
let websocket = warp::path("ws").and(warp::ws2()).map(|ws: warp::ws::Ws2| { let websocket = warp::path("ws").and(warp::ws2()).map(|ws: warp::ws::Ws2| {
ws.on_upgrade(|websocket| { ws.on_upgrade(|websocket| {
let (tx, rx) = websocket.split(); let (tx, _) = websocket.split();
//stream::once::<Message, _>(Ok(Message::text("Hello"))) //stream::once::<Message, _>(Ok(Message::text("Hello")))
rx.forward(tx).map(|_| ()).map_err(|e| { tally_rx
.map(|state| Message::text(format!("{}", state)))
.forward(tx)
.map(|_| ())
.map_err(|e| {
log::error!("Websocket error: {}", e); log::error!("Websocket error: {}", e);
}) })
}) })