File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -97,10 +97,17 @@ MqttClient::~MqttClient()
97
97
}
98
98
}
99
99
100
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
101
+ void MqttClient::onMessage (MqttMessageCallback callback)
102
+ {
103
+ _onMessage = callback;
104
+ }
105
+ #else
100
106
void MqttClient::onMessage (void (*callback)(int ))
101
107
{
102
108
_onMessage = callback;
103
109
}
110
+ #endif
104
111
105
112
int MqttClient::parseMessage ()
106
113
{
@@ -550,7 +557,11 @@ void MqttClient::poll()
550
557
_rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
551
558
552
559
if (_onMessage) {
560
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
561
+ _onMessage (this ,_rxLength);
562
+ #else
553
563
_onMessage (_rxLength);
564
+ #endif
554
565
555
566
if (_rxLength == 0 ) {
556
567
_rxState = MQTT_CLIENT_RX_STATE_READ_TYPE;
@@ -573,7 +584,11 @@ void MqttClient::poll()
573
584
_rxState = MQTT_CLIENT_RX_STATE_READ_PUBLISH_PAYLOAD;
574
585
575
586
if (_onMessage) {
587
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
588
+ _onMessage (this ,_rxLength);
589
+ #else
576
590
_onMessage (_rxLength);
591
+ #endif
577
592
}
578
593
579
594
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
virtual ~MqttClient ();
39
46
47
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
48
+ typedef std::function<void (MqttClient *client, int messageSize)> MessageCallback;
49
+ void onMessage (MessageCallback callback);
50
+ #else
40
51
void onMessage (void (*)(int ));
52
+ #endif
41
53
42
54
int parseMessage ();
43
55
String messageTopic () const ;
@@ -128,7 +140,11 @@ class MqttClient : public Client {
128
140
private:
129
141
Client* _client;
130
142
143
+ #ifdef MQTT_CLIENT_STD_FUNCTION_CALLBACK
144
+ MqttClient::MessageCallback _onMessage;
145
+ #else
131
146
void (*_onMessage)(int );
147
+ #endif
132
148
133
149
String _id;
134
150
String _username;
You can’t perform that action at this time.
0 commit comments