tauri to manage and kill ws-server

This commit is contained in:
polv committed 2026-06-25 15:18:23 +07:00
1 parent 126a3749f9
commit 3e3d2dcbcb
5 files changed
+64 -20

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
fn main() {
tauri_build::build()
tauri_build::build()
}
+55 -13
View File
@@ -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");
}
}
_ => {}
});
}
+1 -1
View File
@@ -2,5 +2,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
app_lib::run();
app_lib::run();
}
+3 -2
View File
@@ -13,9 +13,10 @@
"windows": [
{
"title": "yt2press",
"width": 800,
"height": 600,
"width": 1024,
"height": 768,
"resizable": true,
"maximized": true,
"fullscreen": false
}
],