@@ -25,7 +25,6 @@ @implementation Mqtt
25
25
26
26
- (id )init {
27
27
if ((self = [super init ])) {
28
-
29
28
self.defaultOptions = @{
30
29
@" host" : @" localhost" ,
31
30
@" port" : @1883 ,
@@ -44,9 +43,9 @@ - (id)init {
44
43
@" willQos" : @0 ,
45
44
@" willRetainFlag" : @NO
46
45
};
47
-
46
+
48
47
}
49
-
48
+
50
49
return self;
51
50
}
52
51
@@ -60,70 +59,50 @@ - (instancetype) initWithBrigde:(RCTBridge *) bridge
60
59
for (NSString *key in options.keyEnumerator ) { // Replace default options
61
60
[self .options setValue: options[key] forKey: key];
62
61
}
62
+ self.manager = [[MQTTSessionManager alloc ] init ];
63
+ self.manager .delegate = self;
64
+
63
65
return self;
64
66
}
65
67
66
68
- (void ) connect {
67
- if (!self.manager ) {
68
- [[NSOperationQueue mainQueue ] addOperationWithBlock: ^ {
69
-
70
- self.manager = [[MQTTSessionManager alloc ] init ];
71
- self.manager .delegate = self;
72
- MQTTSSLSecurityPolicy *securityPolicy = nil ;
73
- if (self.options [@" tls" ]) {
74
- securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode: MQTTSSLPinningModeNone];
75
- securityPolicy.allowInvalidCertificates = YES ;
76
- }
77
-
78
-
79
- NSData *willMsg = nil ;
80
- if (self.options [@" willMsg" ] != [NSNull null ]) {
81
- willMsg = [self .options[@" willMsg" ] dataUsingEncoding: NSUTF8StringEncoding];
82
- }
83
- [self .manager connectTo: [self .options valueForKey: @" host" ]
84
- port: [self .options[@" port" ] intValue ]
85
- tls: [self .options[@" tls" ] boolValue ]
86
- keepalive: [self .options[@" keepalive" ] intValue ]
87
- clean: [self .options[@" clean" ] intValue ]
88
- auth: [self .options[@" auth" ] boolValue ]
89
- user: [self .options valueForKey: @" user" ]
90
- pass: [self .options valueForKey: @" pass" ]
91
- will: [self .options[@" will" ] boolValue ]
92
- willTopic: [self .options valueForKey: @" willTopic" ]
93
- willMsg: willMsg
94
- willQos: (MQTTQosLevel)[self .options[@" willQos" ] intValue ]
95
- willRetainFlag: [self .options[@" willRetainFlag" ] boolValue ]
96
- withClientId: [self .options valueForKey: @" clientId" ]
97
- securityPolicy: securityPolicy
98
- certificates: nil
99
- ];
100
-
101
- [self .manager addObserver: self
102
- forKeyPath: @" state"
103
- options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
104
- context: nil ];
105
-
106
- }];
107
-
108
-
109
- } else {
110
-
111
- [self .manager connectToLast ];
112
-
69
+ MQTTSSLSecurityPolicy *securityPolicy = nil ;
70
+ if (self.options [@" tls" ]) {
71
+ securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode: MQTTSSLPinningModeNone];
72
+ securityPolicy.allowInvalidCertificates = YES ;
113
73
}
114
- }
115
74
75
+ NSData *willMsg = nil ;
76
+ if (self.options [@" willMsg" ] != [NSNull null ]) {
77
+ willMsg = [self .options[@" willMsg" ] dataUsingEncoding: NSUTF8StringEncoding];
78
+ }
79
+ [self .manager connectTo: [self .options valueForKey: @" host" ]
80
+ port: [self .options[@" port" ] intValue ]
81
+ tls: [self .options[@" tls" ] boolValue ]
82
+ keepalive: [self .options[@" keepalive" ] intValue ]
83
+ clean: [self .options[@" clean" ] intValue ]
84
+ auth: [self .options[@" auth" ] boolValue ]
85
+ user: [self .options valueForKey: @" user" ]
86
+ pass: [self .options valueForKey: @" pass" ]
87
+ will: [self .options[@" will" ] boolValue ]
88
+ willTopic: [self .options valueForKey: @" willTopic" ]
89
+ willMsg: willMsg
90
+ willQos: (MQTTQosLevel)[self .options[@" willQos" ] intValue ]
91
+ willRetainFlag: [self .options[@" willRetainFlag" ] boolValue ]
92
+ withClientId: [self .options valueForKey: @" clientId" ]
93
+ securityPolicy: securityPolicy
94
+ certificates: nil
95
+ ];
96
+ }
116
97
117
- - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
118
-
119
- switch (self.manager .state ) {
98
+ - (void )sessionManager : (MQTTSessionManager *)sessonManager didChangeState : (MQTTSessionManagerState)newState {
99
+ switch (newState) {
120
100
case MQTTSessionManagerStateClosed:
121
101
[self .bridge.eventDispatcher sendDeviceEventWithName: @" mqtt_events"
122
102
body: @{@" event" : @" closed" ,
123
103
@" clientRef" : [NSNumber numberWithInt: [self clientRef ]],
124
104
@" message" : @" closed"
125
105
}];
126
-
127
106
break ;
128
107
case MQTTSessionManagerStateClosing:
129
108
[self .bridge.eventDispatcher sendDeviceEventWithName: @" mqtt_events"
@@ -165,25 +144,22 @@ - (void) disconnect {
165
144
}
166
145
167
146
- (void ) subscribe : (NSString *)topic qos : (NSNumber *)qos {
168
-
169
- [self .manager setSubscriptions: [NSDictionary dictionaryWithObject: qos forKey: topic]];
147
+ NSMutableDictionary *subscriptions = [self .manager.subscriptions mutableCopy ];
148
+ [subscriptions setObject: qos forKey: topic];
149
+ [self .manager setSubscriptions: subscriptions];
150
+ }
151
+
152
+ - (void ) unsubscribe : (NSString *)topic {
153
+ NSMutableDictionary *subscriptions = [self .manager.subscriptions mutableCopy ];
154
+ [subscriptions removeObjectForKey: topic];
155
+ [self .manager setSubscriptions: subscriptions];
170
156
}
171
157
172
158
- (void ) publish : (NSString *) topic data : (NSData *)data qos : (NSNumber *)qos retain : (BOOL ) retain {
173
- [self .manager sendData: data
174
- topic: topic
175
- qos: [qos intValue ]
176
- retain: retain];
177
- // [data dataUsingEncoding:NSUTF8StringEncoding]
159
+ [self .manager sendData: data topic: topic qos: [qos intValue ] retain: retain];
178
160
}
179
- /*
180
- * MQTTSessionManagerDelegate
181
- */
161
+
182
162
- (void )handleMessage : (NSData *)data onTopic : (NSString *)topic retained : (BOOL )retained {
183
- /*
184
- * MQTTClient: process received message
185
- */
186
-
187
163
NSString *dataString = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
188
164
// RCTLogInfo(@" %@ : %@", topic, dataString);
189
165
[self .bridge.eventDispatcher sendDeviceEventWithName: @" mqtt_events"
@@ -196,28 +172,13 @@ - (void)handleMessage:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)re
196
172
@" retain" : [NSNumber numberWithBool: retained]
197
173
}
198
174
}];
199
-
175
+
200
176
}
201
177
202
178
203
179
- (void )dealloc
204
180
{
205
181
[self disconnect ];
206
- @try {
207
-
208
- @try {
209
-
210
- [[NSNotificationCenter defaultCenter ] removeObserver: self ];
211
- [self .manager removeObserver: self forKeyPath: @" state" ];
212
- [[NSOperationQueue mainQueue ] cancelAllOperations ];
213
- }@catch (id anException){
214
- // do nothing, obviously it wasn't attached because an exception was thrown
215
- }
216
- }
217
- @catch (NSException *exception) {
218
-
219
- }
220
-
221
182
}
222
183
223
184
0 commit comments