Skip to content

How to wrap message handler? #378

Answered by Totodore
hchockarprasad asked this question in Q&A
Discussion options

You must be logged in to vote

Hey ! If you need to keep dynamic parameters for your message handler you can manually implement MessageHandler for a middleware struct that wraps your handlers:

use std::{marker::PhantomData, sync::Arc};

use socketioxide::{
    adapter::Adapter,
    extract::{Data, Extension, SocketRef},
    handler::{FromMessageParts, MessageHandler},
    socket::Socket,
    SocketIo,
};

/// An extension wrapper for the example.
#[derive(Debug, Clone)]
struct Info(&'static str);

struct MessageMiddleware<H, A, T> {
    handler: H,
    _phantom: PhantomData<fn(A, T)>,
}
impl<H, A, T> MessageMiddleware<H, A, T> {
    pub fn new(handler: H) -> Self {
        MessageMiddleware {
            handler,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Totodore
Comment options

Answer selected by Totodore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants