Skip to content

Commit 3b44dd7

Browse files
author
zhangwh
committed
Merge pull request #72 from andreamazz/master
Fix delegate timing issue when using SSL
2 parents a733796 + ca35b03 commit 3b44dd7

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Source/CocoaMQTT.swift

+9-8
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ public class CocoaMQTT: NSObject, CocoaMQTTClient, GCDAsyncSocketDelegate, Cocoa
274274
socket!.writeData(NSData(bytes: data, length: data.count), withTimeout: -1, tag: tag)
275275
}
276276

277+
func sendConnectFrame() {
278+
let frame = CocoaMQTTFrameConnect(client: self)
279+
send(frame)
280+
reader!.start()
281+
delegate?.mqtt(self, didConnect: host, port: Int(port))
282+
}
283+
277284
//AsyncSocket Delegate
278285

279286
public func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) {
@@ -295,12 +302,8 @@ public class CocoaMQTT: NSObject, CocoaMQTTClient, GCDAsyncSocketDelegate, Cocoa
295302
sock.startTLS([kCFStreamSSLPeerName: self.host])
296303
#endif
297304
} else {
298-
let frame = CocoaMQTTFrameConnect(client: self)
299-
send(frame)
300-
reader!.start()
305+
sendConnectFrame()
301306
}
302-
303-
delegate?.mqtt(self, didConnect: host, port: Int(port))
304307
}
305308

306309
public func socket(sock: GCDAsyncSocket!, didReceiveTrust trust: SecTrust!, completionHandler: ((Bool) -> Void)!) {
@@ -314,9 +317,7 @@ public class CocoaMQTT: NSObject, CocoaMQTTClient, GCDAsyncSocketDelegate, Cocoa
314317
#if DEBUG
315318
NSLog("CocoaMQTT: socketDidSecure")
316319
#endif
317-
let frame = CocoaMQTTFrameConnect(client: self)
318-
send(frame)
319-
reader!.start()
320+
sendConnectFrame()
320321
}
321322

322323
public func socket(sock: GCDAsyncSocket!, didWriteDataWithTag tag: Int) {

0 commit comments

Comments
 (0)