lib.rs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // 模块引用
  2. use std::{
  3. thread::{sleep, spawn},
  4. time::Duration,
  5. };
  6. use tauri::{Emitter, Window};
  7. // 生成新函数
  8. #[tauri::command]
  9. fn greet(name: &str) -> String {
  10. format!("Hello, {}! You've been greeted from Rust!", name)
  11. }
  12. // 使用derive 生成新的数据结构
  13. #[derive(Clone, serde::Serialize)]
  14. struct Payload {
  15. message: String,
  16. }
  17. // 向窗口发送消息
  18. #[tauri::command]
  19. fn init_process(window: Window) {
  20. spawn(move || loop {
  21. window
  22. .emit(
  23. "event",
  24. Payload {
  25. message: "你好".into(),
  26. },
  27. )
  28. .unwrap();
  29. sleep(Duration::from_millis(1000));
  30. });
  31. }
  32. // 条件编译属性cfg 的属性attr
  33. #[cfg_attr(mobile, tauri::mobile_entry_point)]
  34. pub fn run() {
  35. tauri::Builder::default()
  36. .plugin(tauri_plugin_websocket::init())
  37. .plugin(tauri_plugin_shell::init())
  38. .invoke_handler(tauri::generate_handler![greet, init_process])
  39. .run(tauri::generate_context!())
  40. .expect("error while running tauri application");
  41. }