简体   繁体   中英

Tauri: write to child std in inside window callback event

I am new to rust and am having some trouble writing to std in of a spawned process on the result of an event on the window using the tauri framework .

I am able to write to stdin of the process / sidecar outside of the call back for the event for the window, but it does not work inside the callback. It's as if the command just gets lost / doesnt work.

I am receiving events, as confirmed by the println statement.

My code is below:

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);
      }
    }
  });
}

Thanks for your help.

So i figured out what was wrong with the code. I had forgotten / didn't realise I needed to add in a new line character to the end of the bytes which were written in the stdin input, so it never registered the input. The following codes works.

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);
          }
        }
      });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM