Description
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.