From d08fd90e634686159be130799702978ed974b0bd Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 11 Jun 2026 20:18:59 +0200 Subject: [PATCH] fixes for wasm client - ui_enum - spdlog pthread - url hostname from emscripten - websocket ping pong not exposed on js - version string in client - cleanup the generation of the version string - set fixed path for wasm ws --- CMakeLists.txt | 2 -- UI/CMakeLists.txt | 3 ++- client/main.cpp | 16 ++++++++++------ client/mainwindow.cpp | 5 +++++ client/web_socket_wrapper.cpp | 24 +++++++++++++++++------- external/CMakeLists.txt | 12 ++++++++++++ server/main.cpp | 7 ++++--- server/simulators.cpp | 4 ++-- 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63964c5..21407f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,9 +21,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY set(PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) # defines targets and sources -if(NOT EMSCRIPTEN) add_subdirectory(UI) -endif() add_subdirectory(client) diff --git a/UI/CMakeLists.txt b/UI/CMakeLists.txt index 7410c53..b31d794 100644 --- a/UI/CMakeLists.txt +++ b/UI/CMakeLists.txt @@ -1,5 +1,5 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) - +if(NOT EMSCRIPTEN) add_library(UI STATIC) find_package(Qt6 REQUIRED COMPONENTS Core SerialBus) @@ -39,6 +39,7 @@ target_link_libraries(UI Qt::SerialBus UI_enum ) +endif() add_library(UI_enum INTERFACE) diff --git a/client/main.cpp b/client/main.cpp index 6a918bb..3fa27f5 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -33,9 +33,10 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); QCommandLineParser parser; - parser.setApplicationDescription("unisim_cpp client\n\tversion: " + QString::fromStdString(Git_version::branch) + - "-" + QString::fromStdString(Git_version::shortSha1) + - QString::fromStdString(Git_version::dirty ? "-dirty" : "")); + parser.setApplicationDescription(QString("unisim_cpp client\n\tVersion: %1-%2%3") + .arg(Git_version::branch) + .arg(Git_version::shortSha1) + .arg(Git_version::dirty ? "-dirty" : "")); parser.addHelpOption(); QCommandLineOption host_option(QStringList() << "s" @@ -70,11 +71,15 @@ int main(int argc, char *argv[]) .value_or(spdlog::level::off)); QUrl url; - url.setScheme("ws"); #ifdef EMSCRIPTEN emscripten::val location = emscripten::val::global("location"); - url.setHost(QString::fromStdString(location["href"].as())); + url.setHost(QString::fromStdString(location["hostname"].as())); + url.setPath("/ws"); + url.setPort(443); + url.setScheme("wss"); #else + url.setScheme("ws"); + url.setPort(parser.value(port_option).toInt()); url.setHost(parser.value(host_option)); #if defined(__linux__) if (parser.value(host_option).endsWith(".local")) @@ -101,7 +106,6 @@ int main(int argc, char *argv[]) } #endif #endif - url.setPort(parser.value(port_option).toInt()); QString default_sim = parser.value(default_sim_option); SPDLOG_INFO("Connecting client on {}", url.toString().toStdString()); MainWindow w(url, default_sim); diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index 3422d37..e6a788a 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -1,5 +1,6 @@ #include "mainwindow.hpp" #include "factory.hpp" +#include "git_version.h" #include "ui_base.hpp" #include "ui_mainwindow.h" #include "web_socket_wrapper.hpp" @@ -15,6 +16,10 @@ MainWindow::MainWindow(QUrl ws_url, QString sim, QWidget *parent) m_web_socket(new Web_socket_wrapper(ws_url)) { ui->setupUi(this); + ui->statusbar->showMessage(QString("Version: %1-%2%3") + .arg(Git_version::branch) + .arg(Git_version::shortSha1) + .arg(Git_version::dirty ? "-dirty" : "")); connect(m_web_socket.get(), &Web_socket_wrapper::log_signal, this, [](const char *filename_in, int line_in, const char *funcname_in, int level, QString msg) { if (spdlog::default_logger_raw()->should_log((spdlog::level::level_enum)level)) diff --git a/client/web_socket_wrapper.cpp b/client/web_socket_wrapper.cpp index 58724ed..b1d697a 100644 --- a/client/web_socket_wrapper.cpp +++ b/client/web_socket_wrapper.cpp @@ -22,6 +22,22 @@ Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent) emit on_connected(); }); connect(m_web_socket, &QWebSocket::disconnected, this, [=, this] { emit on_closed(); }); + connect(m_web_socket, &QWebSocket::errorOccurred, this, [this](QAbstractSocket::SocketError error) { + LOG_ERROR(QString::fromStdString(std::string(magic_enum::enum_name(error)))); + }); + connect(m_ping_timer, &QTimer::timeout, this, [=, this] { +#ifdef EMSCRIPTEN + if (m_web_socket->state() != QAbstractSocket::ConnectedState) + { + m_web_socket->close(); + emit on_closed(); + } + m_ping_timer->start(1000); + }); +#else + m_web_socket->ping(); + m_pong_timer->start(1000); + }); connect(m_web_socket, &QWebSocket::pong, this, [=, this](quint64 elapsedTime) { if (elapsedTime > 1000) { @@ -32,18 +48,12 @@ Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent) m_pong_timer->stop(); m_ping_timer->start(1000); }); - connect(m_web_socket, &QWebSocket::errorOccurred, this, [this](QAbstractSocket::SocketError error) { - LOG_ERROR(QString::fromStdString(std::string(magic_enum::enum_name(error)))); - }); - connect(m_ping_timer, &QTimer::timeout, this, [=, this] { - m_web_socket->ping(); - m_pong_timer->start(1000); - }); connect(m_pong_timer, &QTimer::timeout, this, [=, this] { LOG_CRITICAL("Pong timeout"); m_web_socket->close(); emit on_closed(); }); +#endif m_ping_timer->setSingleShot(true); m_pong_timer->setSingleShot(true); m_web_socket->setProxy(QNetworkProxy::NoProxy); diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 7502cfa..47f19a2 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -7,3 +7,15 @@ add_subdirectory(json) add_subdirectory(magic_enum) add_subdirectory(spdlog) add_subdirectory(qwt) + +if (EMSCRIPTEN) +target_compile_options(spdlog + PRIVATE + -pthread +) + +target_link_options(spdlog + PRIVATE + -pthread +) +endif() \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp index e4ed246..f84b49c 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -29,9 +29,10 @@ int main(int argc, char *argv[]) QCoreApplication a(argc, argv); QCommandLineParser parser; - parser.setApplicationDescription("unisim_cpp server\n\tversion: " + QString::fromStdString(Git_version::branch) + - "-" + QString::fromStdString(Git_version::shortSha1) + - QString::fromStdString(Git_version::dirty ? "-dirty" : "")); + parser.setApplicationDescription(QString("unisim_cpp server\n\tVersion: %1-%2%3") + .arg(Git_version::branch) + .arg(Git_version::shortSha1) + .arg(Git_version::dirty ? "-dirty" : "")); parser.addHelpOption(); QCommandLineOption port_option(QStringList() << "p" diff --git a/server/simulators.cpp b/server/simulators.cpp index f14feb2..d30a0d0 100644 --- a/server/simulators.cpp +++ b/server/simulators.cpp @@ -215,8 +215,8 @@ json Simulators::changed_UI_items() json changed; changed["event"]["type"] = "ui_changed"; - const auto& ui_items = after["UI_items"]; - const auto& prev_ui_items = m_before["UI_items"]; // may be null on first call + const auto &ui_items = after["UI_items"]; + const auto &prev_ui_items = m_before["UI_items"]; // may be null on first call const size_t count = ui_items.size(); for (size_t i = 0; i < count; ++i)