Skip to content

How to return a ws::Handler trait object by the factory param of ws::connect? #324

Description

@ngugc
    pub fn connect<U, F, H>(url: U, factory: F) -> Result<()> 
    where
        U: Borrow<str>,
        F: FnMut(Sender) -> H,
        H: Handler, 

This is the signature of ws::connect. A ws::Handler trait object should be returned by F.
I have the following implementations of ws::Handler.

    struct A { ... }
    impl ws::Handler for A {}
    
    struct B { ... }
    impl ws::Handler for B {}
    
    fn new_ws(t: &String) -> Box<dyn ws::Handler> {
        match t {
            "A" => Box::new(A { ... }),
            "B" => Box::new(B { ... }),
            &_ => unimplemented!(),
        }
    }

How can I use new_ws in ws::connect as below?

    ws::connect(URL, |sender| {
        new_ws("A")
    }).unwrap_or_else(|err| {
        error!("websocket error: {:?}", err);
    })

The error:

expected an `Fn<(ws::message::Message,)>` closure, found `dyn ws::handler::Handler

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions