Skip to main content

Channel

Struct Channel 

Source
pub struct Channel<H>
where H: WebImpl,
{ /* private fields */ }
Available on crate feature web03 only.
Expand description

A channel to a WebSocket server.

See Handle::channel for more details.

Implementations§

Source§

impl<H> Channel<H>
where H: WebImpl,

Source

pub fn id(&self) -> ChannelId

Get the connection identifier for this handle.

Source

pub fn handle(&self) -> &Handle<H>

Get a handle for this channel.

A handle sheds the channel information and allows for setting up things like broadcast listener.

Source

pub fn request(&self) -> RequestBuilder<'_, H, EmptyBody, EmptyCallback>

Send a request of type T over the current channel.

Returns a handle for the request.

If the handle is dropped, the request is cancelled.

§Examples
use yew::prelude::*;
use musli_web::web03::prelude::*;

mod api {
    use musli::{Decode, Encode};
    use musli_web::api;

    #[derive(Encode, Decode)]
    pub struct HelloRequest<'de> {
        pub message: &'de str,
    }

    #[derive(Encode, Decode)]
    pub struct HelloResponse<'de> {
        pub message: &'de str,
    }

    api::define! {
        pub type Hello;

        impl Endpoint for Hello {
            impl<'de> Request for HelloRequest<'de>;
            type Response<'de> = HelloResponse<'de>;
        }
    }
}

enum Msg {
    OnHello(Result<ws::Packet<api::Hello>, ws::Error>),
    OnChannel(Result<ws::Channel, ws::Error>),
}

#[derive(Properties, PartialEq)]
struct Props {
    ws: ws::Handle,
}

struct App {
    message: String,
    _hello: ws::Request,
    _channel_open: ws::Request,
    channel: ws::Channel,
}

impl Component for App {
    type Message = Msg;
    type Properties = Props;

    fn create(ctx: &Context<Self>) -> Self {
        let link = ctx.link().clone();

        let _channel_open = ctx.props().ws
            .channel()
            .on_open(ctx.link().callback(Msg::OnChannel))
            .send();

        Self {
            message: String::from("No Message :("),
            _hello: ws::Request::default(),
            _channel_open: _channel_open,
            channel: ws::Channel::default(),
        }
    }

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::OnHello(Err(error)) => {
                tracing::error!("Request error: {:?}", error);
                false
            }
            Msg::OnHello(Ok(packet)) => {
                if let Ok(response) = packet.decode() {
                    self.message = response.message.to_owned();
                }

                true
            }
            Msg::OnChannel(result) => {
                if let Ok(channel) = result {
                    self.channel = channel;
                }

                self._hello = self.channel
                    .request()
                    .body(api::HelloRequest { message: "Hello!"})
                    .on_packet(ctx.link().callback(Msg::OnHello))
                    .send();

                true
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        html! {
            <div>
                <h1>{"WebSocket Example"}</h1>
                <p>{format!("Message: {}", self.message)}</p>
            </div>
        }
    }
}

Trait Implementations§

Source§

impl<H> Debug for Channel<H>
where H: WebImpl,

Source§

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

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

impl<H> Default for Channel<H>
where H: WebImpl,

Construct a default connected handle.

§Examples

use musli_web::web03::prelude::*;

let channel = ws::Channel::default();
assert_eq!(channel.id(), ws::ChannelId::NONE);
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<H> Drop for Channel<H>
where H: WebImpl,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<H> PartialEq for Channel<H>
where H: WebImpl,

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<H> !RefUnwindSafe for Channel<H>

§

impl<H> !Send for Channel<H>

§

impl<H> !Sync for Channel<H>

§

impl<H> !UnwindSafe for Channel<H>

§

impl<H> Freeze for Channel<H>

§

impl<H> Unpin for Channel<H>

§

impl<H> UnsafeUnpin for Channel<H>

Blanket Implementations§

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

Source§

impl<Token, Builder, How> AllPropsFor<Builder, How> for Token
where Builder: Buildable<Token>, <Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> HasAllProps<(), T> for T

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<Option<T>> for T

Source§

fn into_prop_value(self) -> Option<T>

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> IntoPropValue<T> for T

Source§

fn into_prop_value(self) -> T

Convert self to a value of a Properties struct.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more