Skip to content

Commit

Permalink
Merge pull request #5 from adolfo/feature/add-ssl-support
Browse files Browse the repository at this point in the history
Add SSL support to MQTTSession
  • Loading branch information
aciidgh authored Aug 12, 2016
2 parents 7ff5af9 + 770abdb commit e24e0c6
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Master:

## Create MQTTSession object:
```swift
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15)
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
```

## Connect
Expand Down
2 changes: 1 addition & 1 deletion SwiftMQTT.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pod::Spec.new do |s|

s.name = "SwiftMQTT"
s.version = "1.0.0"
s.version = "1.0.1"
s.summary = "MQTT Client in pure swift"
s.description = <<-DESC
MQTT Client in pure swift
Expand Down
4 changes: 2 additions & 2 deletions SwiftMQTT/SwiftMQTT/MQTTSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class MQTTSession: MQTTSessionStreamDelegate {
private var messagesCompletionBlocks = [UInt16 : MQTTSessionCompletionBlock]()
private var stream: MQTTSessionStream

public init(host: String, port: UInt16, clientID: String, cleanSession: Bool, keepAlive: UInt16) {
stream = MQTTSessionStream(host: host, port: port)
public init(host: String, port: UInt16, clientID: String, cleanSession: Bool, keepAlive: UInt16, useSSL: Bool = false) {
stream = MQTTSessionStream(host: host, port: port, ssl: useSSL)
self.clientID = clientID
self.cleanSession = cleanSession
self.keepAlive = keepAlive
Expand Down
8 changes: 7 additions & 1 deletion SwiftMQTT/SwiftMQTT/MQTTSessionStreamDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ class MQTTSessionStream: NSObject, NSStreamDelegate {

internal let host: String
internal let port: UInt16
internal let ssl: Bool

private var inputStream:NSInputStream?
private var outputStream:NSOutputStream?

internal var delegate: MQTTSessionStreamDelegate?

init(host: String, port: UInt16) {
init(host: String, port: UInt16, ssl: Bool) {
self.host = host
self.port = port
self.ssl = ssl
}

func createStreamConnection() {
Expand All @@ -36,6 +38,10 @@ class MQTTSessionStream: NSObject, NSStreamDelegate {
outputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
inputStream?.open()
outputStream?.open()
if ssl {
inputStream?.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
outputStream?.setProperty(NSStreamSocketSecurityLevelNegotiatedSSL, forKey: NSStreamSocketSecurityLevelKey)
}
}

func closeStreams() {
Expand Down
2 changes: 1 addition & 1 deletion SwiftMQTT/SwiftMQTTTests/SwiftMQTTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SwiftMQTTTests: XCTestCase, MQTTSessionDelegate {

override func setUp() {
super.setUp()
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15)
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
mqttSession.delegate = self
mqttSession.connect { (succeeded, error) -> Void in
XCTAssertTrue(succeeded, "could not connect, error \(error)")
Expand Down
2 changes: 1 addition & 1 deletion SwiftMQTTExample/SwiftMQTTExample/MQTTViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MQTTViewController: UIViewController, MQTTSessionDelegate {
let port:UInt16 = 1883
let clientID = self.clientID()

mqttSession = MQTTSession(host: host, port: port, clientID: clientID, cleanSession: true, keepAlive: 15)
mqttSession = MQTTSession(host: host, port: port, clientID: clientID, cleanSession: true, keepAlive: 15, useSSL: false)
mqttSession.delegate = self

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

0 comments on commit e24e0c6

Please sign in to comment.