| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- // 模块引用
- use std::{
- thread::{sleep, spawn},
- time::Duration,
- };
- use tauri::{Emitter, Window};
- // 生成新函数
- #[tauri::command]
- fn greet(name: &str) -> String {
- format!("Hello, {}! You've been greeted from Rust!", name)
- }
- // 使用derive 生成新的数据结构
- #[derive(Clone, serde::Serialize)]
- struct Payload {
- message: String,
- }
- // 向窗口发送消息
- #[tauri::command]
- fn init_process(window: Window) {
- spawn(move || loop {
- window
- .emit(
- "event",
- Payload {
- message: "你好".into(),
- },
- )
- .unwrap();
- sleep(Duration::from_millis(1000));
- });
- }
- // 条件编译属性cfg 的属性attr
- #[cfg_attr(mobile, tauri::mobile_entry_point)]
- pub fn run() {
- tauri::Builder::default()
- .plugin(tauri_plugin_websocket::init())
- .plugin(tauri_plugin_shell::init())
- .invoke_handler(tauri::generate_handler![greet, init_process])
- .run(tauri::generate_context!())
- .expect("error while running tauri application");
- }
|