Skip to content

Commit 0978b7d

Browse files
authored
Swift 4.1 Support (aciidgh#32)
* Optimize a few things. Create a public message structure to simplify and extend delegate. Add background queue connect convenience method. Have one delegate callback on disconnect. Push errors out to delegate. Rename SessionStream file. * Fix compiler errors in test and sample app. Add comments for OCI changes. * Make delegates weak references to avoid circular ownership Add BatchReceiver sketch * move MQTTMessage into models make optional completion blocks truly optional * Create MQTTBroker protocol * create simple MQTTBatchingSession (pass-thru right now) * MQTTConnectParams work in a framework * fix bug when sending data in a disconnected state * simplify life-cycle on connection errors * Fix stream ready flow * move all mqttparsing into session * move packet parsing out of session * Correct queue usage * forgot the else * Retry logic complete (for now) * don’t reuse time intervals * Add DatedSnapShot * message id change * DataSnapshot sendAll * fix dated snapshot * fix dated snapshot again * Allow data snapshot to convert messages * Fix read loop * Swift 4 compatible with 3 changes. * Add CustomStringConvertable to message, prototype file payload enum, make more structs * cleanup lifecycle * Break up networkPacket method * move stream packet logic into Streamable * bug fix * Update README.md * better session handling * Update .travis.yml * Add swift_version in podspec * Bump podspec versions * Fix typo * Keep payload as Data to minimize changes to public interface * Remove inline change comments * Make keep alive timer optional * Update deprecated string count * Update deprecated timer method * Update readme * Cleanup whitespace * Update Example project * Fix typo * Update tests to ensure connection is made in setUp before running tests * Remove extraneous } from example project * Ensure callbacks are handled on the main thread * Remove new reconnecting session feature for now * Cleanup error handling * Add ping/heartbeat callback * Make error Equatable * Cleanup private/fileprivate * General formatting cleanup * Update tests and add end-to-end subscribe, publish, receive test * Remove new reconnecting session feature for now * Add description for errors * Remove Broker class * Don't dispatch to main thread as already on main by default * Expose desired dispatch queue for delegate callbacks, default to main * Fix bug with reading ping packets * Call completion blocks on delegate thread * Clear the delegate when deinit'd * Include framework as project dependency * Update closure error handling * Use description instead of localizedDescription * Update readme * Add Swift Package Manager support
1 parent e1e11b3 commit 0978b7d

30 files changed

+927
-447
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: objective-c
2-
osx_image: xcode8
2+
osx_image: xcode9.2
33
before_install:
44
- brew update
55
- brew install mosquitto
66
script:
77
- /usr/local/opt/mosquitto/sbin/mosquitto -d
8-
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator -destination "name=iPhone 6s" test
8+
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator -destination "name=iPhone 8 Plus" test

Package.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// swift-tools-version:4.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "SwiftMQTT",
7+
products: [
8+
.library(name: "SwiftMQTT", targets: ["SwiftMQTT"]),
9+
],
10+
dependencies: [],
11+
targets: [
12+
.target(
13+
name: "SwiftMQTT",
14+
dependencies: [],
15+
path: "SwiftMQTT/SwiftMQTT/"
16+
),
17+
.testTarget(
18+
name: "SwiftMQTTTests",
19+
dependencies: ["SwiftMQTT"],
20+
path: "SwiftMQTT/SwiftMQTTTests/"
21+
),
22+
]
23+
)
24+

README.md

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,98 @@
11
# SwiftMQTT
22

3-
MQTT Client in pure Swift ❤️️
3+
MQTT Client
44

55
[![Build Status](https://travis-ci.org/aciidb0mb3r/SwiftMQTT.svg)](https://travis-ci.org/aciidb0mb3r/SwiftMQTT)
66
[![Version](https://img.shields.io/cocoapods/v/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)
77
[![License](https://img.shields.io/cocoapods/l/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)
88

9-
## Info
10-
* Fully written in Swift from ground up
11-
* Closures everywhere 😃
12-
* Includes test cases and sample project
13-
* Based on MQTT Version 3.1.1 (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718043)
9+
* Fully written in Swift 4
10+
* Robust error handling
11+
* Performant
12+
* Based on [MQTT 3.1.1 Specification](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)
1413

15-
![Sample Project Screenshot](http://i.imgur.com/9lefVmVl.png)
16-
17-
## How to use
14+
## Usage
1815

1916
### Create Session
2017
```swift
21-
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
18+
mqttSession = MQTTSession(
19+
host: "localhost",
20+
port: 1883,
21+
clientID: "swift", // must be unique to the client
22+
cleanSession: true,
23+
keepAlive: 15,
24+
useSSL: false
25+
)
2226
```
2327

2428
### Connect
2529
```swift
26-
mqttSession.connect { (succeeded, error) -> Void in
27-
if succeeded {
28-
print("Connected!")
29-
}
30+
mqttSession.connect { error in
31+
if error == .none {
32+
print("Connected!")
33+
} else {
34+
print(error.description)
35+
}
3036
}
3137
```
3238

3339
### Subscribe
3440
```swift
35-
mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
36-
if succeeded {
37-
print("Subscribed!")
38-
}
41+
let topic = "mytopic"
42+
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
43+
if error == .none {
44+
print("Subscribed to \(topic)!")
45+
} else {
46+
print(error.description)
47+
}
3948
}
4049
```
4150

4251
### Unsubscribe
4352
```swift
44-
mqttSession.unSubscribe(from: ["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
45-
if succeeded {
46-
print("unSubscribed!")
47-
}
53+
let topic = "mytopic"
54+
mqttSession.unSubscribe(from: topic) { error in
55+
if error == .none {
56+
print("Unsubscribed from \(topic)!")
57+
} else {
58+
print(error.description)
59+
}
4860
}
4961
```
5062

5163
### Publish
52-
```swift
53-
let jsonDict = ["hey" : "sup"]
54-
let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)
5564

56-
mqttSession.publish(data, in: "/hey/wassap", delivering: .atLeastOnce, retain: false) { (succeeded, error) -> Void in
57-
if succeeded {
58-
print("Published!")
59-
}
65+
```swift
66+
let json = ["key" : "value"]
67+
let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
68+
let topic = "mytopic"
69+
70+
mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in
71+
if error == .none {
72+
print("Published data in \(topic)!")
73+
} else {
74+
print(error.description)
75+
}
6076
}
61-
6277
```
78+
6379
### Conform to `MQTTSessionDelegate` to receive messages
6480
```swift
6581
mqttSession.delegate = self
6682
```
6783
```swift
68-
func mqttSession(session: MQTTSession, received message: Data, in topic: String) {
69-
let string = String(data: message, encoding: .utf8)!
84+
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
85+
print(message.topic)
86+
print(message.stringRepresentation)
87+
}
88+
```
89+
```swift
90+
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
91+
if error == .none {
92+
print("Successfully disconnected from MQTT broker")
93+
} else {
94+
print(error.description)
95+
}
7096
}
7197
```
7298

@@ -77,9 +103,24 @@ func mqttSession(session: MQTTSession, received message: Data, in topic: String)
77103
Install using [CocoaPods](http://cocoapods.org) by adding the following lines to your Podfile:
78104

79105
````ruby
80-
use_frameworks!
81-
pod 'SwiftMQTT'
106+
target 'MyApp' do
107+
use_frameworks!
108+
pod 'SwiftMQTT'
109+
end
82110
````
83111

112+
### Carthage
113+
114+
```
115+
github "aciidb0mb3r/SwiftMQTT"
116+
```
117+
118+
### Swift Package Manager
119+
120+
```swift
121+
dependencies: [
122+
.package(url: "https://github.com/aciidb0mb3r/SwiftMQTT.git", from: "3.0.0")
123+
]
124+
```
84125
## License
85126
MIT

SwiftMQTT.podspec

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
Pod::Spec.new do |s|
33

44
s.name = "SwiftMQTT"
5-
s.version = "2.0.0"
5+
s.version = "3.0.0"
66
s.summary = "MQTT Client in pure Swift"
77
s.description = <<-DESC
8-
MQTT Client in Swift 3.0 based on MQTT Version 3.1.1
8+
MQTT Client in Swift 4.1 based on MQTT Version 3.1.1
99
DESC
1010

1111
s.homepage = "https://github.com/aciidb0mb3r/SwiftMQTT"
1212
s.license = { :type => "MIT", :file => "LICENSE" }
1313
s.author = { "Ankit Agarwal" => "[email protected]" }
1414
s.source = { :git => "https://github.com/aciidb0mb3r/SwiftMQTT.git", :tag => s.version.to_s }
1515

16+
s.swift_version = "4.1"
17+
1618
s.ios.deployment_target = "8.0"
1719
s.osx.deployment_target = "10.10"
1820
s.tvos.deployment_target = "9.0"

0 commit comments

Comments
 (0)