繁体   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