mirror of
https://github.com/nekename/OpenDeck.git
synced 2026-10-28 18:02:23 +00:00
Process incoming events
This commit is contained in:
1 parent
e523a30aac
commit
8697389ed7
3 files changed
+39
-7
No files matched your search
@@ -4,3 +4,8 @@ use serde::Deserialize;
|
||||
pub struct RegisterEvent {
|
||||
pub uuid: String
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct OpenUrlEvent {
|
||||
pub url: String
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
pub mod misc;
|
||||
|
||||
use serde::Deserialize;
|
||||
use tauri::{api::shell::open, Manager};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "event", content = "payload")]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum InboundEvent {
|
||||
OpenUrl(misc::OpenUrlEvent)
|
||||
}
|
||||
|
||||
async fn open_url(event: misc::OpenUrlEvent) -> Result<(), anyhow::Error> {
|
||||
let app_handle = crate::APP_HANDLE.lock().await;
|
||||
open(&app_handle.as_ref().unwrap().shell_scope(), event.url, None)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn process_incoming_message(data: tokio_tungstenite::tungstenite::Message) -> Result<(), tokio_tungstenite::tungstenite::Error> {
|
||||
if let tokio_tungstenite::tungstenite::Message::Text(text) = data {
|
||||
let decoded: InboundEvent = match serde_json::from_str(&text) {
|
||||
Ok(event) => event,
|
||||
Err(_) => return Ok(())
|
||||
};
|
||||
|
||||
let _ = match decoded {
|
||||
InboundEvent::OpenUrl(event) => open_url(event).await
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -16,13 +16,8 @@ lazy_static! {
|
||||
}
|
||||
|
||||
/// Register a plugin to send and receive events with its WebSocket.
|
||||
pub async fn register_plugin(data: inbound::RegisterEvent, stream: WebSocketStream<TcpStream>) {
|
||||
pub async fn register_plugin(data: inbound::misc::RegisterEvent, stream: WebSocketStream<TcpStream>) {
|
||||
let (read, write) = stream.split();
|
||||
tokio::spawn(write.try_for_each(|value| {
|
||||
if let Message::Text(text) = value {
|
||||
println!("{}", text);
|
||||
}
|
||||
futures_util::future::ready(Ok(()))
|
||||
}));
|
||||
tokio::spawn(write.try_for_each(inbound::process_incoming_message));
|
||||
SOCKETS.lock().await.insert(data.uuid.clone(), read);
|
||||
}
|
||||
Reference in new issue
Block a user