Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket Question #787

Open
ademDurakovic opened this issue Mar 31, 2024 · 4 comments
Open

Websocket Question #787

ademDurakovic opened this issue Mar 31, 2024 · 4 comments
Labels
question Issue can be closed by providing information

Comments

@ademDurakovic
Copy link

ademDurakovic commented Mar 31, 2024

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
Copy link
Author

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

@gittiver gittiver added the question Issue can be closed by providing information label Apr 1, 2024
@mrozigor
Copy link
Member

mrozigor commented Apr 2, 2024

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

@ademDurakovic
Copy link
Author

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
Copy link
Member

mrozigor commented Apr 2, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

3 participants