Struct worker::WebSocket

source ·
pub struct WebSocket { /* private fields */ }
Expand description

Wrapper struct for underlying worker-sys WebSocket

Implementations§

source§

impl WebSocket

source

pub async fn connect(url: Url) -> Result<WebSocket>

Attempts to establish a WebSocket connection to the provided Url.

§Example:
let ws = WebSocket::connect("wss://echo.zeb.workers.dev/".parse()?).await?;

// It's important that we call this before we send our first message, otherwise we will
// not have any event listeners on the socket to receive the echoed message.
let mut event_stream = ws.events()?;

ws.accept()?;
ws.send_with_str("Hello, world!")?;

while let Some(event) = event_stream.next().await {
    let event = event?;

    if let WebsocketEvent::Message(msg) = event {
        if let Some(text) = msg.text() {
            return Response::ok(text);
        }
    }
}

Response::error("never got a message echoed back :(", 500)
source

pub async fn connect_with_protocols( url: Url, protocols: Option<Vec<&str>> ) -> Result<WebSocket>

Attempts to establish a WebSocket connection to the provided Url and protocol.

§Example:
let ws = WebSocket::connect_with_protocols("wss://echo.zeb.workers.dev/".parse()?, Some(vec!["GiggleBytes"])).await?;
source

pub fn accept(&self) -> Result<()>

Accepts the connection, allowing for messages to be sent to and from the WebSocket.

source

pub fn send<T: Serialize>(&self, data: &T) -> Result<()>

Serialize data into a string using serde and send it through the WebSocket

source

pub fn send_with_str<S: AsRef<str>>(&self, data: S) -> Result<()>

Sends a raw string through the WebSocket

source

pub fn send_with_bytes<D: AsRef<[u8]>>(&self, bytes: D) -> Result<()>

Sends raw binary data through the WebSocket.

source

pub fn close<S: AsRef<str>>( &self, code: Option<u16>, reason: Option<S> ) -> Result<()>

Closes this channel. This method translates to three different underlying method calls based of the parameters passed.

If the following parameters are Some:

  • code and reason -> close_with_code_and_reason
  • code -> close_with_code
  • reason or none -> close

Effectively, if only reason is Some, the reason argument will be ignored.

source

pub fn events(&self) -> Result<EventStream<'_>>

Gets an implementation Stream that yields events from the inner WebSocket.

source

pub fn serialize_attachment<T: Serialize>(&self, value: T) -> Result<()>

source

pub fn deserialize_attachment<T: DeserializeOwned>(&self) -> Result<Option<T>>

Trait Implementations§

source§

impl AsRef<WebSocket> for WebSocket

source§

fn as_ref(&self) -> &WebSocket

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for WebSocket

source§

fn clone(&self) -> WebSocket

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for WebSocket

source§

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

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

impl From<WebSocket> for WebSocket

source§

fn from(socket: WebSocket) -> Self

Converts to this type from the input type.
source§

impl PartialEq for WebSocket

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for WebSocket

source§

impl Send for WebSocket

source§

impl StructuralPartialEq for WebSocket

source§

impl Sync for WebSocket

Auto Trait Implementations§

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> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
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