tauri to manage and kill ws-server
This commit is contained in:
1 parent
126a3749f9
commit
3e3d2dcbcb
5 files changed
+64
-20
No files matched your search
+55
-13
@@ -1,16 +1,58 @@
|
||||
use tauri::Manager;
|
||||
|
||||
use std::{process::{Child, Command}, sync::Mutex};
|
||||
|
||||
struct State {
|
||||
child: Mutex<Option<Child>>,
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
tauri_plugin_log::Builder::default()
|
||||
.level(log::LevelFilter::Info)
|
||||
.build(),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
tauri::Builder::default()
|
||||
.manage(State {
|
||||
child: Mutex::new(None),
|
||||
})
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
tauri_plugin_log::Builder::default()
|
||||
.level(log::LevelFilter::Info)
|
||||
.build(),
|
||||
)?;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
use std::{env, process::Stdio};
|
||||
|
||||
let cwd = env::current_dir()?;
|
||||
let p = cwd.join("..").join("server.js");
|
||||
println!("{:?}", p);
|
||||
|
||||
let child = Command::new("node")
|
||||
.arg(p)
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()
|
||||
.expect("spawn ws-server");
|
||||
|
||||
let state = app.handle().state::<State>();
|
||||
*state.child.lock().unwrap() = Some(child);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
.run(|app_handle, event| match event {
|
||||
// On exit handler should be avoided for mobile
|
||||
#[cfg(target_os = "windows")]
|
||||
tauri::RunEvent::ExitRequested { .. } => {
|
||||
if let Some(mut child) = app_handle.state::<State>().child.lock().unwrap().take() {
|
||||
let _ = child.kill().expect("ws-server not killed");
|
||||
}
|
||||
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user