paritytech / jsonrpc

Rust JSON-RPC implementation
MIT License
791 stars 274 forks source link

how to create new handler when new connection income #648

Closed ahczbhht1989 closed 3 years ago

ahczbhht1989 commented 3 years ago

When I create a jsonrpc server on websocket channel, code as below

    let r = XXX::new();
    io.extend_with(r.to_delegate());
    let server = ServerBuilder::with_meta_extractor(io, |context: &RequestContext| {
        Arc::new(Session::new(context.sender()))
    })

and I have state in my handler,I want to have different state with different client(each client have it's own handler). The reason is code

// pass the io object to ServerBuilder
let server = ServerBuilder::with_meta_extractor(io, |context: &RequestContext| {
        Arc::new(Session::new(context.sender()))
    })

But I find when connect to server from different client, the state is current state in my XXX So How can I handle this.

tomusdrw commented 3 years ago

Please take a look at the PubSubMetadata trait.

You need to either customize the Metadata (instead of using the default Arc<Session>) and there you can hold any per-user data (make sure to implement correct Drop), or you need to associate some id to every user and then store the per-user data externally.