Giter Club home page Giter Club logo

Comments (3)

sierret avatar sierret commented on August 29, 2024

When you say only one request, do you mean you want to run multiple websocket connections? Or do you mean you want a way to empty the message buffer(what holds incoming messages before they are read) in a parallel way?

Either way, I think you'll end up using the telemetry_client example. Basically, at the bottom of the typical websocketpp object "void start" function instead if:

m_endpoint.run(); //start websocket object event loop

You have:

websocketpp::lib::thread asio_thread(&client::run, &m_client); //create asynchronous event loop thread

asio_thread.detach(); //run asynchronous event loop thread

Be sure to have something like a infinite while loop or something running in the main thread. Otherwise, you'll get a segmentation fault. You can either have a separate main thread or choose a websocket connection to be the main. If it's the latter, on the main thread websocketpp client object, you will use:


websocketpp::lib::thread asio_thread(&client::run, &m_client); //create asynchronous event loop thread

asio_thread.join(); //use .join() instead of .detach()

And this "main thread" websocket object connection needs to be instantiated LAST. Or else it will block any later code from running.

Also maybe read up on on threading, mainly what join and detach actually does.

from websocketpp.

lkpworkspace avatar lkpworkspace commented on August 29, 2024

I think I have found a way to process http asynchronously in websocket server

/// in websocket server main loop
void on_http(connection_hdl hdl) {
  // TODO save hdl
  // ...

  // Call `defer_http_response` to delay processing without blocking the loop
  server::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
  con->defer_http_response();
}

/// in other thread
// TODO Get one connection to be processed
// ...

con->set_code(...);
con->set_body(...);
con->send_http_response();

I don't know if this is right

from websocketpp.

sierret avatar sierret commented on August 29, 2024

I'm not an huge expert but I don't know why you need to defer the response. All that does is stop the connection from timing out. To me it just sounds like extra overhead with no benefit for you. Specifically, I highly doubt it helps your situation as the documents specifically say that it will (likely) tie up resources. defer_http_response() will only slow down your program and won't help running asynchronously.

If you actually want asynchronous requests(and responses) why don't you use in your object .start function:

websocketpp::lib::thread asio_thread(object_run_function, your_object);
asio_thread.detach(); //or asio_thread.join(); if you want it running in the main thread

instead of:

m_endpoint.run();

that I mentioned earlier. It would run with each object function/handler running asynchronously upon a new response/message (as I understand it) .

from websocketpp.

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.