Giter VIP home page Giter VIP logo

Comments (2)

niklasad1 avatar niklasad1 commented on September 4, 2024

I think most of what you want is possible but you need to read the body of request/response which may be a little overhead.

for ws, only connection upgrade request is visible there in my understanding.

Simply you use utilize jsonrpsee as tower service and then you could do manage that yourself:

    let svc = tower::service_fn(move |req: hyper::Request<hyper::body::Incoming>| {
        let is_websocket = jsonrpsee::server::ws::is_upgrade_request(&req);
        let transport_label = if is_websocket { "ws" } else { "http" };

        // extract stuff that you are interested here from the request here

        async move {
            if is_websocket {
                let on_disconnect = svc.on_session_closed();

                // Spawn a task to handle when the connection is closed.
                tokio::spawn(async move {
                    let now = std::time::Instant::now();
                    on_disconnect.await;
                    tracing::debug!("protocol: ws, duration: {now}")
                });
            }
            
            let rp = svc.call(req).await;

            // extract stuff that you are interested here from the response here
            tracing::debug!("protocol: {transport_label}, ...")
        }
    });

Cannot implement at rpc moddleware because cannot access http level information such as response http status.

You can write custom RPC middleware layer that extract that stuff from the http request itself to be accessible for the rpc middleware, have look at this example

I think these APIs should be sufficiently flexible for anyone to implement what you want

from jsonrpsee.

shunsukew avatar shunsukew commented on September 4, 2024

I think most of what you want is possible but you need to read the body of request/response which may be a little overhead.

Yes, in case we read the body from hyper::body::Incoming at somewhere other than call_with_service fn. If possible, this step should be done only once to avoid reading twice.

Simply you use utilize jsonrpsee as tower service and then you could do manage that yourself:

In above code snippet, we can see connection upgrade req and rp, but cannot see websocket messages inside an established connection. ws message is recovered here in ws background task for the first time. so rpc method calls sent as ws message from client side cannot be detected at tower service (is not hyper::Request<hyper::body::Incoming>) I think. (please correct me if I'm wrong)

You can write custom RPC middleware layer that extract that stuff from the http request itself to be accessible for the rpc middleware, have look at this example

Thank you! but what about http response status code? status code will be decided after exiting from rpc middlewares.

from jsonrpsee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.