Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 4, 2024
1 parent 3a53c62 commit cb16ade
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
"-s PTHREAD_POOL_SIZE=4"
"-s INITIAL_MEMORY=128MB"
"-l websocket.js"
"-s WEBSOCKET_SUBPROTOCOL=text"
"-s EXPORTED_RUNTIME_METHODS=['callMain']"
# Debugging
#"-s ASSERTIONS=2"
Expand Down
1 change: 0 additions & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "statemanager.hpp"
#include "window.hpp"
#include "world.hpp"
#include <ranges>

using namespace framework;

Expand Down
24 changes: 24 additions & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "event.hpp"
#include "io.hpp"
#include "label.hpp"
#include "loopable.hpp"
#include "point.hpp"
#include "postalservice.hpp"
#include "socketio.hpp"
Expand All @@ -28,6 +29,23 @@ sol::table require(sol::state &lua, std::string_view module) {
return result.get<sol::table>();
}

class lua_loopable : public loopable {
public:
explicit lua_loopable(sol::function function)
: _function(std::move(function)) {}

virtual ~lua_loopable() = default;

void loop(float_t delta) noexcept override {
UNUSED(delta);

_function();
}

private:
sol::function _function;
};

void scriptengine::run() {
sol::state lua;

Expand Down Expand Up @@ -352,4 +370,10 @@ void scriptengine::run() {
const auto script = storage::io::read("scripts/main.lua");

lua.script(std::string_view(reinterpret_cast<const char *>(script.data()), script.size()));

lua["setup"]();
const auto loop = lua["loop"].get<sol::function>();
const auto engine = lua["engine"].get<framework::engine *>();
engine->add_loopable(std::make_shared<lua_loopable>(std::move(loop)));
lua["run"]();
}
29 changes: 17 additions & 12 deletions src/socketio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ void socketio::connect() {
}

const auto *self = static_cast<socketio *>(data);

const auto message = std::string(reinterpret_cast<const char *>(event->data), event->numBytes - 1);
std::cout << "On Message "
<< "'" << message << "'"
<< " Size " << message.size() << std::endl;

static const std::string zero = "0";
if (message.starts_with(zero)) {
self->send(zero);
}
// static const std::string zero = "0";
// if (message.starts_with(zero)) {
// self->send(zero);
// }

static const std::string ping = "2";
static const std::string pong = "3";
static const std::string ping = "P";
static const std::string pong = "Q";
if (message == ping) {
self->send(pong);
}
Expand Down Expand Up @@ -74,7 +76,8 @@ void socketio::connect() {
return EMSCRIPTEN_RESULT_SUCCESS;
});

emscripten_websocket_set_onclose_callback(_socket, this, [](int, const EmscriptenWebSocketCloseEvent *, void *data) {
emscripten_websocket_set_onclose_callback(_socket, this, [](int, const EmscriptenWebSocketCloseEvent *event, void *data) {
UNUSED(event);
const auto *self = static_cast<socketio *>(data);
fmt::print("Disconnected from {}\n", self->_url);
self->invoke("disconnect");
Expand All @@ -94,15 +97,17 @@ void socketio::disconnect() {
}
}

void socketio::emit(const std::string &event, const std::string &data) {
send(fmt::format("42[\"{}\",\"{}\"]", event, data));
void socketio::emit(const std::string &topic, const std::string &data) {
send(fmt::format("42[\"{}\",\"{}\"]", topic, data));
}

void socketio::on(const std::string &event, EventCallback callback) {
_callbacks[event].push_back(std::move(callback));
void socketio::on(const std::string &topic, EventCallback callback) {
// send subscribe command
_callbacks[topic].push_back(std::move(callback));
}

void socketio::send(const std::string &message) const {
std::cout << "Send " << message << std::endl;
if (_socket) {
emscripten_websocket_send_utf8_text(_socket, message.c_str());
}
Expand Down
4 changes: 2 additions & 2 deletions src/socketio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class socketio {

void connect();
void disconnect();
void emit(const std::string &event, const std::string &data);
void on(const std::string &event, EventCallback callback);
void emit(const std::string &topic, const std::string &data);
void on(const std::string &topic, EventCallback callback);

private:
void send(const std::string &message) const;
Expand Down

0 comments on commit cb16ade

Please sign in to comment.