diff options
author | metamuffin <metamuffin@disroot.org> | 2022-09-08 13:31:18 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-09-08 13:31:18 +0200 |
commit | cd255848196da0d8732d31049cc0f98388205a30 (patch) | |
tree | 3813003cc153210d24a7819c0dce8d68b26867a2 | |
parent | 88241946e3144fede5c86f98d00bb723c1cc2761 (diff) | |
download | keks-meet-cd255848196da0d8732d31049cc0f98388205a30.tar keks-meet-cd255848196da0d8732d31049cc0f98388205a30.tar.bz2 keks-meet-cd255848196da0d8732d31049cc0f98388205a30.tar.zst |
more code
-rw-r--r-- | client-web/source/room.ts | 6 | ||||
-rw-r--r-- | client-web/watch-builder.ts | 36 | ||||
-rw-r--r-- | common/packets.d.ts | 1 | ||||
-rw-r--r-- | server/src/main.rs | 35 | ||||
-rw-r--r-- | server/src/protocol.rs | 1 | ||||
-rw-r--r-- | server/src/room.rs | 1 |
6 files changed, 63 insertions, 17 deletions
diff --git a/client-web/source/room.ts b/client-web/source/room.ts index 3ed7ddf..af85044 100644 --- a/client-web/source/room.ts +++ b/client-web/source/room.ts @@ -4,7 +4,6 @@ import { log } from "./logger.ts"; import { RemoteUser } from "./remote_user.ts"; import { User } from "./user.ts"; import { LocalUser } from "./local_user.ts"; -import { hex_id, parameter_string } from "./helper.ts"; import { ServerboundPacket, ClientboundPacket } from "../../common/packets.d.ts"; @@ -20,7 +19,7 @@ export class Room { this.name = name this.el = document.createElement("div") this.el.classList.add("room") - this.websocket = new WebSocket(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.host}/signaling/${encodeURIComponent(name)}`) + this.websocket = new WebSocket(`${window.location.protocol.endsWith("s:") ? "wss" : "ws"}://${window.location.host}/${encodeURIComponent(name)}/signaling`) this.websocket.onclose = () => this.websocket_close() this.websocket.onopen = () => this.websocket_open() this.websocket.onmessage = (ev) => { @@ -70,7 +69,6 @@ export class Room { } websocket_open() { log("ws", "websocket opened"); - this.websocket.send(this.local_user.name) - setInterval(() => this.websocket_send({}), 30000) // stupid workaround for nginx disconnection inactive connections + setInterval(() => this.websocket_send({ ping: null }), 30000) // stupid workaround for nginx disconnecting inactive connections } }
\ No newline at end of file diff --git a/client-web/watch-builder.ts b/client-web/watch-builder.ts new file mode 100644 index 0000000..05871e1 --- /dev/null +++ b/client-web/watch-builder.ts @@ -0,0 +1,36 @@ + +// `emit` uses fetch to *download* was, that is just stupid +// import { bundle } from "https://deno.land/x/emit@0.1.1/mod.ts"; + +// instead, lets run `deno bundle` manually + +async function bundle(entry: string, options: { compilerOptions: { checkJs: boolean } }) { + const proc = Deno.run({ cmd: ["deno", "bundle", options.compilerOptions.checkJs ? "--check" : "--no-check", "--unstable", entry], stdout: "piped" }) + const out = await proc.output() + const code = new TextDecoder().decode(out) + return { code } +} + +let refresh_needed = false +let refresh_pending = false +async function refresh() { + refresh_needed = true + if (refresh_pending) return + refresh_needed = false + refresh_pending = true + + try { + const { code } = await bundle("source/index.ts", { compilerOptions: { checkJs: false } }) + await Deno.writeTextFile("public/assets/bundle.js", code) + } catch (e) { console.error(e) } + + refresh_pending = false + if (refresh_needed) refresh() +} + +refresh() +for await (const event of Deno.watchFs("source")) { + if (event.kind == "modify" || event.kind == "create") { + refresh() + } +} diff --git a/common/packets.d.ts b/common/packets.d.ts index abfb7dc..8ade352 100644 --- a/common/packets.d.ts +++ b/common/packets.d.ts @@ -12,6 +12,7 @@ export interface ClientboundPacket { } export interface ServerboundPacket { + ping: null, relay?: { recipient?: number, message: RelayMessage }, } diff --git a/server/src/main.rs b/server/src/main.rs index b4121b9..6a8f11d 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -12,29 +12,38 @@ use warp::hyper::Server; use warp::ws::WebSocket; use warp::{Filter, Rejection, Reply}; -type Rooms = Arc<CHashMap<String, Room>>; +type Rooms = Arc<CHashMap<String, Arc<Room>>>; -#[tokio::main] -async fn main() { +fn main() { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(run()); +} + +async fn run() { env_logger::init_from_env("LOG"); let rooms = Rooms::default(); let rooms = warp::any().map(move || rooms.clone()); - let signaling = warp::path("signaling") - .and(warp::path::param::<String>()) + let app = warp::path!(String) + .map(|_| ()) + .untuple_one() + .and(warp::fs::file("../client-web/public/app.html")); + let signaling = warp::path!(String / "signaling") .and(rooms) .and(warp::ws()) .map(signaling_connect); - let static_routes = { - let index = warp::path::end().and(warp::fs::file("../client-web/public/start.html")); - let assets = warp::path("_assets").and(warp::fs::dir("../client-web/public/assets")); - - warp::get().and(index.or(assets)) - }; + let index = warp::path!().and(warp::fs::file("../client-web/public/start.html")); + let assets = warp::path("_assets").and(warp::fs::dir("../client-web/public/assets")); - let routes = static_routes.or(signaling).recover(handle_rejection); + let routes = warp::get() + .and(assets.or(app).or(index).or(signaling)) + .recover(handle_rejection) + .with(warp::log("stuff")); // if listender fd is passed from the outside world, use it. let mut listenfd = ListenFd::from_env(); @@ -76,7 +85,7 @@ fn signaling_connect(rname: String, rooms: Rooms, ws: warp::ws::Ws) -> impl Repl let room = match rooms.get(&rname) { Some(r) => r, None => { - rooms.insert(rname.to_owned(), Room::default()); + rooms.insert(rname.to_owned(), Default::default()); rooms.get(&rname).unwrap() // TODO never expect this to always work!! } }; diff --git a/server/src/protocol.rs b/server/src/protocol.rs index 780ae4a..f480ce7 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -23,6 +23,7 @@ pub enum ClientboundPacket { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ServerboundPacket { + Ping, Relay { recipient: Option<usize>, message: RelayMessage, diff --git a/server/src/room.rs b/server/src/room.rs index 43cfa90..a47d2e5 100644 --- a/server/src/room.rs +++ b/server/src/room.rs @@ -86,6 +86,7 @@ impl Room { pub async fn client_message(&self, sender: usize, packet: ServerboundPacket) { match packet { + ServerboundPacket::Ping => (), ServerboundPacket::Relay { recipient, message } => { if let Some(recipient) = recipient { self.send_to_client(recipient, ClientboundPacket::Message { sender, message }) |