#How can I create an http server and then shut it down?
34 messages · Page 1 of 1 (latest)
@uneven elk hi thanks I know that library but the thing I asked is not that
I want to create this server when I wanted to
and want to close it with a comamnd after
I will invoke invoke('server:open') command, this will open the server and invoke('server:close') will close it, but I couldn't make it work
Tried using tauri:state, mutex, arc everything
but couldn't succeed
I struggled a lot and ended up here
this method could let you achieve this
or for other method, you could look about this:
https://github.com/BillGoldenWater/ChaosDanmuTool/blob/dev/src-tauri/src/network/http_server.rs#L76-L89
@uneven elk hey, is your server living between the tauri app closing and opening again?
what you mean closing and opening again?
so I have a tauri app the3s.
the3s starts a server
the3s terminates
the3s launches again
is the server still on?
it's running only when the app process is running
is there a way to "spawn" a powershell command that will outlive the the3s?
the3s?
the tauri app
do you need hide the powershell window?
no need
start powershell { echo "the command you want to run" }
but on this, I am usings command with powershell and a -command and with the msi installed the powershell is flashing each time I use the command in tauri app, is there a way to silently run the command without the powershell window flashing?
if you need outlive the app, then https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/start-job
The Start-Job cmdlet starts a PowerShell background job on the local computer. A PowerShell background job runs a command without interacting with the current session. When you start a background job, a job object returns immediately, even if the job takes an extended time to finish. You can continue to work in the session without interruption w...
@barren falconoh sry for hijacking your thread but it was related... hope you dont mind
@uneven elk Is there a way to move the offtopic replies to its own thread?
idk
@uneven elk thanks for the code example, The problem is not about tauri it's about my rust knowledge, I'm closing this thread.
,@uneven elk sorry for bothering again, I read your code and came up with this but aborting join handle does not stop the server.
server.rs
pub struct AuthServer {
server_join_handle: Option<tauri::async_runtime::JoinHandle<()>>,
}
impl AuthServer {
pub fn new() -> AuthServer {
AuthServer {
server_join_handle: None,
}
}
pub fn listen(&mut self, window: Window) {
self.server_join_handle = Some(tauri::async_runtime::spawn(async move {
let server = Server::http("0.0.0.0:23846").unwrap();
for request in server.incoming_requests() {
handle_code_request(request, &window);
}
}));
}
pub fn stop(&mut self) {
if let Some(join_handle) = self.server_join_handle.take() {
join_handle.abort();
}
}
}
#[tauri::command]
pub fn start_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
let mut server = state.lock().unwrap();
server.listen(window);
}
#[tauri::command]
pub fn stop_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
let mut server = state.lock().unwrap();
server.stop();
}
What should I do to close the server 
The server is running in an infinite loop of requests, I don't know where to put that sender
it's using futures::future::select.
@uneven elk hi again, can you be a little clear? I read futures docs and tried to the thing you said but I'm getting error
I am not experienced rust developer sorry
umm Idk this too.
maybe you could use a loop with recv_timeout
then check the receiver
anyway thanks for help, I gave up. I'll keep the server open all the time