Skip to content

Commit

Permalink
use proper method to send websocket data outside of response. fixes #85
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeken committed Aug 11, 2024
1 parent 7c2fa90 commit cc81fa1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/PsychicWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ PsychicWebSocketClient::~PsychicWebSocketClient()
{
}

void PsychicWebSocketClient::_sendMessageCallback(esp_err_t err, int socket, void* arg)
{
// PsychicWebSocketClient* client = (PsychicWebSocketClient*)arg;

if (err == ESP_OK)
return;
else if (err == ESP_FAIL)
ESP_LOGE(PH_TAG, "Websocket: send - socket error (#%d)", socket);
else if (err == ESP_ERR_INVALID_STATE)
ESP_LOGE(PH_TAG, "Websocket: Handshake was already done beforehand (#%d)", socket);
else if (err == ESP_ERR_INVALID_ARG)
ESP_LOGE(PH_TAG, "Websocket: Argument is invalid (null or non-WebSocket) (#%d)", socket);
else
ESP_LOGE(PH_TAG, "Websocket: Send message unknown error. (#%d)", socket);
}

esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_frame_t* ws_pkt)
{
return httpd_ws_send_frame_async(this->server(), this->socket(), ws_pkt);
return httpd_ws_send_data_async(this->server(), this->socket(), ws_pkt, PsychicWebSocketClient::_sendMessageCallback, this);
}

esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_type_t op, const void* data, size_t len)
Expand Down
3 changes: 3 additions & 0 deletions src/PsychicWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ typedef std::function<esp_err_t(PsychicWebSocketRequest* request, httpd_ws_frame

class PsychicWebSocketClient : public PsychicClient
{
protected:
static void _sendMessageCallback(esp_err_t err, int socket, void* arg);

public:
PsychicWebSocketClient(PsychicClient* client);
~PsychicWebSocketClient();
Expand Down

0 comments on commit cc81fa1

Please sign in to comment.