File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -105,7 +105,11 @@ MqttClient::~MqttClient()
105105 }
106106}
107107
108+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
109+ void MqttClient::onMessage (MessageCallback callback)
110+ #else
108111void MqttClient::onMessage (void (*callback)(int ))
112+ #endif
109113{
110114 _onMessage = callback;
111115}
@@ -562,7 +566,11 @@ void MqttClient::poll()
562566 _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
563567
564568 if (_onMessage) {
569+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
570+ _onMessage (this ,_rxLength);
571+ #else
565572 _onMessage (_rxLength);
573+ #endif
566574
567575 if (_rxLength == 0 ) {
568576 _rxState = MQTT_CLIENT_RX_STATE_READ_TYPE;
@@ -585,7 +593,11 @@ void MqttClient::poll()
585593 _rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
586594
587595 if (_onMessage) {
596+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
597+ _onMessage (this ,_rxLength);
598+ #else
588599 _onMessage (_rxLength);
600+ #endif
589601 }
590602
591603 if (_rxLength == 0 ) {
Original file line number Diff line number Diff line change 3232#define MQTT_BAD_USER_NAME_OR_PASSWORD 4
3333#define MQTT_NOT_AUTHORIZED 5
3434
35+ // Make this definition in your application code to use std::functions for onMessage callbacks instead of C-pointers:
36+ // #define MQTT_CLIENT_STD_FUNCTION_CALLBACK
37+
38+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
39+ #include < functional>
40+ #endif
41+
3542class MqttClient : public Client {
3643public:
3744 MqttClient (Client* client);
3845 MqttClient (Client& client);
3946 virtual ~MqttClient ();
4047
41-
48+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
49+ typedef std::function<void (MqttClient *client, int messageSize)> MessageCallback;
50+ void onMessage (MessageCallback callback);
51+ #else
4252 inline void setClient (Client& client) { _client = &client; }
43-
44-
4553 void onMessage (void (*)(int ));
54+ #endif
4655
4756 int parseMessage ();
4857 String messageTopic () const ;
@@ -134,7 +143,11 @@ class MqttClient : public Client {
134143private:
135144 Client* _client;
136145
146+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
147+ MessageCallback _onMessage;
148+ #else
137149 void (*_onMessage)(int );
150+ #endif
138151
139152 String _id;
140153 String _username;
You can’t perform that action at this time.
0 commit comments