@@ -85,12 +85,14 @@ protocol CocoaMQTTClient {
85
85
var willMessage : CocoaMQTTWill ? { get set }
86
86
87
87
func connect( ) -> Bool
88
- func publish( _ topic: String , withString string: String , qos: CocoaMQTTQOS , retained: Bool , dup: Bool ) -> UInt16
89
- func publish( _ message: CocoaMQTTMessage ) -> UInt16
88
+ func disconnect( )
89
+ func ping( )
90
+
90
91
func subscribe( _ topic: String , qos: CocoaMQTTQOS ) -> UInt16
91
92
func unsubscribe( _ topic: String ) -> UInt16
92
- func ping( )
93
- func disconnect( )
93
+ func publish( _ topic: String , withString string: String , qos: CocoaMQTTQOS , retained: Bool , dup: Bool ) -> UInt16
94
+ func publish( _ message: CocoaMQTTMessage ) -> UInt16
95
+
94
96
}
95
97
96
98
/**
@@ -168,7 +170,7 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
168
170
169
171
// global message id
170
172
var gmid : UInt16 = 1
171
- var socket : GCDAsyncSocket ?
173
+ var socket = GCDAsyncSocket ( )
172
174
var reader : CocoaMQTTReader ?
173
175
174
176
@@ -184,6 +186,9 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
184
186
deinit {
185
187
aliveTimer? . invalidate ( )
186
188
autoReconnTimer? . invalidate ( )
189
+
190
+ socket. delegate = nil
191
+ socket. disconnect ( )
187
192
}
188
193
189
194
public func buffer( _ buffer: CocoaMQTTFrameBuffer , sendPublishFrame frame: CocoaMQTTFramePublish ) {
@@ -192,10 +197,7 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
192
197
193
198
fileprivate func send( _ frame: CocoaMQTTFrame , tag: Int = 0 ) {
194
199
let data = frame. data ( )
195
- guard socket != nil else {
196
- return
197
- }
198
- socket!. write ( Data ( bytes: data, count: data. count) , withTimeout: - 1 , tag: tag)
200
+ socket. write ( Data ( bytes: data, count: data. count) , withTimeout: - 1 , tag: tag)
199
201
}
200
202
201
203
fileprivate func sendConnectFrame( ) {
@@ -236,17 +238,36 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
236
238
237
239
@discardableResult
238
240
open func connect( ) -> Bool {
239
- socket = GCDAsyncSocket ( delegate : self , delegateQueue: dispatchQueue)
240
- reader = CocoaMQTTReader ( socket: socket! , delegate: self )
241
+ socket. setDelegate ( self , delegateQueue: dispatchQueue)
242
+ reader = CocoaMQTTReader ( socket: socket, delegate: self )
241
243
do {
242
- try socket! . connect ( toHost: self . host, onPort: self . port)
244
+ try socket. connect ( toHost: self . host, onPort: self . port)
243
245
connState = . connecting
244
246
return true
245
247
} catch let error as NSError {
246
248
printError ( " socket connect error: \( error. description) " )
247
249
return false
248
250
}
249
251
}
252
+
253
+ /// Only can be called from outside. If you want to disconnect from inside framwork, call internal_disconnect()
254
+ /// disconnect expectedly
255
+ open func disconnect( ) {
256
+ disconnectExpectedly = true
257
+ internal_disconnect ( )
258
+ }
259
+
260
+ /// disconnect unexpectedly
261
+ open func internal_disconnect( ) {
262
+ send ( CocoaMQTTFrame ( type: CocoaMQTTFrameType . disconnect) , tag: - 0xE0 )
263
+ socket. disconnect ( )
264
+ }
265
+
266
+ open func ping( ) {
267
+ printDebug ( " ping " )
268
+ send ( CocoaMQTTFrame ( type: CocoaMQTTFrameType . pingreq) , tag: - 0xC0 )
269
+ self . delegate? . mqttDidPing ( self )
270
+ }
250
271
251
272
@discardableResult
252
273
open func publish( _ topic: String , withString string: String , qos: CocoaMQTTQOS = . qos1, retained: Bool = false , dup: Bool = false ) -> UInt16 {
@@ -293,27 +314,6 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
293
314
send ( frame, tag: Int ( msgid) )
294
315
return msgid
295
316
}
296
-
297
- open func ping( ) {
298
- printDebug ( " ping " )
299
- send ( CocoaMQTTFrame ( type: CocoaMQTTFrameType . pingreq) , tag: - 0xC0 )
300
- self . delegate? . mqttDidPing ( self )
301
- }
302
-
303
- /// Only can be called from outside. If you want to disconnect from inside framwork, call internal_disconnect()
304
- /// disconnect expectedly
305
- open func disconnect( ) {
306
- disconnectExpectedly = true
307
- internal_disconnect ( )
308
- }
309
-
310
- /// disconnect unexpectedly
311
- open func internal_disconnect( ) {
312
- send ( CocoaMQTTFrame ( type: CocoaMQTTFrameType . disconnect) , tag: - 0xE0 )
313
- socket? . delegate = nil
314
- socket!. disconnect ( )
315
- socket = nil
316
- }
317
317
}
318
318
319
319
// MARK: - GCDAsyncSocketDelegate
@@ -370,9 +370,10 @@ extension CocoaMQTT: GCDAsyncSocketDelegate {
370
370
}
371
371
372
372
public func socketDidDisconnect( _ sock: GCDAsyncSocket , withError err: Error ? ) {
373
+ socket. delegate = nil
373
374
connState = . disconnected
374
375
delegate? . mqttDidDisconnect ( self , withError: err)
375
-
376
+
376
377
autoReconnTimer? . invalidate ( )
377
378
if !disconnectExpectedly && autoReconnect && autoReconnectTimeInterval > 0 {
378
379
autoReconnTimer = Timer . every ( Double ( autoReconnectTimeInterval) . seconds, { [ weak self] ( timer: Timer ) in
0 commit comments