#[cfg(all(feature = "aws-lc-rs", feature = "ring"))]
compile_error!("features \"aws-lc-rs\" and \"ring\" are mutually exclusive.");
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
compile_error!("either \"aws-lc-rs\" or \"ring\" must be enabled to use the ws module.");
#[cfg(all(feature = "tokio-tungstenite", feature = "tokio-websockets"))]
compile_error!("features \"tokio-tungstenite\" and \"tokio-websockets\" are mutually exclusive.");
macro_rules! log {
($level:tt($indent:literal), $fmt:literal $($arg:tt)*) => {
#[cfg(debug_assertions)]
eprintln!(
"{:indent$}{}(via::ws): {}",
"",
stringify!($level),
format_args!($fmt $($arg)*),
indent = $indent
);
};
}
mod channel;
mod error;
mod io;
mod request;
mod run;
mod upgrade;
mod util;
pub use channel::*;
pub use error::{Result, ResultExt};
pub use request::Request;
pub use upgrade::Ws;
/// Upgrade the connection to a web socket.
///
/// *Note:*
///
/// In order to guarantee progress of the receive loop of your web socket
/// listener, you must await at least one future in the body of the loop.
///
/// # Example
///
/// ```no_run
/// use std::process::ExitCode;
/// use via::ws::{self, Channel, Message};
/// use via::{Error, Server};
///
/// async fn echo(mut channel: Channel, _: ws::Request) -> ws::Result {
/// while let Some(message) = channel.recv().await {
/// if message.is_close() {
/// println!("info: close requested by client");
/// break;
/// }
///
/// if message.is_binary() || message.is_text() {
/// channel.send(message).await?;
/// } else if cfg!(debug_assertions) {
/// println!("warn: ignoring message {:?}", message);
/// }
/// }
///
/// Ok(())
/// }
///
/// #[tokio::main]
/// async fn main() -> Result<ExitCode, Error> {
/// let mut app = via::app(());
///
/// // GET /echo ~> web socket upgrade.
/// app.route("/echo").to(via::ws(echo));
///
/// Server::new(app).listen(("127.0.0.1", 8080)).await
/// }
///```
///
pub fn ws<T, App, Await>(listener: T) -> Ws<T>
where
T: Fn(Channel, Request<App>) -> Await,
Await: Future<Output = Result> + Send,
{
Ws::new(listener)
}