Created
April 17, 2022 20:54
-
-
Save TheBestJohn/fedb449a78c09242676c9fca151c14f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::{Deserialize, Serialize}; | |
use tauri::{ | |
plugin::{Builder, TauriPlugin}, | |
AppHandle, Manager, Runtime, State, | |
}; | |
use std::time::Duration; | |
#[cfg(feature = "client")] | |
use rumqttc::{self, AsyncClient, MqttOptions, QoS, EventLoop}; | |
#[cfg(feature = "client")] | |
struct Client { | |
name: String, | |
client: AsyncClient, | |
loop_handle: Option<tauri::async_runtime::JoinHandle> | |
} | |
impl Client { | |
async fn spawn(name:String, server:String) -> Result<Client, Error> { | |
let mut mqttoptions = MqttOptions::new(&name, &server, 1883); | |
mqttoptions.set_keep_alive(Duration::from_secs(5)); | |
let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); | |
let loop_handle = Some(tauri::async_runtime::spawn(async move { | |
loop { | |
let notification = eventloop.poll().await.unwrap(); | |
//figure out channels or something to call callback | |
println!("{:?}", notification); | |
} | |
})); | |
// client.clone().publish("testTopic", QoS::ExactlyOnce, false, "Testing".as_bytes()).await.unwrap(); | |
Ok(Client{name, client, loop_handle}) | |
} | |
} | |
#[tauri::command] | |
async fn new_client() { | |
let client = Client::spawn(String::from("rumqtt-tauri-async"),String::from("test.mosquitto.org")); | |
} | |
/// Initializes the plugin. | |
pub fn init<R: Runtime>() -> TauriPlugin<R> { | |
Builder::new("mqtt") | |
.invoke_handler(tauri::generate_handler![new_client]) | |
.setup(|app_handle| { | |
// setup plugin specific state here | |
app_handle.manage(MyState::default()); | |
Ok(()) | |
}) | |
.build() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment