pub struct Websocket<InputEncoding, OutputEncoding>(/* private fields */);Expand description
The websocket protocol that encodes the input and output streams using a websocket connection.
The websocket protocol accepts two generic argument that define the input and output serialization
formats. For example, Websocket<CborEncoding, JsonEncoding> would accept a stream of Cbor-encoded messages
and return a stream of JSON-encoded messages.
§Example
use server_fn::{ServerFnError, BoxedStream, Websocket, codec::JsonEncoding};
use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct Message {
user: String,
message: String,
}
// The websocket protocol can be used on any server function that accepts and returns a [`BoxedStream`]
// with items that can be encoded by the input and output encoding generics.
//
// In this case, the input and output encodings are [`Json`] and [`Json`], respectively which requires
// the items to implement [`Serialize`] and [`Deserialize`].
#[server(protocol = Websocket<JsonEncoding, JsonEncoding>)]
async fn echo_websocket(
input: BoxedStream<Message, ServerFnError>,
) -> Result<BoxedStream<Message, ServerFnError>, ServerFnError> {
Ok(input.into())
}Trait Implementations§
Source§impl<Input, InputItem, OutputItem, InputEncoding, OutputEncoding, Client, Server, Error, InputStreamError, OutputStreamError> Protocol<Input, BoxedStream<OutputItem, OutputStreamError>, Client, Server, Error, InputStreamError, OutputStreamError> for Websocket<InputEncoding, OutputEncoding>where
Input: Deref<Target = BoxedStream<InputItem, InputStreamError>> + Into<BoxedStream<InputItem, InputStreamError>> + From<BoxedStream<InputItem, InputStreamError>>,
InputEncoding: Encodes<InputItem> + Decodes<InputItem>,
OutputEncoding: Encodes<OutputItem> + Decodes<OutputItem>,
InputStreamError: FromServerFnError + Send,
OutputStreamError: FromServerFnError + Send,
Error: FromServerFnError + Send,
Server: Server<Error, InputStreamError, OutputStreamError>,
Client: Client<Error, InputStreamError, OutputStreamError>,
OutputItem: Send + 'static,
InputItem: Send + 'static,
impl<Input, InputItem, OutputItem, InputEncoding, OutputEncoding, Client, Server, Error, InputStreamError, OutputStreamError> Protocol<Input, BoxedStream<OutputItem, OutputStreamError>, Client, Server, Error, InputStreamError, OutputStreamError> for Websocket<InputEncoding, OutputEncoding>where
Input: Deref<Target = BoxedStream<InputItem, InputStreamError>> + Into<BoxedStream<InputItem, InputStreamError>> + From<BoxedStream<InputItem, InputStreamError>>,
InputEncoding: Encodes<InputItem> + Decodes<InputItem>,
OutputEncoding: Encodes<OutputItem> + Decodes<OutputItem>,
InputStreamError: FromServerFnError + Send,
OutputStreamError: FromServerFnError + Send,
Error: FromServerFnError + Send,
Server: Server<Error, InputStreamError, OutputStreamError>,
Client: Client<Error, InputStreamError, OutputStreamError>,
OutputItem: Send + 'static,
InputItem: Send + 'static,
Source§async fn run_server<F, Fut>(
request: <Server as Server<Error, InputStreamError, OutputStreamError>>::Request,
server_fn: F,
) -> Result<<Server as Server<Error, InputStreamError, OutputStreamError>>::Response, Error>
async fn run_server<F, Fut>( request: <Server as Server<Error, InputStreamError, OutputStreamError>>::Request, server_fn: F, ) -> Result<<Server as Server<Error, InputStreamError, OutputStreamError>>::Response, Error>
Run the server function on the server. The implementation should handle deserializing the
input, running the server function, and serializing the output.
Source§fn run_client(
path: &str,
input: Input,
) -> impl Future<Output = Result<BoxedStream<OutputItem, OutputStreamError>, Error>> + Send
fn run_client( path: &str, input: Input, ) -> impl Future<Output = Result<BoxedStream<OutputItem, OutputStreamError>, Error>> + Send
Run the server function on the client. The implementation should handle serializing the
input, sending the request, and deserializing the output.
Auto Trait Implementations§
impl<InputEncoding, OutputEncoding> Freeze for Websocket<InputEncoding, OutputEncoding>
impl<InputEncoding, OutputEncoding> RefUnwindSafe for Websocket<InputEncoding, OutputEncoding>where
InputEncoding: RefUnwindSafe,
OutputEncoding: RefUnwindSafe,
impl<InputEncoding, OutputEncoding> Send for Websocket<InputEncoding, OutputEncoding>
impl<InputEncoding, OutputEncoding> Sync for Websocket<InputEncoding, OutputEncoding>
impl<InputEncoding, OutputEncoding> Unpin for Websocket<InputEncoding, OutputEncoding>
impl<InputEncoding, OutputEncoding> UnwindSafe for Websocket<InputEncoding, OutputEncoding>where
InputEncoding: UnwindSafe,
OutputEncoding: UnwindSafe,
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
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> SerializableKey for T
impl<T> SerializableKey for T
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Borrows the value.
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Takes the value.