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
This commit is contained in:
2026-06-12 13:07:04 +02:00
parent 5495e20b1e
commit d08fd90e63
8 changed files with 52 additions and 21 deletions
-2
View File
@@ -21,9 +21,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
set(PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR}) set(PROJECT_DIR ${CMAKE_CURRENT_LIST_DIR})
# defines targets and sources # defines targets and sources
if(NOT EMSCRIPTEN)
add_subdirectory(UI) add_subdirectory(UI)
endif()
add_subdirectory(client) add_subdirectory(client)
+2 -1
View File
@@ -1,5 +1,5 @@
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT EMSCRIPTEN)
add_library(UI STATIC) add_library(UI STATIC)
find_package(Qt6 REQUIRED COMPONENTS Core SerialBus) find_package(Qt6 REQUIRED COMPONENTS Core SerialBus)
@@ -39,6 +39,7 @@ target_link_libraries(UI
Qt::SerialBus Qt::SerialBus
UI_enum UI_enum
) )
endif()
add_library(UI_enum INTERFACE) add_library(UI_enum INTERFACE)
+10 -6
View File
@@ -33,9 +33,10 @@ int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription("unisim_cpp client\n\tversion: " + QString::fromStdString(Git_version::branch) + parser.setApplicationDescription(QString("unisim_cpp client\n\tVersion: %1-%2%3")
"-" + QString::fromStdString(Git_version::shortSha1) + .arg(Git_version::branch)
QString::fromStdString(Git_version::dirty ? "-dirty" : "")); .arg(Git_version::shortSha1)
.arg(Git_version::dirty ? "-dirty" : ""));
parser.addHelpOption(); parser.addHelpOption();
QCommandLineOption host_option(QStringList() << "s" QCommandLineOption host_option(QStringList() << "s"
@@ -70,11 +71,15 @@ int main(int argc, char *argv[])
.value_or(spdlog::level::off)); .value_or(spdlog::level::off));
QUrl url; QUrl url;
url.setScheme("ws");
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
emscripten::val location = emscripten::val::global("location"); emscripten::val location = emscripten::val::global("location");
url.setHost(QString::fromStdString(location["href"].as<std::string>())); url.setHost(QString::fromStdString(location["hostname"].as<std::string>()));
url.setPath("/ws");
url.setPort(443);
url.setScheme("wss");
#else #else
url.setScheme("ws");
url.setPort(parser.value(port_option).toInt());
url.setHost(parser.value(host_option)); url.setHost(parser.value(host_option));
#if defined(__linux__) #if defined(__linux__)
if (parser.value(host_option).endsWith(".local")) if (parser.value(host_option).endsWith(".local"))
@@ -101,7 +106,6 @@ int main(int argc, char *argv[])
} }
#endif #endif
#endif #endif
url.setPort(parser.value(port_option).toInt());
QString default_sim = parser.value(default_sim_option); QString default_sim = parser.value(default_sim_option);
SPDLOG_INFO("Connecting client on {}", url.toString().toStdString()); SPDLOG_INFO("Connecting client on {}", url.toString().toStdString());
MainWindow w(url, default_sim); MainWindow w(url, default_sim);
+5
View File
@@ -1,5 +1,6 @@
#include "mainwindow.hpp" #include "mainwindow.hpp"
#include "factory.hpp" #include "factory.hpp"
#include "git_version.h"
#include "ui_base.hpp" #include "ui_base.hpp"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "web_socket_wrapper.hpp" #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)) m_web_socket(new Web_socket_wrapper(ws_url))
{ {
ui->setupUi(this); 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, 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) { [](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)) if (spdlog::default_logger_raw()->should_log((spdlog::level::level_enum)level))
+17 -7
View File
@@ -22,6 +22,22 @@ Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent)
emit on_connected(); emit on_connected();
}); });
connect(m_web_socket, &QWebSocket::disconnected, this, [=, this] { emit on_closed(); }); 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) { connect(m_web_socket, &QWebSocket::pong, this, [=, this](quint64 elapsedTime) {
if (elapsedTime > 1000) if (elapsedTime > 1000)
{ {
@@ -32,18 +48,12 @@ Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent)
m_pong_timer->stop(); m_pong_timer->stop();
m_ping_timer->start(1000); 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] { connect(m_pong_timer, &QTimer::timeout, this, [=, this] {
LOG_CRITICAL("Pong timeout"); LOG_CRITICAL("Pong timeout");
m_web_socket->close(); m_web_socket->close();
emit on_closed(); emit on_closed();
}); });
#endif
m_ping_timer->setSingleShot(true); m_ping_timer->setSingleShot(true);
m_pong_timer->setSingleShot(true); m_pong_timer->setSingleShot(true);
m_web_socket->setProxy(QNetworkProxy::NoProxy); m_web_socket->setProxy(QNetworkProxy::NoProxy);
+12
View File
@@ -7,3 +7,15 @@ add_subdirectory(json)
add_subdirectory(magic_enum) add_subdirectory(magic_enum)
add_subdirectory(spdlog) add_subdirectory(spdlog)
add_subdirectory(qwt) add_subdirectory(qwt)
if (EMSCRIPTEN)
target_compile_options(spdlog
PRIVATE
-pthread
)
target_link_options(spdlog
PRIVATE
-pthread
)
endif()
+4 -3
View File
@@ -29,9 +29,10 @@ int main(int argc, char *argv[])
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription("unisim_cpp server\n\tversion: " + QString::fromStdString(Git_version::branch) + parser.setApplicationDescription(QString("unisim_cpp server\n\tVersion: %1-%2%3")
"-" + QString::fromStdString(Git_version::shortSha1) + .arg(Git_version::branch)
QString::fromStdString(Git_version::dirty ? "-dirty" : "")); .arg(Git_version::shortSha1)
.arg(Git_version::dirty ? "-dirty" : ""));
parser.addHelpOption(); parser.addHelpOption();
QCommandLineOption port_option(QStringList() << "p" QCommandLineOption port_option(QStringList() << "p"
+2 -2
View File
@@ -215,8 +215,8 @@ json Simulators::changed_UI_items()
json changed; json changed;
changed["event"]["type"] = "ui_changed"; changed["event"]["type"] = "ui_changed";
const auto& ui_items = after["UI_items"]; const auto &ui_items = after["UI_items"];
const auto& prev_ui_items = m_before["UI_items"]; // may be null on first call const auto &prev_ui_items = m_before["UI_items"]; // may be null on first call
const size_t count = ui_items.size(); const size_t count = ui_items.size();
for (size_t i = 0; i < count; ++i) for (size_t i = 0; i < count; ++i)