Start receive message

This commit is contained in:
xmppftw xmppftw 2024-06-03 17:37:51 +02:00
parent ba276d9b00
commit 130d6bebfc
2 changed files with 44 additions and 14 deletions

View File

@ -52,7 +52,7 @@ fn main() {
let tabs_store = gio::ListStore::new::<Tab>();
let xmpp_receiver = xmpp_client::client(&username, &password);
let (xmpp_receiver, cmd_sender) = xmpp_client::client(&username, &password);
let tabs_store_copy = tabs_store.clone();
glib::spawn_future_local(async move {
while let Ok(event) = xmpp_receiver.recv().await {
@ -155,10 +155,22 @@ fn main() {
});
let win2 = win.clone();
let cmd_sender2 = cmd_sender.clone();
win.entry().connect_activate(move |entry| {
let text = entry.text();
entry.set_text("");
println!("Send: {text}");
let current_tab: Tab = win2
.tabs_selection()
.selected_item()
.and_downcast()
.unwrap();
cmd_sender2
.send_blocking(xmpp_client::XMPPCommand::SendPM(
xmpp::BareJid::new(&current_tab.jid()).unwrap(),
text.as_str().to_string(),
))
.unwrap();
let message = Message::now(&get_own_nick(), &text);
let selection = win2.selection();
let store = selection

View File

@ -1,16 +1,22 @@
use async_channel::Receiver;
use async_channel::{Receiver, Sender};
use tokio::runtime::Runtime;
use xmpp::{BareJid, ClientBuilder, ClientFeature, Event};
use std::sync::OnceLock;
#[derive(Clone, Debug)]
pub enum XMPPCommand {
SendPM(BareJid, String),
}
fn tokio_runtime() -> &'static Runtime {
static RUNTIME: OnceLock<Runtime> = OnceLock::new();
RUNTIME.get_or_init(|| Runtime::new().expect("Setting up tokio runtime needs to succeed."))
}
pub(crate) fn client(jid: &str, password: &str) -> Receiver<xmpp::Event> {
pub(crate) fn client(jid: &str, password: &str) -> (Receiver<xmpp::Event>, Sender<XMPPCommand>) {
let (event_sender, event_receiver) = async_channel::bounded::<xmpp::Event>(1);
let (cmd_sender, cmd_receiver) = async_channel::bounded::<XMPPCommand>(1);
let jid = jid.to_string();
let password = password.to_string();
@ -21,22 +27,34 @@ pub(crate) fn client(jid: &str, password: &str) -> Receiver<xmpp::Event> {
.enable_feature(ClientFeature::ContactList)
.build();
while let Some(events) = client.wait_for_events().await {
for event in events {
match event {
Event::ContactAdded(_) => {
event_sender.send(event).await.expect("BOOOOOOHOOOO");
loop {
tokio::select! {
Some(events) = client.wait_for_events() => {
for event in events {
match event {
Event::ContactAdded(_) => {
event_sender.send(event).await.expect("BOOOOOOHOOOO");
}
Event::ChatMessage(_, _, _, _) => {
event_sender.send(event).await.expect("BOOOHOOOO");
}
_ => {
continue;
}
}
}
Event::ChatMessage(_, _, _, _) => {
event_sender.send(event).await.expect("BOOOHOOOO");
}
_ => {
continue;
}
command = cmd_receiver.recv() => {
match command.unwrap() {
XMPPCommand::SendPM(jid, content) => {
client.send_message(jid.into(), xmpp::parsers::message::MessageType::Chat, "en", &content).await;
},
}
}
}
}
});
event_receiver
(event_receiver, cmd_sender)
}