pub trait JsonWebSocketHandler:
Send
+ Sync
+ 'static {
type InboundMessage: DeserializeOwned + Send + 'static;
type OutboundMessage: Serialize + Send + 'static;
type StreamType: Stream<Item = Self::OutboundMessage> + Send + Sync + 'static;
// Required methods
fn connect(
&self,
conn: &mut WebSocketConn,
) -> impl Future<Output = Self::StreamType> + Send;
fn receive_message(
&self,
message: Result<Self::InboundMessage>,
conn: &mut WebSocketConn,
) -> impl Future<Output = ()> + Send;
// Provided method
fn disconnect(
&self,
conn: &mut WebSocketConn,
close_frame: Option<CloseFrame>,
) -> impl Future<Output = ()> + Send { ... }
}Expand description
§Implement this trait to use websockets with a json handler
JsonWebSocketHandler provides a small layer of abstraction on top of
WebSocketHandler, serializing and deserializing messages for
you. This may eventually move to a crate of its own.
§ℹ️ In order to use this trait, the json crate feature must be enabled.
use async_channel::{Receiver, Sender, unbounded};
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use trillium::log_error;
use trillium_websockets::{JsonWebSocketHandler, Result, WebSocketConn, json_websocket};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Response {
inbound_message: Inbound,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Inbound {
message: String,
}
struct SomeJsonChannel;
impl JsonWebSocketHandler for SomeJsonChannel {
type InboundMessage = Inbound;
type OutboundMessage = Response;
type StreamType = Pin<Box<Receiver<Self::OutboundMessage>>>;
async fn connect(&self, conn: &mut WebSocketConn) -> Self::StreamType {
let (s, r) = unbounded();
conn.insert_state(s);
Box::pin(r)
}
async fn receive_message(
&self,
inbound_message: Result<Self::InboundMessage>,
conn: &mut WebSocketConn,
) {
if let Ok(inbound_message) = inbound_message {
log_error!(
conn.state::<Sender<Response>>()
.unwrap()
.send(Response { inbound_message })
.await
);
}
}
}
// fn main() {
// trillium_smol::run(json_websocket(SomeJsonChannel));
// }Required Associated Types§
Sourcetype InboundMessage: DeserializeOwned + Send + 'static
type InboundMessage: DeserializeOwned + Send + 'static
A type that can be deserialized from the json sent from the connected clients
Sourcetype OutboundMessage: Serialize + Send + 'static
type OutboundMessage: Serialize + Send + 'static
A serializable type that will be sent in the StreamType and received by the connected websocket clients
Sourcetype StreamType: Stream<Item = Self::OutboundMessage> + Send + Sync + 'static
type StreamType: Stream<Item = Self::OutboundMessage> + Send + Sync + 'static
A type that implements a stream of
Self::OutboundMessages. This can be
futures_lite::stream::Pending if you never need to send an
outbound message.
Required Methods§
Sourcefn connect(
&self,
conn: &mut WebSocketConn,
) -> impl Future<Output = Self::StreamType> + Send
fn connect( &self, conn: &mut WebSocketConn, ) -> impl Future<Output = Self::StreamType> + Send
connect is called once for each upgraded websocket
connection, and returns a Self::StreamType.
Sourcefn receive_message(
&self,
message: Result<Self::InboundMessage>,
conn: &mut WebSocketConn,
) -> impl Future<Output = ()> + Send
fn receive_message( &self, message: Result<Self::InboundMessage>, conn: &mut WebSocketConn, ) -> impl Future<Output = ()> + Send
receive_message is called once for each successfully deserialized
InboundMessage along with the websocket conn that it was received
from.
Provided Methods§
Sourcefn disconnect(
&self,
conn: &mut WebSocketConn,
close_frame: Option<CloseFrame>,
) -> impl Future<Output = ()> + Send
fn disconnect( &self, conn: &mut WebSocketConn, close_frame: Option<CloseFrame>, ) -> impl Future<Output = ()> + Send
disconnect is called when websocket clients disconnect, along
with a CloseFrame, if one was provided. Implementing disconnect
is optional.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".