For the important context:
- I’m trying to make a C++ client that connects and sends data to a websocket server. I didn’t make and have no control over the server, its my friends. We have to use websockets because we’re trying to connect to CC: Tweaked (a minecraft mod) computers.
- I struggled to even make a valid websocket connection for a while, but my friend came in clutch with a solution (though I’m pretty sure he got help from AI). I’ve tried to do my own research on winhttp to understand it, and I don’t think there are any major flaws in the code, but I’m not 100% sure (I did modify it a bit).
Currently my connection doesn’t seem to sever even if the server is completely terminated. The program has no idea the connection is broken, and keeps trying to send data when I ask it to. How can I find out when the connection is broken so I can display it and disable sending data until it’s reconnected?
Here are pastebin links to the class: WebSocketClient.cpp and WebSocketClient.h
I then have a different class for utilizing this client (in a wxWidgets program). For example, here’s the function I use to send data:
json Client::Chat(std::string guid, std::string label, std::string data) {
json message = {
{"id", guid},
{"label", label},
{"data", data}
};
if (client.Send(message)) {
std::string response = client.Receive();
try {
json responseJson = json::parse(response);
return responseJson;
}
catch (json::parse_error& e) {
return nullptr;
}
}
return nullptr;
}
I would like this function to know if the connection is active, because currently if the server shuts down and I call this function, my program will completely freeze waiting for a response that cannot come, until I forcibly shut it down.
After googling a bit I found something called the “keepalive concept”. I’m not sure I understood it correctly, so maybe it’s actually what I want, but after looking into it for a bit I’m pretty sure it won’t help me. I don’t want to keep the connection alive, I actively want to fully terminate it until a new one is made. At least that’s what I think I want, but my networking knowledge is pretty lacking, so maybe I’m wrong on a more fundamental level.