How to Callback from rust #11302
Answered
by
FabianLars
charlesooo
asked this question in
Q&A
-
i want to host a http server in rust, send post request to frontend, using js code exited to handle request, and send back result to append on response. but i cant find a way to do that. use actix_web::web;
use actix_web::{App, HttpResponse, HttpServer};
use std::sync::OnceLock;
use tauri::Emitter;
use tauri::{Manager, WebviewWindow};
static WINDOW: OnceLock<WebviewWindow> = OnceLock::new();
static CORS_HEADER: (&str, &str) = ("Access-Control-Allow-Origin", "*");
async fn handler(bytes: web::Bytes) -> HttpResponse {
let win = WINDOW.get().unwrap();
win.emit("request", bytes.escape_ascii().to_string())
.unwrap();
// trapped here ...
HttpResponse::Ok()
.append_header(CORS_HEADER)
.body("RETURN RESULT")
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
WINDOW.set(app.get_webview_window("main").unwrap()).unwrap();
tauri::async_runtime::spawn(
HttpServer::new(move || App::new().route("/get_svg_data", web::post().to(handler)))
.bind(("127.0.0.1", 5188))
.expect("Failed to bind address")
.run(),
);
// only include this code on debug builds
#[cfg(debug_assertions)]
{
let window = app.get_webview_window("main").unwrap();
window.open_devtools();
}
Ok(())
})
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_window_state::Builder::new().build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
} |
Beta Was this translation helpful? Give feedback.
Answered by
FabianLars
Oct 17, 2024
Replies: 1 comment 1 reply
-
You could use actix_web::web;
use actix_web::{App, HttpResponse, HttpServer};
use std::sync::OnceLock;
use tauri::Emitter;
use tauri::{Manager, WebviewWindow};
static WINDOW: OnceLock<WebviewWindow> = OnceLock::new();
static CORS_HEADER: (&str, &str) = ("Access-Control-Allow-Origin", "*");
async fn handler(bytes: web::Bytes) -> HttpResponse {
let win = WINDOW.get().unwrap();
let (sender, receiver) = std::sync::mpsc::channel();
win.listen("response", move |event| {
sender.send(event.payload());
});
win.emit("request", bytes.escape_ascii().to_string())
.unwrap();
let response = receiver.recv().unwrap();
HttpResponse::Ok()
.append_header(CORS_HEADER)
.body("RETURN RESULT")
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
WINDOW.set(app.get_webview_window("main").unwrap()).unwrap();
tauri::async_runtime::spawn(
HttpServer::new(move || App::new().route("/get_svg_data", web::post().to(handler)))
.bind(("127.0.0.1", 5188))
.expect("Failed to bind address")
.run(),
);
// only include this code on debug builds
#[cfg(debug_assertions)]
{
let window = app.get_webview_window("main").unwrap();
window.open_devtools();
}
Ok(())
})
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_window_state::Builder::new().build())
.run(tauri::generate_context!())
.expect("error while running tauri application");
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
charlesooo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could
listen()
to another event before your win.emit line and then after the win.emit line block the function to wait for the response via a channel or whatever. It could look something like this, note that i wrote this here so it may not compile but it should give you an idea