簡體   English   中英

如何從火箭請求中發出 Tauri window 事件

[英]How to emit a Tauri window event from rocket request

我希望這不是太晦澀。

我正在玩 Tauri,我想為應用程序公開一個基於 web 的控制面板。 通過轉到 local.network 上的 url,例如http://192.168.1.101:8000/some-action ,它會向該機器上運行的 Tauri 應用程序發送一條 window 消息。 想象一下辦公室中的儀表板應用程序,網絡上的用戶可以通過 web url 控制該應用程序的行為方式。

到目前為止,這是我的 rust 代碼:

// use rocket runtime
#[rocket::main]
async fn main() {
    tauri::Builder::default()
        .setup(|app| {

            let window = app.get_window("main").unwrap();

            #[get("/")]
            fn index() {
                // this is where I want to emit the window event if possible
                //window.emit("from-rust", format!("message")).expect("failed to emit");
            }
            
            // mount the rocket instance
            tauri::async_runtime::spawn(async move {
                let _rocket = rocket::build()
                    .mount("/", routes![index])
                    .launch().await;
                });
            
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

我能夠運行火箭服務器,但是,我不知道如何從火箭請求處理程序 function 發送 window 事件。

任何建議或見解將不勝感激。

fn項目不允許捕獲其環境。 但是由於#[get(...)]只允許在fn上使用,所以你不能使用它。

不過,您可以自己為自定義結構實現所需的特征。

use async_trait::async_trait;
use rocket::http::Method;
use rocket::route::{Handler, Outcome};
use rocket::{Data, Request, Route};
use tauri::{Manager, generate_context};

#[derive(Clone)]
struct WindowHandler {
    window: tauri::Window,
}

impl WindowHandler {
    fn new(window: tauri::Window) -> Self {
        Self { window }
    }
}

#[async_trait]
impl Handler for WindowHandler {
    async fn handle<'r>(&self, request: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> {
        self.window
            .emit("from-rust", format!("message"))
            .expect("failed to emit");
        todo!()
    }
}
impl From<WindowHandler> for Vec<Route> {
    fn from(value: WindowHandler) -> Self {
        vec![Route::new(Method::Get, "/", value)]
    }
}

#[rocket::main]
async fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let window = app.get_window("main").unwrap();

            let index = WindowHandler::new(window);
            // mount the rocket instance
            tauri::async_runtime::spawn(async move {
                let _rocket = rocket::build().mount("/", index).launch().await;
            });
            Ok(())
        })
        .run(generate_context!())
        .expect("error while running tauri application");
}

或者另一種可能性是使用全局WINDOW: OnceLock<Window>像這樣:

#![feature(once_cell)]
use std::sync::OnceLock;

use rocket::{get, routes};
use tauri::{Manager, generate_context, Window};

static WINDOW: OnceLock<Window> = OnceLock::new();
#[rocket::main]
async fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let window = app.get_window("main").unwrap();

            _ = WINDOW.set(window);

            #[get("/")]
            fn index() {
                WINDOW.get().expect("window is available").emit("from-rust", format!("message")).expect("failed to emit");
            }
            // mount the rocket instance
            tauri::async_runtime::spawn(async move {
                let _rocket = rocket::build().mount("/", routes![index]).launch().await;
            });
            Ok(())
        })
        .run(generate_context!())
        .expect("error while running tauri application");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM