From 4391ad521d67ae82aa0df0d24a235cdb6becdd5f Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Tue, 9 Jun 2020 23:48:53 -0400 Subject: [PATCH] Create space for multiple routes --- src/index.ts | 3 ++- src/main.rs | 31 ++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index 22eff75..7675f96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,8 +18,9 @@ import {} from "./net/LoopbackServer"; */ Select(".GameCanvas").forEachCanvas((c, cx, keys) => { - const connection = new Connection("ws://localhost:9090/"); + const connection = new Connection("ws://localhost:9090/base2020.ws"); new Main(c, cx, keys, connection.socket); + c.focus(); }); BindTests(); diff --git a/src/main.rs b/src/main.rs index 96d3d53..0f97999 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use serde_json::{from_str, to_string}; use std::net::ToSocketAddrs; use stream::FuturesUnordered; use structopt::StructOpt; -use warp::{serve, ws, ws::Ws, Filter}; +use warp::{path, serve, ws, ws::Ws, Filter}; use ws::{Message, WebSocket}; pub mod net; @@ -46,13 +46,16 @@ async fn main() -> Result<()> { }) }); + // assemble routes + let routes = path!("base2020.ws").and(socket_handler); + let addrs = args .listen .to_socket_addrs() .context("Couldn't parse the listen address")?; let servers = FuturesUnordered::new(); for addr in addrs { - let (_, server) = serve(socket_handler.clone()).try_bind_ephemeral(addr)?; + let (_, server) = serve(routes.clone()).try_bind_ephemeral(addr)?; servers.push(server); } @@ -72,19 +75,17 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()> ) }); - let mut source = source - .map_err(Into::into) - .try_filter_map(|msg| { - ready(match msg.to_str() { - Ok(json) => from_str::(json) - .context("Parsing JSON") - .map(Some), - Err(()) => { - debug!("Non-text message {:?}", &msg); - Ok(None) - } - }) - }); + let mut source = source.map_err(Into::into).try_filter_map(|msg| { + ready(match msg.to_str() { + Ok(json) => from_str::(json) + .context("Parsing JSON") + .map(Some), + Err(()) => { + debug!("Non-text message {:?}", &msg); + Ok(None) + } + }) + }); run_client(game_server, &mut source, &mut sink).await }