From 11c99ec387bc8b5c5d1a6ca03901ef0fc1b9ef7c Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 27 May 2017 18:01:01 +0200 Subject: [PATCH] add register_handler to Client --- examples/client.rs | 15 +++++---------- src/client.rs | 9 ++++++++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/client.rs b/examples/client.rs index de8fa51..b6a1ee0 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -5,6 +5,7 @@ use xmpp::client::ClientBuilder; use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent}; use xmpp::plugins::presence::{PresencePlugin, Show}; use xmpp::plugins::ping::{PingPlugin, PingEvent}; +use xmpp::event::{Priority, Propagation}; use std::env; @@ -18,16 +19,10 @@ fn main() { client.register_plugin(MessagingPlugin::new()); client.register_plugin(PresencePlugin::new()); client.register_plugin(PingPlugin::new()); + client.register_handler(Priority::Max, |e: &MessageEvent| { + println!("{:?}", e); + Propagation::Continue + }); client.plugin::().set_presence(Show::Available, None).unwrap(); client.main().unwrap(); - /*loop { - let event = client.next_event().unwrap(); - if let Some(evt) = event.downcast::() { - println!("{:?}", evt); - } - else if let Some(evt) = event.downcast::() { - println!("{:?}", evt); - client.plugin::().reply_ping(evt); - } - }*/ } diff --git a/src/client.rs b/src/client.rs index 1ce9e21..918ca88 100644 --- a/src/client.rs +++ b/src/client.rs @@ -11,7 +11,7 @@ use sasl::common::{Credentials as SaslCredentials, Identity, Secret, ChannelBind use sasl::common::scram::{Sha1, Sha256}; use components::sasl_error::SaslError; use util::FromElement; -use event::{Dispatcher, Propagation, SendElement, ReceiveElement, Priority}; +use event::{Event, Dispatcher, Propagation, SendElement, ReceiveElement, Priority}; use base64; @@ -128,6 +128,13 @@ impl Client { } } + pub fn register_handler(&mut self, pri: Priority, func: F) + where + E: Event, + F: Fn(&E) -> Propagation + 'static { + self.dispatcher.lock().unwrap().register(pri, func); + } + /// Returns the plugin given by the type parameter, if it exists, else panics. pub fn plugin(&self) -> &P { self.plugins.get(&TypeId::of::

())