[英]Tauri: write to child std in inside window callback event
我是 rust 的新手,並且在使用tauri 框架將窗口上的事件結果寫入生成的進程的 std 時遇到了一些麻煩。
我能夠在窗口事件的回調之外寫入進程/邊車的標准輸入,但它在回調內不起作用。 就好像命令丟失了/不起作用。
我正在接收事件,正如 println 語句所確認的那樣。
我的代碼如下:
pub fn window_manager(win: Window<Wry>) {
let (mut rx, mut child) = Command::new_sidecar("window_manager")
.expect("failed to create `window_manager`")
.spawn()
.expect("Failed to spawn sidecar");
let child = Arc::new(Mutex::new(child));
child.lock().unwrap().write(b"********").unwrap(); // This writes to std in here...
win.listen("event-name", move |event| {
// let child = Arc::new(Mutex::new(child));
// child.lock().unwrap().write(b"********").unwrap(); // This does not write to std in here...
println!("WINDOW EVENT RECEIVED {:?}", event.payload());
});
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
println!("STDOUT from sidecar....: {}", line);
}
}
});
}
謝謝你的幫助。
所以我想出了代碼有什么問題。 我忘記了/沒有意識到我需要在 stdin 輸入中寫入的字節末尾添加一個新行字符,因此它從未注冊過輸入。 以下代碼有效。
pub fn window_manager(win: Window<Wry>) {
let (mut rx, mut child) = Command::new_sidecar("window_manager")
.expect("failed to create `window_manager`")
.spawn()
.expect("Failed to spawn sidecar");
let child = Arc::new(Mutex::new(child));
main_window.listen("event-name", move |event| {
child
.lock()
.unwrap()
.write("oh my lol \n".as_bytes())
.unwrap(); // This does write to std in here...
println!("WINDOW EVENT RECEIVED {:?}", event.payload());
});
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
println!("STDOUT from sidecar....: {}", line);
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.