File tree 2 files changed +28
-3
lines changed
2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -105,7 +105,11 @@ MqttClient::~MqttClient()
105
105
}
106
106
}
107
107
108
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
109
+ void MqttClient::onMessage (MessageCallback callback)
110
+ #else
108
111
void MqttClient::onMessage (void (*callback)(int ))
112
+ #endif
109
113
{
110
114
_onMessage = callback;
111
115
}
@@ -562,7 +566,11 @@ void MqttClient::poll()
562
566
_rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
563
567
564
568
if (_onMessage) {
569
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
570
+ _onMessage (this ,_rxLength);
571
+ #else
565
572
_onMessage (_rxLength);
573
+ #endif
566
574
567
575
if (_rxLength == 0 ) {
568
576
_rxState = MQTT_CLIENT_RX_STATE_READ_TYPE;
@@ -585,7 +593,11 @@ void MqttClient::poll()
585
593
_rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
586
594
587
595
if (_onMessage) {
596
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
597
+ _onMessage (this ,_rxLength);
598
+ #else
588
599
_onMessage (_rxLength);
600
+ #endif
589
601
}
590
602
591
603
if (_rxLength == 0 ) {
Original file line number Diff line number Diff line change 32
32
#define MQTT_BAD_USER_NAME_OR_PASSWORD 4
33
33
#define MQTT_NOT_AUTHORIZED 5
34
34
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
+
35
42
class MqttClient : public Client {
36
43
public:
37
44
MqttClient (Client* client);
38
45
MqttClient (Client& client);
39
46
virtual ~MqttClient ();
40
47
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
42
52
inline void setClient (Client& client) { _client = &client; }
43
-
44
-
45
53
void onMessage (void (*)(int ));
54
+ #endif
46
55
47
56
int parseMessage ();
48
57
String messageTopic () const ;
@@ -134,7 +143,11 @@ class MqttClient : public Client {
134
143
private:
135
144
Client* _client;
136
145
146
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
147
+ MessageCallback _onMessage;
148
+ #else
137
149
void (*_onMessage)(int );
150
+ #endif
138
151
139
152
String _id;
140
153
String _username;
You can’t perform that action at this time.
0 commit comments