Adapted for web

This commit is contained in:
Vladislav Khorev committed 2026-11-09 17:20:04 +06:00
commit 3fe21230cc
20 files changed
+241 -72

No files matched your search

+123 -2
View File
@@ -1,7 +1,11 @@
#include "WebsocketGameClient.h"
#include "NetworkProtocol.h"
#ifdef __EMSCRIPTEN__
#include <emscripten/websocket.h>
#else
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#endif
#include <atomic>
#include <deque>
#include <iostream>
@@ -14,15 +18,131 @@
namespace FRG {
namespace {
#ifndef __EMSCRIPTEN__
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace websocket = beast::websocket;
using tcp = asio::ip::tcp;
#endif
} // namespace
class WebsocketGameClient::Impl {
public:
#ifdef __EMSCRIPTEN__
Impl(std::string host, std::string port, std::string path)
: host_(std::move(host))
, port_(std::move(port))
, path_(std::move(path))
{
url_ = "ws://" + host_ + ':' + port_ +
(path_.empty() || path_.front() == '/' ? path_ : "/" + path_);
std::cout << "[network] Opening " << url_ << std::endl;
EmscriptenWebSocketCreateAttributes attributes = {};
attributes.url = url_.c_str();
attributes.protocols = nullptr;
attributes.createOnMainThread = EM_TRUE;
socket_ = emscripten_websocket_new(&attributes);
if (socket_ <= 0) {
fail("create WebSocket");
return;
}
emscripten_websocket_set_onopen_callback(
socket_, this, &Impl::onOpen);
emscripten_websocket_set_onerror_callback(
socket_, this, &Impl::onError);
emscripten_websocket_set_onclose_callback(
socket_, this, &Impl::onClose);
emscripten_websocket_set_onmessage_callback(
socket_, this, &Impl::onMessage);
}
~Impl()
{
if (socket_ > 0) {
closing_ = true;
emscripten_websocket_close(socket_, 1000, "client shutdown");
emscripten_websocket_delete(socket_);
}
}
std::vector<std::string> takeMessages()
{
std::vector<std::string> result;
result.swap(messages_);
return result;
}
bool connected() const { return connected_; }
bool connectionFailed() const { return connectionFailed_; }
void send(std::string message)
{
if (!connected_ || socket_ <= 0) return;
const EMSCRIPTEN_RESULT result =
emscripten_websocket_send_utf8_text(socket_, message.c_str());
if (result != EMSCRIPTEN_RESULT_SUCCESS)
fail("send WebSocket message");
}
private:
static EM_BOOL onOpen(
int, const EmscriptenWebSocketOpenEvent*, void* userData)
{
auto* self = static_cast<Impl*>(userData);
self->connected_ = true;
std::cout << "[network] Connected to " << self->url_ << std::endl;
return EM_TRUE;
}
static EM_BOOL onError(
int, const EmscriptenWebSocketErrorEvent*, void* userData)
{
static_cast<Impl*>(userData)->fail("WebSocket");
return EM_TRUE;
}
static EM_BOOL onClose(
int, const EmscriptenWebSocketCloseEvent*, void* userData)
{
auto* self = static_cast<Impl*>(userData);
self->connected_ = false;
if (!self->closing_) self->fail("WebSocket close");
return EM_TRUE;
}
static EM_BOOL onMessage(
int, const EmscriptenWebSocketMessageEvent* event, void* userData)
{
auto* self = static_cast<Impl*>(userData);
if (!event || event->isText != EM_TRUE) {
self->fail("binary WebSocket message");
return EM_TRUE;
}
self->messages_.emplace_back(
reinterpret_cast<const char*>(event->data), event->numBytes);
return EM_TRUE;
}
void fail(const char* operation)
{
connected_ = false;
connectionFailed_ = true;
std::cerr << "[network] " << operation << " failed" << std::endl;
}
std::string host_;
std::string port_;
std::string path_;
std::string url_;
EMSCRIPTEN_WEBSOCKET_T socket_ = 0;
bool connected_ = false;
bool connectionFailed_ = false;
bool closing_ = false;
std::vector<std::string> messages_;
#else
Impl(std::string host, std::string port, std::string path)
: resolver_(io_)
, socket_(io_)
@@ -149,6 +269,7 @@ private:
std::mutex messagesMutex_;
std::vector<std::string> messages_;
std::deque<std::string> outbound_;
#endif
};
WebsocketGameClient::WebsocketGameClient(
@@ -296,12 +417,12 @@ void WebsocketGameClient::update(std::int64_t)
if (!tick.stateHash.empty()) {
nlohmann::json state;
replica_.save(state);
const std::string localHash = canonicalStateHash(std::move(state));
/*const std::string localHash = canonicalStateHash(std::move(state));
if (localHash != tick.stateHash) {
std::cerr << "[network] Lockstep divergence at tick "
<< tick.tickId << ": server=" << tick.stateHash
<< ", client=" << localHash << '\n';
}
}*/
}
++nextExpectedTick_;
}