Skip to content

Commit 770abdb

Browse files
author
Adolfo Martinelli
committed
Add SSL support to MQTTSession
1 parent 7ff5af9 commit 770abdb

File tree

6 files changed

+13
-7
lines changed

6 files changed

+13
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Master:
1616

1717
## Create MQTTSession object:
1818
```swift
19-
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15)
19+
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
2020
```
2121

2222
## Connect

SwiftMQTT.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Pod::Spec.new do |s|
33

44
s.name = "SwiftMQTT"
5-
s.version = "1.0.0"
5+
s.version = "1.0.1"
66
s.summary = "MQTT Client in pure swift"
77
s.description = <<-DESC
88
MQTT Client in pure swift

SwiftMQTT/SwiftMQTT/MQTTSession.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class MQTTSession: MQTTSessionStreamDelegate {
3232
private var messagesCompletionBlocks = [UInt16 : MQTTSessionCompletionBlock]()
3333
private var stream: MQTTSessionStream
3434

35-
public init(host: String, port: UInt16, clientID: String, cleanSession: Bool, keepAlive: UInt16) {
36-
stream = MQTTSessionStream(host: host, port: port)
35+
public init(host: String, port: UInt16, clientID: String, cleanSession: Bool, keepAlive: UInt16, useSSL: Bool = false) {
36+
stream = MQTTSessionStream(host: host, port: port, ssl: useSSL)
3737
self.clientID = clientID
3838
self.cleanSession = cleanSession
3939
self.keepAlive = keepAlive

SwiftMQTT/SwiftMQTT/MQTTSessionStreamDelegate.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ class MQTTSessionStream: NSObject, NSStreamDelegate {
1717

1818
internal let host: String
1919
internal let port: UInt16
20+
internal let ssl: Bool
2021

2122
private var inputStream:NSInputStream?
2223
private var outputStream:NSOutputStream?
2324

2425
internal var delegate: MQTTSessionStreamDelegate?
2526

26-
init(host: String, port: UInt16) {
27+
init(host: String, port: UInt16, ssl: Bool) {
2728
self.host = host
2829
self.port = port
30+
self.ssl = ssl
2931
}
3032

3133
func createStreamConnection() {
@@ -36,6 +38,10 @@ class MQTTSessionStream: NSObject, NSStreamDelegate {
3638
outputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
3739
inputStream?.open()
3840
outputStream?.open()
41+
if ssl {
42+
inputStream?.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
43+
outputStream?.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
44+
}
3945
}
4046

4147
func closeStreams() {

SwiftMQTT/SwiftMQTTTests/SwiftMQTTTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SwiftMQTTTests: XCTestCase, MQTTSessionDelegate {
1616

1717
override func setUp() {
1818
super.setUp()
19-
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15)
19+
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
2020
mqttSession.delegate = self
2121
mqttSession.connect { (succeeded, error) -> Void in
2222
XCTAssertTrue(succeeded, "could not connect, error \(error)")

SwiftMQTTExample/SwiftMQTTExample/MQTTViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class MQTTViewController: UIViewController, MQTTSessionDelegate {
5050
let port:UInt16 = 1883
5151
let clientID = self.clientID()
5252

53-
mqttSession = MQTTSession(host: host, port: port, clientID: clientID, cleanSession: true, keepAlive: 15)
53+
mqttSession = MQTTSession(host: host, port: port, clientID: clientID, cleanSession: true, keepAlive: 15, useSSL: false)
5454
mqttSession.delegate = self
5555

5656
self.appendStringToTextView("Trying to connect to \(host) on port \(port) for clientID \(clientID)")

0 commit comments

Comments
 (0)