CocoaMQTT
CocoaMQTT copied to clipboard
CocoaMQTT/Websockets cannot connect to wss host? Need help
Hi, I'm facing this issue when using CocoaMQTT/Websocket by doing pod 'CocoaMQTT/WebSockets', '1.3.0-rc.2'
to connect to my host at this url: wss://cpl-emqx-socket.staging-bitcastle.work:8083/mqtt
And this is what console log show for me:
2021-11-01 14:09:23.375234+0700 BitcastleApp[13709:570825] Task <ECC86E98-32B4-4C58-85D1-34BB7FC3C881>.<1> finished with error [-1003] Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSErrorFailingURLStringKey=https://wss//dev-cpl-socket.staging-bitcastle.work:8083/mqtt, NSErrorFailingURLKey=https://wss//dev-cpl-socket.staging-bitcastle.work:8083/mqtt, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalWebSocketTask <ECC86E98-32B4-4C58-85D1-34BB7FC3C881>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalWebSocketTask <ECC86E98-32B4-4C58-85D1-34BB7FC3C881>.<1>, NSLocalizedDescription=A server with the specified hostname could not be found.}
Here is my code, why the url still has http/https before wss? Can someone explain for me where's wrong in my code:
import UIKit
import CocoaMQTT
class CPLMQTTManager: NSObject {
var mqtt: CocoaMQTT?
static let shared = CPLMQTTManager()
var topic: String?
private override init() {
}
func startConnection() {
let clientID = "BitCastle" + String(ProcessInfo().processIdentifier)
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
mqtt = CocoaMQTT(clientID: clientID, host: "wss://dev-cpl-socket.staging-bitcastle.work", port: 8083, socket: websocket)
mqtt?.enableSSL = true
mqtt?.username = ""
mqtt?.password = ""
mqtt?.willMessage = CocoaMQTTMessage(topic: "orderbook_BTC_USDT", string: "hello")
mqtt?.keepAlive = 60
mqtt?.delegate = self
_ = mqtt?.connect(timeout: 60)
}
}
extension CPLMQTTManager: CocoaMQTTDelegate {
func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
completionHandler(true)
}
func mqttDidPing(_ mqtt: CocoaMQTT) {
}
func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
}
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
print("Connect HUHUH")
}
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
print("publish HUHUHU")
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
print("HUHUHUH \(message.string?.description ?? "")")
print("OPOPOP: \(message.topic)")
}
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {
if failed.count > 0 {
print("UIUIUI \(failed)")
}else {
print("KOKOKO \(success)")
}
}
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {
}
}
@sonhd92 hi, try these codes
let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
mqtt = CocoaMQTT(clientID: clientID, host: "cpl-emqx-socket.staging-bitcastle.work", port: 8083, socket: websocket)
mqtt!.username = ""
mqtt!.password = ""
mqtt!.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
mqtt!.keepAlive = 60
mqtt!.delegate = self
mqtt!.enableSSL = true
@sonhd92 did you find any workaround? I have the same problem.
@sonhd92 did you find any workaround? I have the same problem.
I use the solution of @leeway1208 above and it's worked for me