Struct catenis_api_client::notification::WsNotifyChannel
source · pub struct WsNotifyChannel { /* private fields */ }
Expand description
Represents a Catenis WebSocket notification channel.
This is used to receive notifications from the Catenis system.
An instance of this object should be obtained from a CatenisClient
object via its
new_ws_notify_channel
method.
Implementations
sourceimpl WsNotifyChannel
impl WsNotifyChannel
sourcepub fn open<F>(&mut self, notify_event_handler: F) -> Result<JoinHandle<()>>where
F: Fn(WsNotifyChannelEvent) + Send + 'static,
pub fn open<F>(&mut self, notify_event_handler: F) -> Result<JoinHandle<()>>where
F: Fn(WsNotifyChannelEvent) + Send + 'static,
Open the WebSocket notification channel setting up a handler to monitor the activity on that channel.
Note: this is a non-blocking operation. The provided handler function is run on its own thread.
Example
use std::sync::{Arc, Mutex};
use catenis_api_client::{
CatenisClient, ClientOptions, Environment, Result,
api::NotificationEvent,
notification::WsNotifyChannelEvent,
};
let ctn_client = CatenisClient::new_with_options(
Some((
"drc3XdxNtzoucpw9xiRp",
concat!(
"4c1749c8e86f65e0a73e5fb19f2aa9e74a716bc22d7956bf3072b4bc3fbfe2a0",
"d138ad0d4bcfee251e4e5f54d6e92b8fd4eb36958a7aeaeeb51e8d2fcc4552c3"
),
).into()),
&[
ClientOptions::Environment(Environment::Sandbox),
],
)?;
// Instantiate WebSocket notification channel object for New Message Received
// notification event
let notify_channel = Arc::new(Mutex::new(
ctn_client.new_ws_notify_channel(NotificationEvent::NewMsgReceived)
));
let notify_channel_2 = notify_channel.clone();
let notify_thread = notify_channel.lock().unwrap()
// Open WebSocket notification channel and monitor events on it
.open(move |event: WsNotifyChannelEvent| {
let notify_channel = notify_channel_2.lock().unwrap();
match event {
WsNotifyChannelEvent::Error(err) => {
println!("WebSocket notification channel error: {:?}", err);
},
WsNotifyChannelEvent::Open => {
println!("WebSocket notification channel open");
},
WsNotifyChannelEvent::Close(close_info) => {
println!("WebSocket notification channel closed: {:?}", close_info);
},
WsNotifyChannelEvent::Notify(notify_msg) => {
println!("Received notification (new message read): {:?}", notify_msg);
notify_channel.close();
},
}
})?;
Trait Implementations
sourceimpl Clone for WsNotifyChannel
impl Clone for WsNotifyChannel
sourcefn clone(&self) -> WsNotifyChannel
fn clone(&self) -> WsNotifyChannel
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresourceimpl Debug for WsNotifyChannel
impl Debug for WsNotifyChannel
Auto Trait Implementations
impl !RefUnwindSafe for WsNotifyChannel
impl Send for WsNotifyChannel
impl Sync for WsNotifyChannel
impl Unpin for WsNotifyChannel
impl !UnwindSafe for WsNotifyChannel
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more