♻️ Handle incomming messages

This commit is contained in:
Rune Harlyk
2026-01-03 12:49:48 +01:00
committed by nikguin04
parent c0c13754f4
commit fa332995f9
11 changed files with 603 additions and 632 deletions
+21 -61
View File
@@ -1,82 +1,42 @@
#include <communication/websocket_adapter.h>
#include <string>
static const char *TAG = "Websocket";
static const char* TAG = "Websocket";
Websocket::Websocket(PsychicHttpServer &server, const char *route) : _server(server), _route(route) {
_socket.onOpen((std::bind(&Websocket::onWSOpen, this, std::placeholders::_1)));
_socket.onClose(std::bind(&Websocket::onWSClose, this, std::placeholders::_1));
_socket.onFrame(std::bind(&Websocket::onFrame, this, std::placeholders::_1, std::placeholders::_2));
Websocket::Websocket(PsychicHttpServer& server, const char* route) : server_(server), route_(route) {
socket_.onOpen(std::bind(&Websocket::onWSOpen, this, std::placeholders::_1));
socket_.onClose(std::bind(&Websocket::onWSClose, this, std::placeholders::_1));
socket_.onFrame(std::bind(&Websocket::onFrame, this, std::placeholders::_1, std::placeholders::_2));
}
void Websocket::begin() { _server.on(_route, &_socket); }
void Websocket::begin() { server_.on(route_, &socket_); }
void Websocket::onEvent(std::string event, EventCallback callback) {
//CommAdapterBase::onEvent(std::move(event), std::move(callback));
void Websocket::onWSOpen(PsychicWebSocketClient* client) {
ESP_LOGI(TAG, "Client connected: %s [%u]", client->remoteIP().toString().c_str(), client->socket());
sendPong(client->socket());
}
void Websocket::emit(const char *event, JsonVariant &payload, const char *originId, bool onlyToSameOrigin) {
//CommAdapterBase::emit(event, payload, originId, onlyToSameOrigin);
void Websocket::onWSClose(PsychicWebSocketClient* client) {
ESP_LOGI(TAG, "Client disconnected: %s [%u]", client->remoteIP().toString().c_str(), client->socket());
removeClient(client->socket());
}
void Websocket::onWSOpen(PsychicWebSocketClient *client) {
ESP_LOGI("EventSocket", "ws[%s][%u] connect", client->remoteIP().toString().c_str(), client->socket());
ping(client->socket());
}
void Websocket::onWSClose(PsychicWebSocketClient *client) {
xSemaphoreTake(mutex_, portMAX_DELAY);
for (auto &event_subscriptions : client_subscriptions) {
event_subscriptions.second.remove(client->socket());
esp_err_t Websocket::onFrame(PsychicWebSocketRequest* request, httpd_ws_frame* frame) {
if (frame->type != HTTPD_WS_TYPE_BINARY) {
ESP_LOGW(TAG, "Expected binary frame, got type %d", frame->type);
return ESP_OK;
}
xSemaphoreGive(mutex_);
ESP_LOGI("EventSocket", "ws[%s][%u] disconnect", client->remoteIP().toString().c_str(), client->socket());
}
esp_err_t Websocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
// ESP_LOGV(TAG, "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
// request->client()->socket(), frame->type);
// if (frame->type != HTTPD_WS_TYPE_TEXT && frame->type != HTTPD_WS_TYPE_BINARY) {
// ESP_LOGE(TAG, "Unsupported frame type: %d", frame->type);
// return ESP_OK;
// }
// #if USE_MSGPACK
// if (frame->type == HTTPD_WS_TYPE_BINARY) {
// handleIncoming(frame->payload, frame->len, request->client()->socket());
// } else {
// ESP_LOGE(TAG, "Expected binary, got text");
// }
// #else
// if (frame->type == HTTPD_WS_TYPE_TEXT) {
// handleIncoming(frame->payload, frame->len, request->client()->socket());
// } else {
// ESP_LOGE(TAG, "Expected text, got binary");
// }
// #endif
handleIncoming(frame->payload, frame->len, request->client()->socket());
return ESP_OK;
}
void Websocket::send(const uint8_t *data, size_t len, int cid) {
if (cid != -1) {
auto *client = _socket.getClient(cid);
void Websocket::send(const uint8_t* data, size_t len, int cid) {
if (cid >= 0) {
auto* client = socket_.getClient(cid);
if (client) {
ESP_LOGV(TAG, "Sending to client %s: %s", client->remoteIP().toString().c_str(), data);
#if USE_MSGPACK
client->sendMessage(HTTPD_WS_TYPE_BINARY, data, len);
#else
client->sendMessage(HTTPD_WS_TYPE_TEXT, data, len);
#endif
}
} else {
ESP_LOGV(TAG, "Sending to all clients: %s", data);
#if USE_MSGPACK
_socket.sendAll(HTTPD_WS_TYPE_BINARY, data, len);
#else
_socket.sendAll(HTTPD_WS_TYPE_TEXT, data, len);
#endif
socket_.sendAll(HTTPD_WS_TYPE_BINARY, data, len);
}
}