Struct worker_plus::WebSocket

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

Wrapper struct for underlying worker-sys WebSocket

Implementations§

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)

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

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

Sends a raw string through the WebSocket

Sends raw binary data through the WebSocket.

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.

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

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.