Struct trillium_channels::ChannelConn

source ·
pub struct ChannelConn<'a> { /* private fields */ }
Expand description

A ChannelConn is a wrapper around a [WebSocketConn] that also contains a ChannelClient

It that provides convenient access to functions from the ChannelClient held in the WebSocketConn’s StateSet, and dereferences to the WebSocketConn for other functionality.

Implementations§

source§

impl ChannelConn<'_>

source

pub fn client(&self) -> Option<&ChannelClient>

Borrow the channel client

source

pub fn conn(&self) -> &WebSocketConn

Borrow the websocket conn

source

pub fn broadcast(&self, event: impl Into<ChannelEvent>)

Send a ChannelEvent to all connected clients. Note that these messages will only reach clients that subscribe to the event’s topic.

source

pub async fn send_event(&self, event: impl Into<ChannelEvent>)

Send a ChannelEvent to this specific client. Note that this message will only be received if the client subscribes to the event’s topic.

source

pub async fn reply_ok(&self, event: &ChannelEvent, response: &impl Serialize)

Send an ok reply in reference to the provided ChannelEvent with the provided response payload.

Note that this sets the event as "phx_reply" and the payload as {"status": "ok", "response": response }, as well as setting the reference field.

source

pub async fn reply_error(&self, event: &ChannelEvent, error: &impl Serialize)

Send an error reply in reference to the provided ChannelEvent with the provided response payload.

Note that this sets the event as "phx_error" as well as setting the reference field.

source

pub async fn allow_join(&self, event: &ChannelEvent, value: &impl Serialize)

Join a topic, sending an ok reply with the provided optional value. This sends an ok reply to the client as well as adding the topic to the client’s subscriptions.

source

pub async fn allow_leave(&self, event: &ChannelEvent, payload: &impl Serialize)

Leave a topic as requested by the provided channel event, including the optional payload. This sends an ok reply to the client as well as removing the channel from the client’s subscriptions.

Methods from Deref<Target = WebSocketConn>§

pub async fn send_string(&mut self, string: String) -> Result<(), Error>

send a [Message::Text] variant

pub async fn send_bytes(&mut self, bin: Vec<u8>) -> Result<(), Error>

send a [Message::Binary] variant

pub async fn send(&mut self, message: Message) -> Result<(), Error>

Sends a [Message] to the client

pub fn stopper(&self) -> Stopper

retrieve a clone of the server’s [Stopper]

pub async fn close(&mut self) -> Result<(), Error>

close the websocket connection gracefully

pub fn headers(&self) -> &Headers

retrieve the request headers for this conn

pub fn peer_ip(&self) -> Option<IpAddr>

retrieves the peer ip for this conn, if available

pub fn set_peer_ip(&mut self, peer_ip: Option<IpAddr>)

Sets the peer ip for this conn

pub fn path(&self) -> &str

retrieves the path part of the request url, up to and excluding any query component

pub fn querystring(&self) -> &str

Retrieves the query component of the path, excluding ?. Returns an empty string if there is no query component.

pub fn method(&self) -> Method

retrieve the request method for this conn

pub fn state<T>(&self) -> Option<&T>
where T: 'static,

retrieve state from the state set that has been accumulated by trillium handlers run on the trillium::Conn before it became a websocket. see trillium::Conn::state for more information

pub fn state_mut<T>(&mut self) -> Option<&mut T>
where T: 'static,

retrieve a mutable borrow of the state from the state set

pub fn set_state<T>(&mut self, state: T)
where T: Send + Sync + 'static,

👎Deprecated: use WebsocketConn::insert_state

see [insert_state]

pub fn insert_state<T>(&mut self, state: T) -> Option<T>
where T: Send + Sync + 'static,

inserts new state

returns the previously set state of the same type, if any existed

pub fn take_state<T>(&mut self) -> Option<T>
where T: 'static,

take some type T out of the state set that has been accumulated by trillium handlers run on the trillium::Conn before it became a websocket. see trillium::Conn::take_state for more information

pub fn take_inbound_stream( &mut self, ) -> Option<impl Stream<Item = Result<Message, Error>>>

take the inbound Message stream from this conn

pub fn inbound_stream( &mut self, ) -> Option<impl Stream<Item = Result<Message, Error>>>

borrow the inbound Message stream from this conn

Trait Implementations§

source§

impl<'a> Debug for ChannelConn<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for ChannelConn<'_>

§

type Target = WebSocketConn

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for ChannelConn<'_>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<'a> Freeze for ChannelConn<'a>

§

impl<'a> !RefUnwindSafe for ChannelConn<'a>

§

impl<'a> Send for ChannelConn<'a>

§

impl<'a> Sync for ChannelConn<'a>

§

impl<'a> Unpin for ChannelConn<'a>

§

impl<'a> !UnwindSafe for ChannelConn<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V