Improve WebSocket server startup and error reporting

- Introduce a brief wait time to ensure the port is released before starting the server.
- Update server options to allow port reuse, enhancing server flexibility.
- Enhance error handling during context creation to provide more informative error messages regarding port usage and permissions.
This commit is contained in:
Torsten Schulz (local)
2025-11-18 08:08:07 +01:00
committed by Torsten (PC)
parent 32bc126def
commit b3c9c8f37c

View File

@@ -93,6 +93,9 @@ void WebSocketServer::stop() {
}
void WebSocketServer::startServer() {
// Kurze Wartezeit, falls ein vorheriger Prozess den Port noch freigibt
std::this_thread::sleep_for(std::chrono::milliseconds(100));
struct lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = port;
@@ -101,7 +104,8 @@ void WebSocketServer::startServer() {
// Server-Optionen für mehrere gleichzeitige Verbindungen
info.options = LWS_SERVER_OPTION_VALIDATE_UTF8 |
LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE |
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME;
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME |
LWS_SERVER_OPTION_REUSE_ADDR; // Erlaube Port-Wiederverwendung (SO_REUSEADDR)
// Erlaube mehrere Verbindungen pro IP
info.ka_time = 60;
@@ -127,7 +131,10 @@ void WebSocketServer::startServer() {
context = lws_create_context(&info);
if (!context) {
throw std::runtime_error("Failed to create LWS context");
std::string errorMsg = "Failed to create LWS context on port " + std::to_string(port);
errorMsg += ". Port may be in use or insufficient permissions.";
std::cerr << errorMsg << std::endl;
throw std::runtime_error(errorMsg);
}
std::cout << "WebSocket-Server erfolgreich gestartet auf Port " << port << std::endl;