pub struct WebSocket<H> { /* private fields */ }Expand description
The trillium handler. See crate-level docs for example usage.
Implementations§
Source§impl<T> WebSocket<JsonHandler<T>>where
T: JsonWebSocketHandler,
impl<T> WebSocket<JsonHandler<T>>where
T: JsonWebSocketHandler,
Sourcepub fn new_json(handler: T) -> Self
pub fn new_json(handler: T) -> Self
Build a new trillium WebSocket handler from the provided
JsonWebSocketHandler
Source§impl<H> WebSocket<H>where
H: WebSocketHandler,
impl<H> WebSocket<H>where
H: WebSocketHandler,
Sourcepub fn new(handler: H) -> Self
pub fn new(handler: H) -> Self
Build a new WebSocket with an async handler function that
receives a WebSocketConn
Sourcepub fn allow_origins<'a>(
self,
origins: impl IntoIterator<Item = &'a str>,
) -> Self
pub fn allow_origins<'a>( self, origins: impl IntoIterator<Item = &'a str>, ) -> Self
Accept handshakes only from pages on these origins.
websocket.allow_origins(["https://app.example.com", "https://admin.example.com"]);Origins are compared exactly, after normalizing default ports and idn hosts. Nothing is
matched by prefix or suffix, because an allowed origin of example.com matched by suffix
also admits evil-example.com. For a family of subdomains, use
allow_origin_fn.
A request with no Origin header is allowed, as it did not come from a browser.
§Panics
Panics if any of the provided strings is not a url with a scheme and a host, or if it carries a path, query, fragment, or userinfo.
Sourcepub fn allow_origin_fn<F>(self, predicate: F) -> Self
pub fn allow_origin_fn<F>(self, predicate: F) -> Self
Accept handshakes for which this predicate returns true.
The argument is the raw Origin header, so None (a non-browser client) stays
distinguishable from Some("null") (a sandboxed iframe or a file:// page, which is
attacker-reachable and should generally be rejected).
websocket.allow_origin_fn(|origin| match origin {
None => true,
Some(origin) => origin
.strip_prefix("https://")
.is_some_and(|host| host == "example.com" || host.ends_with(".example.com")),
});Sourcepub fn allow_any_origin(self) -> Self
pub fn allow_any_origin(self) -> Self
Accept handshakes from any origin, disabling the same-origin default.
Any page on the web can then open a socket to this handler and act with the browser’s ambient authority — the visitor’s cookies are attached to the handshake. Only do this if the socket is either unauthenticated or authenticated by something the page cannot replay, such as a token the client sends in its first message.
Sourcepub fn with_protocols(self, protocols: &[&str]) -> Self
pub fn with_protocols(self, protocols: &[&str]) -> Self
protocols is a sequence of known protocols. On successful handshake,
the returned response headers contain the first protocol in this list
which the server also knows.
Sourcepub fn with_protocol_config(self, config: WebSocketConfig) -> Self
pub fn with_protocol_config(self, config: WebSocketConfig) -> Self
configure the websocket protocol
Sourcepub fn required(self) -> Self
pub fn required(self) -> Self
configure this handler to halt and send back a 426 Upgrade Required if a websocket cannot be negotiated
Trait Implementations§
Source§impl<H> Handler for WebSocket<H>where
H: WebSocketHandler,
impl<H> Handler for WebSocket<H>where
H: WebSocketHandler,
Source§async fn run(&self, conn: Conn) -> Conn
async fn run(&self, conn: Conn) -> Conn
Source§async fn init(&mut self, info: &mut Info)
async fn init(&mut self, info: &mut Info)
Source§fn has_upgrade(&self, upgrade: &Upgrade) -> bool
fn has_upgrade(&self, upgrade: &Upgrade) -> bool
Handler::upgrade. The first handler that responds true to this will receive
ownership of the trillium::Upgrade in a subsequent call to
Handler::upgradeSource§async fn upgrade(&self, upgrade: Upgrade)
async fn upgrade(&self, upgrade: Upgrade)
Handler::has_upgrade and will
only be called once for this upgrade. There is no return value, and this function takes
exclusive ownership of the underlying transport once this is called. You can downcast
the transport to whatever the source transport type is and perform any non-http protocol
communication that has been negotiated. You probably don’t want this unless you’re
implementing something like websockets. Please note that for many transports such as
TcpStreams, dropping the transport (and therefore the Upgrade) will hang up /
disconnect.Source§fn before_send(&self, conn: Conn) -> impl Future<Output = Conn> + Send
fn before_send(&self, conn: Conn) -> impl Future<Output = Conn> + Send
Auto Trait Implementations§
impl<H> !RefUnwindSafe for WebSocket<H>
impl<H> !UnwindSafe for WebSocket<H>
impl<H> Freeze for WebSocket<H>where
H: Freeze,
impl<H> Send for WebSocket<H>where
H: Send,
impl<H> Sync for WebSocket<H>where
H: Sync,
impl<H> Unpin for WebSocket<H>where
H: Unpin,
impl<H> UnsafeUnpin for WebSocket<H>where
H: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<R> CryptoRng for R
impl<R> RngCore for Rwhere
R: Rng,
Source§impl<R> RngExt for R
impl<R> RngExt for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read more