Create stub server in Rust using Warp to handle websockets.

This commit is contained in:
Tangent Wantwight 2020-05-10 20:06:20 -04:00
parent 5c9552fe49
commit d031061c41
4 changed files with 1445 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.cache/
node_modules/
dist/
target/

1393
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "base2020-server"
version = "0.1.0"
authors = ["tangent128 <tangent128@gmail.com>"]
edition = "2018"
[dependencies]
anyhow = "1"
futures = "0.3"
structopt = "0.3"
serde = "1"
tokio = {version = "0.2", features = ["macros"]}
warp = "0.2"

38
src/main.rs Normal file
View File

@ -0,0 +1,38 @@
use anyhow::Result;
use futures::stream::{FuturesUnordered, StreamExt};
use std::net::ToSocketAddrs;
use structopt::StructOpt;
use warp::{serve, ws, ws::Ws, Filter};
#[derive(StructOpt)]
/// Server for base2020 lockstep protocol for multiplayer games.
struct Args {
/// The socket address to listen for connections on;
/// can be a hostname to bind to multiple hosts at once,
/// such as to listen on both IPv4 & IPv6.
listen: String,
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::from_args();
let socket_handler = ws().map(|upgrade: Ws| {
upgrade.on_upgrade(|websocket| {
async move {
drop(websocket);
}
})
});
let addrs = args.listen.to_socket_addrs()?;
let servers = FuturesUnordered::new();
for addr in addrs {
let (_, server) = serve(socket_handler).try_bind_ephemeral(addr)?;
servers.push(server);
}
servers.for_each(|_| async {}).await;
Ok(())
}