Skip to content

Commit 9c390ec

Browse files
committed
change the way maintain CocoaMQTT.socket. set delegate before connect and remove delegate in socketDidDisconnect(). Update pod spec version
1 parent 3d11f03 commit 9c390ec

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

CocoaMQTT.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "CocoaMQTT"
3-
s.version = "1.0.15"
3+
s.version = "1.0.16"
44
s.summary = "MQTT v3.1.1 client library for iOS and OS X written with Swift 3"
55
s.homepage = "https://github.com/emqtt/CocoaMQTT"
66
s.license = { :type => "MIT" }
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
1111
s.ios.deployment_target = "8.0"
1212
s.tvos.deployment_target = "9.0"
1313
# s.watchos.deployment_target = "2.0"
14-
s.source = { :git => "https://github.com/emqtt/CocoaMQTT.git", :tag => "1.0.15"}
14+
s.source = { :git => "https://github.com/emqtt/CocoaMQTT.git", :tag => "1.0.16"}
1515
s.source_files = "Source/{*.h}", "Source/*.swift"
1616
s.dependency "CocoaAsyncSocket", "~> 7.5.1"
1717
s.dependency "SwiftyTimer", "~> 2.0.0"

Source/CocoaMQTT.swift

+35-34
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ protocol CocoaMQTTClient {
8585
var willMessage: CocoaMQTTWill? {get set}
8686

8787
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+
9091
func subscribe(_ topic: String, qos: CocoaMQTTQOS) -> UInt16
9192
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+
9496
}
9597

9698
/**
@@ -168,7 +170,7 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
168170

169171
// global message id
170172
var gmid: UInt16 = 1
171-
var socket: GCDAsyncSocket?
173+
var socket = GCDAsyncSocket()
172174
var reader: CocoaMQTTReader?
173175

174176

@@ -184,6 +186,9 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
184186
deinit {
185187
aliveTimer?.invalidate()
186188
autoReconnTimer?.invalidate()
189+
190+
socket.delegate = nil
191+
socket.disconnect()
187192
}
188193

189194
public func buffer(_ buffer: CocoaMQTTFrameBuffer, sendPublishFrame frame: CocoaMQTTFramePublish) {
@@ -192,10 +197,7 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
192197

193198
fileprivate func send(_ frame: CocoaMQTTFrame, tag: Int = 0) {
194199
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)
199201
}
200202

201203
fileprivate func sendConnectFrame() {
@@ -236,17 +238,36 @@ open class CocoaMQTT: NSObject, CocoaMQTTClient, CocoaMQTTFrameBufferProtocol {
236238

237239
@discardableResult
238240
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)
241243
do {
242-
try socket!.connect(toHost: self.host, onPort: self.port)
244+
try socket.connect(toHost: self.host, onPort: self.port)
243245
connState = .connecting
244246
return true
245247
} catch let error as NSError {
246248
printError("socket connect error: \(error.description)")
247249
return false
248250
}
249251
}
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+
}
250271

251272
@discardableResult
252273
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 {
293314
send(frame, tag: Int(msgid))
294315
return msgid
295316
}
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-
}
317317
}
318318

319319
// MARK: - GCDAsyncSocketDelegate
@@ -370,9 +370,10 @@ extension CocoaMQTT: GCDAsyncSocketDelegate {
370370
}
371371

372372
public func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
373+
socket.delegate = nil
373374
connState = .disconnected
374375
delegate?.mqttDidDisconnect(self, withError: err)
375-
376+
376377
autoReconnTimer?.invalidate()
377378
if !disconnectExpectedly && autoReconnect && autoReconnectTimeInterval > 0 {
378379
autoReconnTimer = Timer.every(Double(autoReconnectTimeInterval).seconds, { [weak self] (timer: Timer) in

0 commit comments

Comments
 (0)