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<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: &'life1 mut WebSocketConn
    ) -> Pin<Box<dyn Future<Output = Self::StreamType> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn receive_message<'life0, 'life1, 'async_trait>(
        &'life0 self,
        message: Result<Self::InboundMessage>,
        conn: &'life1 mut WebSocketConn
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided method
    fn disconnect<'life0, 'life1, 'async_trait>(
        &'life0 self,
        conn: &'life1 mut WebSocketConn,
        close_frame: Option<CloseFrame<'static>>
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
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::{unbounded, Receiver, Sender};
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use trillium::{async_trait, log_error};
use trillium_websockets::{json_websocket, JsonWebSocketHandler, WebSocketConn, Result};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Response {
    inbound_message: Inbound,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Inbound {
    message: String,
}

struct SomeJsonChannel;

#[async_trait]
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.set_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§

source

type InboundMessage: DeserializeOwned + Send + 'static

A type that can be deserialized from the json sent from the connected clients

source

type OutboundMessage: Serialize + Send + 'static

A serializable type that will be sent in the StreamType and received by the connected websocket clients

source

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§

source

fn connect<'life0, 'life1, 'async_trait>( &'life0 self, conn: &'life1 mut WebSocketConn ) -> Pin<Box<dyn Future<Output = Self::StreamType> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

connect is called once for each upgraded websocket connection, and returns a Self::StreamType.

source

fn receive_message<'life0, 'life1, 'async_trait>( &'life0 self, message: Result<Self::InboundMessage>, conn: &'life1 mut WebSocketConn ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

receive_message is called once for each successfully deserialized InboundMessage along with the websocket conn that it was received from.

Provided Methods§

source

fn disconnect<'life0, 'life1, 'async_trait>( &'life0 self, conn: &'life1 mut WebSocketConn, close_frame: Option<CloseFrame<'static>> ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

disconnect is called when websocket clients disconnect, along with a CloseFrame, if one was provided. Implementing disconnect is optional.

Implementors§