Giter Site home page Giter Site logo

Comments (5)

IronsDu avatar IronsDu commented on August 15, 2024

@jagernicolas
Hi, see here:

auto wsEnterCallback = [](const HttpSession::Ptr& httpSession,
WebSocketFormat::WebSocketFrameType opcode,
const std::string& payload) {
std::cout << "frame enter of type:" << int(opcode) << std::endl;
std::cout << "payload is:" << payload << std::endl;
// echo frame
auto frame = std::make_shared<std::string>();
WebSocketFormat::wsFrameBuild(payload.c_str(),
payload.size(),
*frame,
WebSocketFormat::WebSocketFrameType::TEXT_FRAME,
true,
false);
httpSession->send(frame);

When session enter, We can push HttpSession::Ptr into session list(need mutex).

And, we can use HttpSessionHandlers::setClosedCallback for remove session from session list, like this:

.configureEnterCallback([httpEnterCallback, wsEnterCallback](const HttpSession::Ptr& httpSession, HttpSessionHandlers& handlers) {
handlers.setHttpCallback(httpEnterCallback);
handlers.setWSCallback(wsEnterCallback);

from brynet.

IronsDu avatar IronsDu commented on August 15, 2024

also add some code in this:

auto enterCallback = [host](const HttpSession::Ptr& httpSession, HttpSessionHandlers& handlers) {
HttpRequest request;
request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET);
request.setUrl("/ws");
request.addHeadValue("Host", host);
request.addHeadValue("Upgrade", "websocket");
request.addHeadValue("Connection", "Upgrade");
request.addHeadValue("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
request.addHeadValue("Sec-WebSocket-Version", "13");
std::string requestStr = request.getResult();
httpSession->send(requestStr.c_str(), requestStr.size());
handlers.setWSConnected([](const HttpSession::Ptr& session, const HTTPParser&) {
for (int i = 0; i < 200; i++)
{
sendPacket(session, "hello, world!", 13);
}
});
handlers.setWSCallback([](const HttpSession::Ptr& session,
WebSocketFormat::WebSocketFrameType, const std::string& payload) {
std::cout << payload << std::endl;
sendPacket(session, "hello, world!", 13);
count += 1;
});
};

from brynet.

jagernicolas avatar jagernicolas commented on August 15, 2024

Hi @IronsDu ,
I was exactly doing that... however here my mistake,
std::list<HttpSession::Ptr&> list;
instead of
std::list<HttpSession::Ptr> list;

auto wsEnterCallback = [&list](const HttpSession::Ptr& httpSession, 
         WebSocketFormat::WebSocketFrameType opcode, 
         const std::string& payload) { 
             std::cout << "frame enter of type:" << int(opcode) << std::endl; 
             std::cout << "payload is:" << payload << std::endl; 
             // echo frame 

             liste.push_back(session); // TODO: add mutex

             auto frame = std::make_shared<std::string>(); 
             WebSocketFormat::wsFrameBuild(payload.c_str(), 
                 payload.size(), 
                 *frame, 
                 WebSocketFormat::WebSocketFrameType::TEXT_FRAME, 
                 true, 
                 false); 
             httpSession->send(frame); 

I will make a short example, would you me to share it with you ? I could make a PR when it's done (today or tomorrow)

from brynet.

jagernicolas avatar jagernicolas commented on August 15, 2024

nevermind, I just saw that your example BenchWebsocket.cpp show that.

thx @IronsDu for your quick support!

from brynet.

IronsDu avatar IronsDu commented on August 15, 2024

Thank you. You can make a PR for this example.

from brynet.

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.