🪄 Formats EventSocket
This commit is contained in:
@@ -42,16 +42,11 @@ const char* getEventPayload(const char* msg) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
EventSocket::EventSocket(PsychicHttpServer *server,
|
||||
SecurityManager *securityManager,
|
||||
AuthenticationPredicate authenticationPredicate) : _server(server),
|
||||
_securityManager(securityManager),
|
||||
_authenticationPredicate(authenticationPredicate)
|
||||
{
|
||||
}
|
||||
EventSocket::EventSocket(PsychicHttpServer *server, SecurityManager *securityManager,
|
||||
AuthenticationPredicate authenticationPredicate)
|
||||
: _server(server), _securityManager(securityManager), _authenticationPredicate(authenticationPredicate) {}
|
||||
|
||||
void EventSocket::begin()
|
||||
{
|
||||
void EventSocket::begin() {
|
||||
_socket.setFilter(_securityManager->filterRequest(_authenticationPredicate));
|
||||
_socket.onOpen((std::bind(&EventSocket::onWSOpen, this, std::placeholders::_1)));
|
||||
_socket.onClose(std::bind(&EventSocket::onWSClose, this, std::placeholders::_1));
|
||||
@@ -61,27 +56,22 @@ void EventSocket::begin()
|
||||
ESP_LOGV("EventSocket", "Registered event socket endpoint: %s", EVENT_SERVICE_PATH);
|
||||
}
|
||||
|
||||
void EventSocket::onWSOpen(PsychicWebSocketClient *client)
|
||||
{
|
||||
void EventSocket::onWSOpen(PsychicWebSocketClient *client) {
|
||||
ESP_LOGI("EventSocket", "ws[%s][%u] connect", client->remoteIP().toString().c_str(), client->socket());
|
||||
}
|
||||
|
||||
void EventSocket::onWSClose(PsychicWebSocketClient *client)
|
||||
{
|
||||
for (auto &event_subscriptions : client_subscriptions)
|
||||
{
|
||||
void EventSocket::onWSClose(PsychicWebSocketClient *client) {
|
||||
for (auto &event_subscriptions : client_subscriptions) {
|
||||
event_subscriptions.second.remove(client->socket());
|
||||
}
|
||||
ESP_LOGI("EventSocket", "ws[%s][%u] disconnect", client->remoteIP().toString().c_str(), client->socket());
|
||||
}
|
||||
|
||||
esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame)
|
||||
{
|
||||
esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame *frame) {
|
||||
ESP_LOGV("EventSocket", "ws[%s][%u] opcode[%d]", request->client()->remoteIP().toString().c_str(),
|
||||
request->client()->socket(), frame->type);
|
||||
|
||||
if (frame->type != HTTPD_WS_TYPE_TEXT)
|
||||
{
|
||||
if (frame->type != HTTPD_WS_TYPE_TEXT) {
|
||||
ESP_LOGE("EventSocket", "Unsupported frame type");
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -133,13 +123,11 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void EventSocket::emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin)
|
||||
{
|
||||
void EventSocket::emit(const char *event, const char *payload, const char *originId, bool onlyToSameOrigin) {
|
||||
int originSubscriptionId = originId[0] ? atoi(originId) : -1;
|
||||
xSemaphoreTake(clientSubscriptionsMutex, portMAX_DELAY);
|
||||
auto &subscriptions = client_subscriptions[event];
|
||||
if (subscriptions.empty())
|
||||
{
|
||||
if (subscriptions.empty()) {
|
||||
xSemaphoreGive(clientSubscriptionsMutex);
|
||||
return;
|
||||
}
|
||||
@@ -147,58 +135,45 @@ void EventSocket::emit(const char *event, const char *payload, const char *origi
|
||||
snprintf(msg, sizeof(msg), "2/%s[%s]", event, payload);
|
||||
|
||||
// if onlyToSameOrigin == true, send the message back to the origin
|
||||
if (onlyToSameOrigin && originSubscriptionId > 0)
|
||||
{
|
||||
if (onlyToSameOrigin && originSubscriptionId > 0) {
|
||||
auto *client = _socket.getClient(originSubscriptionId);
|
||||
if (client)
|
||||
{
|
||||
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event, client->remoteIP().toString().c_str(), msg);
|
||||
if (client) {
|
||||
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event,
|
||||
client->remoteIP().toString().c_str(), msg);
|
||||
client->sendMessage(msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // else send the message to all other clients
|
||||
} else { // else send the message to all other clients
|
||||
|
||||
for (int subscription : client_subscriptions[event])
|
||||
{
|
||||
if (subscription == originSubscriptionId)
|
||||
continue;
|
||||
for (int subscription : client_subscriptions[event]) {
|
||||
if (subscription == originSubscriptionId) continue;
|
||||
auto *client = _socket.getClient(subscription);
|
||||
if (!client)
|
||||
{
|
||||
if (!client) {
|
||||
subscriptions.remove(subscription);
|
||||
continue;
|
||||
}
|
||||
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event, client->remoteIP().toString().c_str(), msg);
|
||||
ESP_LOGV("EventSocket", "Emitting event: %s to %s, Message: %s", event,
|
||||
client->remoteIP().toString().c_str(), msg);
|
||||
client->sendMessage(msg);
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(clientSubscriptionsMutex);
|
||||
}
|
||||
|
||||
void EventSocket::handleEventCallbacks(String event, JsonObject &jsonObject, int originId)
|
||||
{
|
||||
for (auto &callback : event_callbacks[event])
|
||||
{
|
||||
void EventSocket::handleEventCallbacks(String event, JsonObject &jsonObject, int originId) {
|
||||
for (auto &callback : event_callbacks[event]) {
|
||||
callback(jsonObject, originId);
|
||||
}
|
||||
}
|
||||
|
||||
void EventSocket::handleSubscribeCallbacks(String event, const String &originId)
|
||||
{
|
||||
for (auto &callback : subscribe_callbacks[event])
|
||||
{
|
||||
void EventSocket::handleSubscribeCallbacks(String event, const String &originId) {
|
||||
for (auto &callback : subscribe_callbacks[event]) {
|
||||
callback(originId, true);
|
||||
}
|
||||
}
|
||||
|
||||
void EventSocket::onEvent(String event, EventCallback callback)
|
||||
{
|
||||
event_callbacks[event].push_back(callback);
|
||||
}
|
||||
void EventSocket::onEvent(String event, EventCallback callback) { event_callbacks[event].push_back(callback); }
|
||||
|
||||
void EventSocket::onSubscribe(String event, SubscribeCallback callback)
|
||||
{
|
||||
void EventSocket::onSubscribe(String event, SubscribeCallback callback) {
|
||||
subscribe_callbacks[event].push_back(callback);
|
||||
ESP_LOGD("EventSocket", "onSubscribe for event: %s", event.c_str());
|
||||
}
|
||||
@@ -10,22 +10,15 @@
|
||||
|
||||
#define EVENT_SERVICE_PATH "/ws/events"
|
||||
|
||||
enum message_type_t {
|
||||
CONNECT = 0,
|
||||
DISCONNECT = 1,
|
||||
EVENT = 2,
|
||||
PING = 3,
|
||||
PONG = 4,
|
||||
BINARY_EVENT = 5
|
||||
};
|
||||
enum message_type_t { CONNECT = 0, DISCONNECT = 1, EVENT = 2, PING = 3, PONG = 4, BINARY_EVENT = 5 };
|
||||
|
||||
typedef std::function<void(JsonObject &root, int originId)> EventCallback;
|
||||
typedef std::function<void(const String &originId, bool sync)> SubscribeCallback;
|
||||
|
||||
class EventSocket
|
||||
{
|
||||
class EventSocket {
|
||||
public:
|
||||
EventSocket(PsychicHttpServer *server, SecurityManager *_securityManager, AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_AUTHENTICATED);
|
||||
EventSocket(PsychicHttpServer *server, SecurityManager *_securityManager,
|
||||
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_AUTHENTICATED);
|
||||
|
||||
void begin();
|
||||
|
||||
@@ -34,7 +27,8 @@ public:
|
||||
void onSubscribe(String event, SubscribeCallback callback);
|
||||
|
||||
void emit(const char *event, const char *payload, const char *originId = "", bool onlyToSameOrigin = false);
|
||||
// if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to all clients except the originId
|
||||
// if onlyToSameOrigin == true, the message will be sent to the originId only, otherwise it will be broadcasted to
|
||||
// all clients except the originId
|
||||
|
||||
private:
|
||||
PsychicHttpServer *_server;
|
||||
|
||||
Reference in New Issue
Block a user