CrowCpp / Crow

A Fast and Easy to use microframework for the web.
https://crowcpp.org
Other
2.95k stars 335 forks source link

Websocket Question #787

Open ademDurakovic opened 3 months ago

ademDurakovic commented 3 months ago

Hi, I am writing a project that needs to have a c++ back end and I have a react front end.

I want to use web sockets with Crow to do this. I am trying to set it up but I don't really know if it's working nor do I have much experience with this. Currently this is my code and I just want to do a print to the terminal when the socket opens but the print never comes although the code seems to run fine.

Does this look correct or am I doing something wrong?

Here is my code:

include "crow.h"

include "crow/websocket.h"

int main() { crow::SimpleApp app; CROW_WEBSOCKET_ROUTE(app, "/ws") .onopen([](crow::websocket::connection& conn) { // Handle WebSocket connection open event // For example, you can send a welcome message to the client conn.send_text("WebSocket connection opened");

            // Print a message to indicate that a WebSocket connection is opened
            std::cout << "WebSocket connection opened" << std::endl;
        })
        .onmessage([](crow::websocket::connection& conn, const std::string& message, bool is_binary) {
            // Handle WebSocket message event
            // For example, you can echo the received message back to the client
            conn.send_text(message);
        })
        .onclose([](crow::websocket::connection& conn, const std::string& reason) {
            // Handle WebSocket connection close event
            // For example, you can log the reason for the connection closure
            std::cout << "WebSocket connection closed: " << reason << std::endl;
        });

// Start the server on port 18080
app.port(18080).run();
return 0;

}

Also, should I be doing web sockets or just normal HTTP? What do you guys think is simpler? I was doing HTTP but then I was just getting a bunch of annoying CORS errors in my front end so I thought I'd try web socket. The project is just a simple casino game room. I appreciate the help.

ademDurakovic commented 3 months ago

(2024-03-31 22:24:17) [INFO ] Crow/master server is running at http://0.0.0.0:18080 using 2 threads (2024-03-31 22:24:17) [INFO ] Call app.loglevel(crow::LogLevel::Warning) to hide Info level logs. (2024-03-31 22:24:23) [INFO ] Closing IO service 0x600003828280 (2024-03-31 22:24:23) [INFO ] Closing main IO service (0x134e04958) (2024-03-31 22:24:23) [INFO ] Exiting.

Process finished with exit code 0

this is the output if it helps.

mrozigor commented 3 months ago

How do you call server (address, port, request etc)? Are there any prints on console (about started/closed connection)?

ademDurakovic commented 3 months ago

How do you call server (address, port, request etc)? Are there any prints on console (about started/closed connection)?

What do you mean by call server? (sorry don't really understand all this) and no prints were made to the console I thought when I ran this code that the print in the .onopen() would be called but it doesn't.

mrozigor commented 3 months ago

I mean how your request looks like? WS URL, what data do you try to push? Do you see that connection was established on client side? Is response visible on client?