Struct holochain_websocket::ListenerHandle
source · pub struct ListenerHandle { /* private fields */ }
Expand description
Handle for shutting down a listener stream.
Example
use futures::stream::StreamExt;
use holochain_websocket::*;
use url2::url2;
#[tokio::main]
async fn main() {
let (listener_handle, mut listener_stream) = WebsocketListener::bind_with_handle(
url2!("ws://127.0.0.1:12345"),
std::sync::Arc::new(WebsocketConfig::default()),
)
.await
.unwrap();
tokio::spawn(async move { while let Some(Ok(_)) = listener_stream.next().await {} });
listener_handle.close();
}
Implementations§
source§impl ListenerHandle
impl ListenerHandle
sourcepub fn local_addr(&self) -> &Url2
pub fn local_addr(&self) -> &Url2
Get the url of the bound local listening socket.
sourcepub fn get_config(&self) -> Arc<WebsocketConfig>
pub fn get_config(&self) -> Arc<WebsocketConfig>
Get the config associated with this listener.
sourcepub async fn close_on<F>(self, f: F)where
F: Future<Output = bool>,
pub async fn close_on<F>(self, f: F)where
F: Future<Output = bool>,
Close the listener when the future resolves to true. If the future returns false the listener will not be closed.
tokio::spawn(async move { while let Some(Ok(_)) = listener_stream.next().await {} });
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::task::spawn(listener_handle.close_on(async move { rx.await.unwrap_or(true) }));
tx.send(true).unwrap();