Skip to content

Commit

Permalink
Merge c4fa489 into 7837c63
Browse files Browse the repository at this point in the history
  • Loading branch information
HJianBo committed Dec 24, 2019
2 parents 7837c63 + c4fa489 commit 6af4689
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Example/Podfile.lock
Carthage/
Cartfile.resolved
xcenv.sh
*.swp
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

10 changes: 5 additions & 5 deletions CocoaMQTT.podspec
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Pod::Spec.new do |s|
s.name = "CocoaMQTT"
s.version = "1.3.0-alpha.1"
s.version = "1.3.0-alpha.2"
s.summary = "MQTT v3.1.1 client library for iOS and OS X written with Swift 5"
s.homepage = "https://github.com/emqx/CocoaMQTT"
s.license = { :type => "MIT" }
s.authors = { "Feng Lee" => "feng@emqtt.io", "CrazyWisdom" => "zh.whong@gmail.com", "Alex Yu" => "alexyu.dc@gmail.com" }

s.swift_version = "5.0"
s.requires_arc = true
s.osx.deployment_target = "10.10"
s.ios.deployment_target = "9.0"
s.tvos.deployment_target = "9.0"
s.osx.deployment_target = "10.12"
s.ios.deployment_target = "10.0"
s.tvos.deployment_target = "10.0"
# s.watchos.deployment_target = "2.0"
s.source = { :git => "https://github.com/emqx/CocoaMQTT.git", :tag => "1.3.0-alpha.1"}
s.source = { :git => "https://github.com/emqx/CocoaMQTT.git", :tag => "1.3.0-alpha.2"}
s.default_subspec = 'Core'

s.subspec 'Core' do |ss|
Expand Down
10 changes: 6 additions & 4 deletions CocoaMQTT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
6EC871E023A4214000F69AE8 /* Starscream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6EC871DF23A4214000F69AE8 /* Starscream.framework */; };
6EC871E323A421A200F69AE8 /* CocoaMQTTSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC871E223A421A200F69AE8 /* CocoaMQTTSocket.swift */; };
6EC871E523A421DE00F69AE8 /* CocoaMQTTWebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC871E423A421DE00F69AE8 /* CocoaMQTTWebSocket.swift */; };
8225B53A227C2FC600E4DB51 /* CocoaMQTT.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0409977A1C1B1529006B5A6D /* CocoaMQTT.swift */; };
Expand Down Expand Up @@ -38,9 +37,10 @@
8D43DE6222FAFDA700D9A06B /* FramePingReq.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D43DE6122FAFDA700D9A06B /* FramePingReq.swift */; };
8D43DE6422FAFE5F00D9A06B /* FramePingResp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D43DE6322FAFE5F00D9A06B /* FramePingResp.swift */; };
8D43DE6622FAFF2500D9A06B /* FrameDisconnect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D43DE6522FAFF2500D9A06B /* FrameDisconnect.swift */; };
8D6C0E25239E7B44003094E9 /* CocoaAsyncSocket.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8225B543227C308900E4DB51 /* CocoaAsyncSocket.framework */; };
8D6C0E26239E7B44003094E9 /* CocoaAsyncSocket.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 8225B543227C308900E4DB51 /* CocoaAsyncSocket.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
8D7A262E23AF2FEA00CE7442 /* client-keycert.p12 in Resources */ = {isa = PBXBuildFile; fileRef = 8D7A262D23AF2FEA00CE7442 /* client-keycert.p12 */; };
8D7A262F23AF3B4700CE7442 /* Starscream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6EC871DF23A4214000F69AE8 /* Starscream.framework */; };
8D7A263023AF3B4700CE7442 /* Starscream.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 6EC871DF23A4214000F69AE8 /* Starscream.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -61,6 +61,7 @@
dstSubfolderSpec = 10;
files = (
8D6C0E26239E7B44003094E9 /* CocoaAsyncSocket.framework in Embed Frameworks */,
8D7A263023AF3B4700CE7442 /* Starscream.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -112,8 +113,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
6EC871E023A4214000F69AE8 /* Starscream.framework in Frameworks */,
8225B571227C3B0200E4DB51 /* CocoaAsyncSocket.framework in Frameworks */,
8D7A262F23AF3B4700CE7442 /* Starscream.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -600,6 +600,7 @@
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 10.0;
VALID_ARCHS = "$(inherited)";
};
name = Debug;
Expand Down Expand Up @@ -632,6 +633,7 @@
SUPPORTED_PLATFORMS = "$(inherited)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TVOS_DEPLOYMENT_TARGET = 10.0;
VALID_ARCHS = "$(inherited)";
};
name = Release;
Expand Down
58 changes: 56 additions & 2 deletions CocoaMQTTTests/CocoaMQTTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CocoaMQTTTests: XCTestCase {
super.tearDown()
}

func testConnect() {
func testConnect() {
let caller = Caller()
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: port)
mqtt.delegateQueue = deleQueue
Expand Down Expand Up @@ -75,13 +75,67 @@ class CocoaMQTTTests: XCTestCase {
caller.subs == []
}

mqtt.disconnect()
wait_for {
caller.isConnected == false
}
XCTAssertEqual(mqtt.connState, .disconnected)

}

func testWebsocketConnect() {
let caller = Caller()
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
mqtt.delegateQueue = deleQueue
mqtt.delegate = caller
mqtt.logLevel = .debug
mqtt.autoReconnect = false
//mqtt.enableSSL = true

_ = mqtt.connect()
wait_for { caller.isConnected }
XCTAssertEqual(mqtt.connState, .connected)

let topics = ["t/0", "t/1", "t/2"]

mqtt.subscribe(topics[0])
mqtt.subscribe(topics[1])
mqtt.subscribe(topics[2])
wait_for {
caller.subs == topics
}

mqtt.publish(topics[0], withString: "0", qos: .qos0, retained: false)
mqtt.publish(topics[1], withString: "1", qos: .qos1, retained: false)
mqtt.publish(topics[2], withString: "2", qos: .qos2, retained: false)
wait_for {
if caller.recvs.count >= 3 {
let f0 = caller.recvs[0]
let f1 = caller.recvs[1]
let f2 = caller.recvs[2]
XCTAssertEqual(f0.topic, topics[0])
XCTAssertEqual(f1.topic, topics[1])
XCTAssertEqual(f2.topic, topics[2])
return true
}
return false
}

mqtt.unsubscribe(topics[0])
mqtt.unsubscribe(topics[1])
mqtt.unsubscribe(topics[2])
wait_for {
caller.subs == []
}

mqtt.disconnect()
wait_for {
caller.isConnected == false
}
XCTAssertEqual(mqtt.connState, .disconnected)
}

func testAutoReconnect() {
let caller = Caller()
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: port)
Expand Down
6 changes: 4 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@
"${PODS_ROOT}/Target Support Files/Pods-Example/Pods-Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/CocoaAsyncSocket/CocoaAsyncSocket.framework",
"${BUILT_PRODUCTS_DIR}/CocoaMQTT/CocoaMQTT.framework",
"${BUILT_PRODUCTS_DIR}/Starscream/Starscream.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CocoaAsyncSocket.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CocoaMQTT.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Starscream.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down Expand Up @@ -320,7 +322,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -369,7 +371,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
25 changes: 25 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ViewController: UIViewController {
mqttSetting()
// selfSignedSSLSetting()
// simpleSSLSetting()
// mqttWebsocketsSetting()
// mqttWebsocketSSLSetting()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -83,6 +85,29 @@ class ViewController: UIViewController {
mqtt!.sslSettings = sslSettings
}

func mqttWebsocketsSetting() {
let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 8083, socket: websocket)
mqtt!.username = ""
mqtt!.password = ""
mqtt!.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
}

func mqttWebsocketSSLSetting() {
let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 8084, socket: websocket)
mqtt!.enableSSL = true
mqtt!.username = ""
mqtt!.password = ""
mqtt!.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
}

func getClientCertFromP12File(certName: String, certPassword: String) -> CFArray? {
// get p12 file path
let resourcePath = Bundle.main.path(forResource: certName, ofType: "p12")
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
platform :ios, 9.0
platform :ios, 10.0
use_frameworks!

target 'Example' do
pod 'CocoaMQTT', :path => '../'
pod 'CocoaMQTT/WebSockets', :path => '../'
end
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ Need a .p12 file which is generated by a public key file and a private key file.
openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12
```

## MQTT over Websocket

In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.

If you integrated by **CocoaPods**, you need to modify you `Podfile` like the followings and execute `pod install` again:

```ruby
use_frameworks!

target 'Example' do
pod 'CocoaMQTT/WebSockets', '1.3.0'
end

```

Then, Create a MQTT instance over Websocket:

```swift
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)

// ...

_ = mqtt.connect()

```

## Example App

You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:
Expand All @@ -124,6 +151,7 @@ Then, open the `Example.xcworkspace/` by Xcode and start it!
These third-party functions are used:

* [GCDAsyncSocket](https://github.com/robbiehanson/CocoaAsyncSocket)
* [Starscream](https://github.com/daltoniam/Starscream)


## LICENSE
Expand All @@ -137,6 +165,7 @@ MIT License (see `LICENSE`)
* [@jan-bednar](https://github.com/jan-bednar)
* [@jmiltner](https://github.com/jmiltner)
* [@manucheri](https://github.com/manucheri)
* [@Cyrus Ingraham](https://github.com/cyrusingraham)

## Author

Expand Down
44 changes: 9 additions & 35 deletions Source/CocoaMQTT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public class CocoaMQTT: NSObject, CocoaMQTTClient {

/// Enable SSL connection
public var enableSSL: Bool {
get { return (self.socket as? CocoaMQTTSocket)?.enableSSL ?? false }
set { (self.socket as? CocoaMQTTSocket)?.enableSSL = newValue }
get { return self.socket.enableSSL }
set { socket.enableSSL = newValue }
}

///
Expand Down Expand Up @@ -542,41 +542,17 @@ extension CocoaMQTT {
}
}

// MARK: - GCDAsyncSocketDelegate
// MARK: - CocoaMQTTSocketDelegate
extension CocoaMQTT: CocoaMQTTSocketDelegate {

public func socketConnected(_ socket: CocoaMQTTSocketProtocol) {
printInfo("Connected")

public func socketConnected(_ socket: CocoaMQTTSocketProtocol) {
sendConnectFrame()
}

// public func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
// printInfo("Connected to \(host) : \(port)")

// #if os(iOS)
// if backgroundOnSocket {
// sock.perform {
// guard sock.enableBackgroundingOnSocket() else {
// printWarning("Enable backgrounding socket failed, please check related permissions")
// return
// }
// printInfo("Enable backgrounding socket successfully")
// }
// }
// #endif

public func socket(_ socket: CocoaMQTTSocketProtocol,
didReceive trust: SecTrust,
completionHandler: @escaping (Bool) -> Swift.Void) {

// if enableSSL {
// var setting = sslSettings ?? [:]
// if allowUntrustCACertificate {
// setting[GCDAsyncSocketManuallyEvaluateTrust as String] = NSNumber(value: true)
// }
// sock.startTLS(setting)
// } else {
// sendConnectFrame()
// }
// }

public func socket(_ socket: CocoaMQTTSocketProtocol, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Swift.Void) {
printDebug("Call the SSL/TLS manually validating function")

delegate?.mqtt?(self, didReceive: trust, completionHandler: completionHandler)
Expand All @@ -594,7 +570,6 @@ extension CocoaMQTT: CocoaMQTTSocketDelegate {
}

public func socket(_ socket: CocoaMQTTSocketProtocol, didRead data: Data, withTag tag: Int) {
printDebug("Socket read data with tag: \(tag)")
let etag = CocoaMQTTReadTag(rawValue: tag)!
var bytes = [UInt8]([0])
switch etag {
Expand Down Expand Up @@ -788,4 +763,3 @@ extension CocoaMQTT: CocoaMQTTReaderDelegate {
didReceivePong(self)
}
}

0 comments on commit 6af4689

Please sign in to comment.