pub struct Channel<CH>(/* private fields */);Expand description
Trillium handler containing a ChannelHandler
This is constructed from a ChannelHandler using Channel::new
and dereferences to that type.
By default, a browser can only connect from a page on the same host as the
socket. To serve pages from other origins, see Channel::allow_origins.
Implementations§
Source§impl<CH: ChannelHandler> Channel<CH>
impl<CH: ChannelHandler> Channel<CH>
Sourcepub fn new(channel_handler: CH) -> Self
pub fn new(channel_handler: CH) -> Self
Constructs a new trillium Channel handler from the provided
ChannelHandler implementation
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 connections only from pages on these origins.
Channel::new(Chat).allow_origins(["https://app.example.com"]);Origins are compared exactly, after normalizing default ports and idn hosts, never by
prefix or suffix. For a family of subdomains, use
allow_origin_fn.
A connection 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 connections 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).
Sourcepub fn allow_any_origin(self) -> Self
pub fn allow_any_origin(self) -> Self
Accept connections from any origin, disabling the same-origin default.
Any page on the web can then open a socket to this channel and act with the browser’s ambient authority — the visitor’s cookies are attached to the connection. Only do this if the socket is either unauthenticated or authenticated by something the page cannot replay, such as a token in the socket’s query string or join payload.
Sourcepub fn with_protocol_config(self, config: WebSocketConfig) -> Self
pub fn with_protocol_config(self, config: WebSocketConfig) -> Self
Configure the websocket protocol for every connection, such as the maximum message and frame sizes.
Sourcepub fn broadcaster(&self) -> ChannelBroadcaster
pub fn broadcaster(&self) -> ChannelBroadcaster
Retrieve a ChannelBroadcaster that can be moved elsewhere or cloned in order to trigger channel events and listen for global events.
Sourcepub fn broadcast(&self, event: impl Into<ChannelEvent>)
pub fn broadcast(&self, event: impl Into<ChannelEvent>)
Send a ChannelEvent to all connected clients that subscribe to the topic
Trait Implementations§
Source§impl<CH> Handler for Channel<CH>where
CH: ChannelHandler,
impl<CH> Handler for Channel<CH>where
CH: ChannelHandler,
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§async fn before_send(&self, conn: Conn) -> Conn
async fn before_send(&self, conn: Conn) -> Conn
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.Auto Trait Implementations§
impl<CH> !RefUnwindSafe for Channel<CH>
impl<CH> !UnwindSafe for Channel<CH>
impl<CH> Forget for Channel<CH>
impl<CH> Freeze for Channel<CH>
impl<CH> Move for Channel<CH>
impl<CH> Send for Channel<CH>
impl<CH> Sync for Channel<CH>
impl<CH> Unpin for Channel<CH>
impl<CH> UnsafeUnpin for Channel<CH>where
WebSocket<ChannelCentral<CH>>: 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