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::Request,
server_fn: F,
) -> Result<Server::Response, Error>
async fn run_server<F, Fut>( request: Server::Request, server_fn: F, ) -> Result<Server::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> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Returns the layout of the type.
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Returns whether the given value has been niched. Read more
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
Writes data to
out
indicating that a T
is niched.