fucking sse, how do they work

This commit is contained in:
zoe 2022-02-05 20:40:42 +01:00
parent ab7785ed00
commit dce8bc5d92
7 changed files with 101 additions and 20 deletions

39
Cargo.lock generated
View File

@ -152,9 +152,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.0.13"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08799f92c961c7a1cf0cc398a9073da99e21ce388b46372c37f3191f2f3eed3e"
checksum = "b63edc3f163b3c71ec8aa23f9bd6070f77edbf3d1d198b164afa90ff00e4ec62"
dependencies = [
"atty",
"bitflags",
@ -169,9 +169,9 @@ dependencies = [
[[package]]
name = "clap_derive"
version = "3.0.12"
version = "3.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fd2078197a22f338bd4fbf7d6387eb6f0d6a3c69e6cbc09f5c93e97321fd92a"
checksum = "9a1132dc3944b31c20dd8b906b3a9f0a5d0243e092d59171414969657ac6aa85"
dependencies = [
"heck",
"proc-macro-error",
@ -673,15 +673,15 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.115"
version = "0.2.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a8d982fa7a96a000f6ec4cfe966de9703eccde29750df2bb8949da91b0e818d"
checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
[[package]]
name = "lock_api"
version = "0.4.5"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109"
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
dependencies = [
"scopeguard",
]
@ -1693,9 +1693,9 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6"
[[package]]
name = "tracing"
version = "0.1.29"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105"
checksum = "2d8d93354fe2a8e50d5953f5ae2e47a3fc2ef03292e7ea46e3cc38f549525fb9"
dependencies = [
"cfg-if 1.0.0",
"pin-project-lite",
@ -1705,9 +1705,9 @@ dependencies = [
[[package]]
name = "tracing-attributes"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e"
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
dependencies = [
"proc-macro2",
"quote",
@ -1716,11 +1716,12 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.21"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4"
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
dependencies = [
"lazy_static",
"valuable",
]
[[package]]
@ -1736,9 +1737,9 @@ dependencies = [
[[package]]
name = "tracing-subscriber"
version = "0.3.7"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5312f325fe3588e277415f5a6cca1f4ccad0f248c4cd5a4bd33032d7286abc22"
checksum = "74786ce43333fcf51efe947aed9718fbe46d5c7328ec3f1029e818083966d9aa"
dependencies = [
"ansi_term",
"lazy_static",
@ -1795,6 +1796,12 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "version_check"
version = "0.9.4"

View File

@ -5,7 +5,12 @@ extern crate lazy_static;
use clap::Parser;
use rocket::fs::{relative, FileServer};
use rocket::{State, Shutdown};
use rocket::serde::{Serialize, Deserialize};
use rocket_dyn_templates::Template;
use rocket::form::Form;
use rocket::tokio::sync::broadcast::{channel, Sender, error::RecvError};
use rocket::response::stream::{EventStream, Event, TextStream};
use std::{collections::HashMap, mem::drop, sync::Mutex};
mod names;
@ -17,6 +22,16 @@ lazy_static! {
static ref ROOMS: Mutex<HashMap<String, rooms::Room>> = Mutex::new(HashMap::new());
}
#[derive(Debug, Clone, FromForm, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, UriDisplayQuery))]
#[serde(crate = "rocket::serde")]
struct Play {
pub roomname: String,
pub position: String,
pub player1: bool,
}
/*
* Config options
*/
@ -39,7 +54,7 @@ fn rocket() -> _ {
rocket::custom(config)
.attach(Template::fairing())
.mount("/", FileServer::from(relative!("static")))
.mount("/", routes![index, room])
.mount("/", routes![index, room, stream, post])
}
/*
@ -94,7 +109,7 @@ async fn room(roomname: &str, token: usize, player1: bool) -> Template {
if room.token == token {
drop(rooms);
//TODO: placeholder
templates::get_back_to_home("cool!")
templates::get_room(roomname, token, player1)
}
// the room is real but the token is incorrect
else {
@ -108,3 +123,21 @@ async fn room(roomname: &str, token: usize, player1: bool) -> Template {
templates::get_back_to_home("illegalroom")
}
}
#[get("/stream")]
async fn stream(queue: &State<Sender<Play>>, mut end: Shutdown) -> TextStream![String]{
let mut rx = queue.subscribe();
TextStream! {
loop {
yield "hello!".to_string();
}
}
}
/// Receive a message from a form submission and broadcast it to any receivers.
#[post("/message", data = "<form>")]
fn post(form: Form<Play>, queue: &State<Sender<Play>>) {
// A send 'fails' if there are no active subscribers. That's okay.
let _res = queue.send(form.into_inner());
}

View File

@ -4,6 +4,7 @@ use rand::{thread_rng, Rng};
pub struct Room {
pub token: usize,
pub full: bool,
pub play_queue: Vec<String>,
}
impl Room {
@ -14,6 +15,7 @@ impl Room {
Room {
full: false,
token: tok,
play_queue: vec![],
}
}
}

View File

@ -29,6 +29,16 @@ pub fn join_room(roomname: &str, token: usize) -> Template {
join_template_from(&message, "let me in!", &link_url, "hidden", roomname)
}
pub fn get_room(roomname: &str, token: usize, player1: bool) -> Template{
let mut context: HashMap<&str, &str> = HashMap::new();
let tokstring = token.to_string();
let p1string = player1.to_string();
context.insert("roomname", roomname);
context.insert("token", &tokstring);
context.insert("player1", &p1string);
Template::render("room", context)
}
fn join_template_from(message: &str, link: &str, link_url: &str, hide_url_button: &str, roomname: &str) -> Template {
let url_button_url = &format!("{}/?roomname={}", ARGS.base_url, roomname);
let mut context: HashMap<&str, &str> = HashMap::new();

1
static/room.js Normal file
View File

@ -0,0 +1 @@

View File

@ -1,15 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
<script src="/script.js" charset="utf-8" defer></script>
<script src="/join.js" charset="utf-8" defer></script>
<title>Reversi Online</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1"
/>
<link rel="icon" type="image/x-icon" href="./static/think.svg" />
<link rel="icon" type="image/x-icon" href="./static/favicon.svg" />
</head>
</html>
<body>

27
templates/room.html.hbs Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
<script src="/room.js" charset="utf-8" defer></script>
<title>Reversi Online</title>
<meta
charset="utf-8"
name="viewport"
content="width=device-width, initial-scale=1"
/>
<link rel="icon" type="image/x-icon" href="./static/favicon.svg" />
</head>
</html>
<body>
<div class="hidden">
<input id="player1" value="{{player1}}">
<input id="roomname" value="{{roomname}}">
<input id="token" value="{{token}}">
</div>
<div class="contentdiv">
</div>
<footer>
<span id="status">reversi</span>
</footer>
</body>