Create space for multiple routes

This commit is contained in:
Tangent Wantwight 2020-06-09 23:48:53 -04:00
parent ea2770105a
commit 4391ad521d
2 changed files with 18 additions and 16 deletions

View File

@ -18,8 +18,9 @@ import {} from "./net/LoopbackServer";
*/ */
Select(".GameCanvas").forEachCanvas((c, cx, keys) => { Select(".GameCanvas").forEachCanvas((c, cx, keys) => {
const connection = new Connection<KeyName[], Data>("ws://localhost:9090/"); const connection = new Connection<KeyName[], Data>("ws://localhost:9090/base2020.ws");
new Main(c, cx, keys, connection.socket); new Main(c, cx, keys, connection.socket);
c.focus();
}); });
BindTests(); BindTests();

View File

@ -12,7 +12,7 @@ use serde_json::{from_str, to_string};
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use stream::FuturesUnordered; use stream::FuturesUnordered;
use structopt::StructOpt; use structopt::StructOpt;
use warp::{serve, ws, ws::Ws, Filter}; use warp::{path, serve, ws, ws::Ws, Filter};
use ws::{Message, WebSocket}; use ws::{Message, WebSocket};
pub mod net; pub mod net;
@ -46,13 +46,16 @@ async fn main() -> Result<()> {
}) })
}); });
// assemble routes
let routes = path!("base2020.ws").and(socket_handler);
let addrs = args let addrs = args
.listen .listen
.to_socket_addrs() .to_socket_addrs()
.context("Couldn't parse the listen address")?; .context("Couldn't parse the listen address")?;
let servers = FuturesUnordered::new(); let servers = FuturesUnordered::new();
for addr in addrs { 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); servers.push(server);
} }
@ -72,19 +75,17 @@ async fn handle_socket(game_server: Handle, websocket: WebSocket) -> Result<()>
) )
}); });
let mut source = source let mut source = source.map_err(Into::into).try_filter_map(|msg| {
.map_err(Into::into) ready(match msg.to_str() {
.try_filter_map(|msg| { Ok(json) => from_str::<ClientMessage>(json)
ready(match msg.to_str() { .context("Parsing JSON")
Ok(json) => from_str::<ClientMessage>(json) .map(Some),
.context("Parsing JSON") Err(()) => {
.map(Some), debug!("Non-text message {:?}", &msg);
Err(()) => { Ok(None)
debug!("Non-text message {:?}", &msg); }
Ok(None) })
} });
})
});
run_client(game_server, &mut source, &mut sink).await run_client(game_server, &mut source, &mut sink).await
} }