Skip to content

Commit

Permalink
Swift 4.1 Support (#32)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
adolfo authored Aug 17, 2018
1 parent e1e11b3 commit 0978b7d
Show file tree
Hide file tree
Showing 30 changed files with 927 additions and 447 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: objective-c
osx_image: xcode8
osx_image: xcode9.2
before_install:
- brew update
- brew install mosquitto
script:
- /usr/local/opt/mosquitto/sbin/mosquitto -d
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator -destination "name=iPhone 6s" test
- xcodebuild -project SwiftMQTT/SwiftMQTT.xcodeproj -scheme SwiftMQTT -sdk iphonesimulator -destination "name=iPhone 8 Plus" test
24 changes: 24 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version:4.0

import PackageDescription

let package = Package(
name: "SwiftMQTT",
products: [
.library(name: "SwiftMQTT", targets: ["SwiftMQTT"]),
],
dependencies: [],
targets: [
.target(
name: "SwiftMQTT",
dependencies: [],
path: "SwiftMQTT/SwiftMQTT/"
),
.testTarget(
name: "SwiftMQTTTests",
dependencies: ["SwiftMQTT"],
path: "SwiftMQTT/SwiftMQTTTests/"
),
]
)

109 changes: 75 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,98 @@
# SwiftMQTT

MQTT Client in pure Swift ❤️️
MQTT Client

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

## Info
* Fully written in Swift from ground up
* Closures everywhere 😃
* Includes test cases and sample project
* 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)
* Fully written in Swift 4
* Robust error handling
* Performant
* Based on [MQTT 3.1.1 Specification](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)

![Sample Project Screenshot](http://i.imgur.com/9lefVmVl.png)

## How to use
## Usage

### Create Session
```swift
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
mqttSession = MQTTSession(
host: "localhost",
port: 1883,
clientID: "swift", // must be unique to the client
cleanSession: true,
keepAlive: 15,
useSSL: false
)
```

### Connect
```swift
mqttSession.connect { (succeeded, error) -> Void in
if succeeded {
print("Connected!")
}
mqttSession.connect { error in
if error == .none {
print("Connected!")
} else {
print(error.description)
}
}
```

### Subscribe
```swift
mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
if succeeded {
print("Subscribed!")
}
let topic = "mytopic"
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
if error == .none {
print("Subscribed to \(topic)!")
} else {
print(error.description)
}
}
```

### Unsubscribe
```swift
mqttSession.unSubscribe(from: ["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
if succeeded {
print("unSubscribed!")
}
let topic = "mytopic"
mqttSession.unSubscribe(from: topic) { error in
if error == .none {
print("Unsubscribed from \(topic)!")
} else {
print(error.description)
}
}
```

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

mqttSession.publish(data, in: "/hey/wassap", delivering: .atLeastOnce, retain: false) { (succeeded, error) -> Void in
if succeeded {
print("Published!")
}
```swift
let json = ["key" : "value"]
let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let topic = "mytopic"

mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in
if error == .none {
print("Published data in \(topic)!")
} else {
print(error.description)
}
}

```

### Conform to `MQTTSessionDelegate` to receive messages
```swift
mqttSession.delegate = self
```
```swift
func mqttSession(session: MQTTSession, received message: Data, in topic: String) {
let string = String(data: message, encoding: .utf8)!
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
print(message.topic)
print(message.stringRepresentation)
}
```
```swift
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
if error == .none {
print("Successfully disconnected from MQTT broker")
} else {
print(error.description)
}
}
```

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

````ruby
use_frameworks!
pod 'SwiftMQTT'
target 'MyApp' do
use_frameworks!
pod 'SwiftMQTT'
end
````

### Carthage

```
github "aciidb0mb3r/SwiftMQTT"
```

### Swift Package Manager

```swift
dependencies: [
.package(url: "https://github.com/aciidb0mb3r/SwiftMQTT.git", from: "3.0.0")
]
```
## License
MIT
6 changes: 4 additions & 2 deletions SwiftMQTT.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
Pod::Spec.new do |s|

s.name = "SwiftMQTT"
s.version = "2.0.0"
s.version = "3.0.0"
s.summary = "MQTT Client in pure Swift"
s.description = <<-DESC
MQTT Client in Swift 3.0 based on MQTT Version 3.1.1
MQTT Client in Swift 4.1 based on MQTT Version 3.1.1
DESC

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

s.swift_version = "4.1"

s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.tvos.deployment_target = "9.0"
Expand Down
Loading

0 comments on commit 0978b7d

Please sign in to comment.