qchateau / packio

An asynchronous msgpack-RPC and JSON-RPC library built on top of Boost.Asio.
https://qchateau.github.io/packio/
Mozilla Public License 2.0
136 stars 20 forks source link

Accessing session from handler? #67

Closed kylegranger closed 1 year ago

kylegranger commented 1 year ago

Hi! I'm working with your library, and I'm trying to figure out how to get the session (or socket) from within a handler. We'd like to send back notifications on the same websocket; the server may have several users connected.

The only example code I saw related to this, was capturing a session_ptr when adding the handler (from basic_test_server_crash.cpp)

    std::shared_ptr<session_type> session_ptr;
    this->server_->async_serve([&](auto ec, auto session) {
        ASSERT_FALSE(ec);
        session->start();
        session_ptr = session;
    });
    this->server_->dispatcher()->add(
        "close", [&]() { session_ptr->socket().close(); });

But, I'm not sure something like that would work with more than one connection. I could capture the server itself, but I don't know how to go about finding the socket in the handler.

Thank you for any hints here!

Kyle

qchateau commented 1 year ago

Hi,

I've never thought about it which means sending a notification from the server is not supported. I'd have to spend some time to check if we can even access the session from a handler, then adding support for bidirectional rpc is yet another task...

kylegranger commented 1 year ago

Ah, ok, understood. Something like this then is clearly not supported at the present time.

As an alternative, we'll just be using then a full-duplex boost::asio websocket server, and implement the JSON-RPC processing on top of it ourselves. (It's a requirement that we use the main boost io_context.) Thank you for your response!