aboutsummaryrefslogtreecommitdiff
path: root/client-native-lib/src/signaling.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-09-14 20:11:23 +0200
committermetamuffin <metamuffin@disroot.org>2022-09-14 20:11:23 +0200
commitc752fe962df841b0cb811b09f155568735e7380c (patch)
tree1ee5ea82899a517a3ad841072e7b094836613e9e /client-native-lib/src/signaling.rs
parenta8de400e17bd3eb7892cac5a0bef02b35482e946 (diff)
downloadkeks-meet-c752fe962df841b0cb811b09f155568735e7380c.tar
keks-meet-c752fe962df841b0cb811b09f155568735e7380c.tar.bz2
keks-meet-c752fe962df841b0cb811b09f155568735e7380c.tar.zst
rename crate
Diffstat (limited to 'client-native-lib/src/signaling.rs')
-rw-r--r--client-native-lib/src/signaling.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/client-native-lib/src/signaling.rs b/client-native-lib/src/signaling.rs
new file mode 100644
index 0000000..2ac3edc
--- /dev/null
+++ b/client-native-lib/src/signaling.rs
@@ -0,0 +1,59 @@
+use crate::protocol::ClientboundPacket;
+use crate::{crypto::hash, protocol::ServerboundPacket};
+use futures_util::{SinkExt, StreamExt};
+use log::{debug, info};
+use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
+use tokio_tungstenite::tungstenite::{self, Message};
+
+pub async fn signaling_connect(
+ host: &str,
+ secret: &str,
+) -> (
+ UnboundedSender<ServerboundPacket>,
+ UnboundedReceiver<ClientboundPacket>,
+) {
+ let uri = format!("wss://{host}/signaling/{}", hash(secret));
+ info!("connecting to signaling server at {uri:?}");
+ let (conn, _) = tokio_tungstenite::connect_async(url::Url::parse(&uri).unwrap())
+ .await
+ .unwrap();
+ info!("connection established");
+
+ let (mut tx, rx) = conn.split();
+
+ let (in_tx, in_rx) = unbounded_channel();
+ let (out_tx, mut out_rx) = unbounded_channel();
+
+ tokio::spawn(async {
+ rx.for_each(move |mesg| {
+ info!("packet in");
+ let mesg = mesg.unwrap();
+ match mesg {
+ tungstenite::Message::Text(t) => {
+ let p: ClientboundPacket = serde_json::from_str(t.as_str()).unwrap();
+ debug!("<- {p:?}");
+ in_tx.send(p).unwrap()
+ }
+ tungstenite::Message::Close(_) => {
+ eprintln!("ws closed :(");
+ unreachable!();
+ }
+ _ => (),
+ }
+ Box::pin(async { () })
+ })
+ .await;
+ });
+ tokio::spawn(async move {
+ while let Some(p) = out_rx.recv().await {
+ debug!(" -> {p:?}");
+ tx.send(Message::Text(
+ serde_json::to_string::<ServerboundPacket>(&p).unwrap(),
+ ))
+ .await
+ .unwrap()
+ }
+ });
+
+ (out_tx, in_rx)
+}