И если я использую обычный порт MQTT, то соединение работает нормально, используя мобильные данные. Но проблема в том, что Устройство Raspberry предоставляет учетные данные для подключения во время входа в систему. И Шлюз предоставляет только порт WebSocket и SSL устройства. И все устройства будут иметь разные учетные данные, поэтому применение статического значения для порта не будет работать.
- MQTT Broker и порт
Код: Выделить всё
MQTT Cloud Broker
Server driver.cloudmqtt.com
User : xxxxxx
Password: xxxxxx
Port: 18658 //Only this port is working
SSL Port: 28658
Websockets Port (TLS only): 38658
Mosquitto version: 1.5.7
- Код подключения
Код: Выделить всё
func connect(host: String, port: Int, username: String?, password: String?, cleanSession: Bool) {
guard !host.isEmpty else {
delegate?.onMqttDisconnected()
return
}
status = .connecting
let clientId = "CONNECTED_GEAR-" + String(ProcessInfo().processIdentifier)
if NetworkMonitor.shared.connection == .wifi {
mqttClient = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port))
if let mqttClient = mqttClient {
UserManager.shared.isConnected = true
mqttClient.username = username
mqttClient.password = password
mqttClient.keepAlive = UInt16(60)
mqttClient.cleanSession = cleanSession
mqttClient.backgroundOnSocket = true
mqttClient.delegate = self
_ = mqttClient.connect()
} else {
UserManager.shared.isConnected = false
delegate?.onMqttError(message: "Mqtt initialization error")
status = .error
}
} else {
let socket = CocoaMQTTWebSocket()
socket.enableSSL = false
mqttClient = CocoaMQTT(clientID: clientId, host: host, port: UInt16(port), socket: socket)
if let mqttClient = mqttClient {
UserManager.shared.isConnected = true
mqttClient.username = username
mqttClient.password = password
mqttClient.keepAlive = UInt16(60)
mqttClient.cleanSession = cleanSession
mqttClient.logLevel = .debug
mqttClient.autoReconnect = false
mqttClient.allowUntrustCACertificate = true
mqttClient.backgroundOnSocket = true
mqttClient.delegate = self
_ = mqttClient.connect()
} else {
UserManager.shared.isConnected = false
delegate?.onMqttError(message: "Mqtt initialization error")
status = .error
}
}
}
- Журналы MQTT
Код: Выделить всё
Network reachable via Cellular
External Host: driver.cloudmqtt.com
External Port: 38658
External User: xxxxxxx
External Password: xxxxxxx
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): SEND: CONNECT(id: CONNECTED_GEAR-933, username: xxxxxxx, password: xxxxxxx, keepAlive : 60, cleansess: false)
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 16
CocoaMQTT(debug): remainingLen(len: len) [54]
CocoaMQTT(debug): variableHeader [0, 4, 77, 81, 84, 84, 4, 192, 0, 60]
CocoaMQTT(debug): payload [0, 18, 67, 79, 78, 78, 69, 67, 84, 69, 68, 95, 71, 69, 65, 82, 45, 57, 51, 51, 0, 8, 104, 101, 121, 103, 108, 105, 104, 103, 0, 12, 71, 103, 107, 106, 74, 101, 110, 81, 114, 119, 120, 48]
CocoaMQTT(debug): =============================================================
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): socket wrote data 0
CocoaMQTT(debug): socket disconnected
Disconnect: Socket closed by remote peer
Код: Выделить всё
External Host: driver.cloudmqtt.com
External Port: 38658
External User: xxxxxxx
External Password: xxxxxxx
CocoaMQTT(debug): ping
CocoaMQTT(debug): SEND: PING
CocoaMQTT(debug): =========================MQTT 3.1.1=========================
CocoaMQTT(debug): packetFixedHeaderType 192
CocoaMQTT(debug): remainingLen(len: len) [0]
CocoaMQTT(debug): variableHeader []
CocoaMQTT(debug): payload []
CocoaMQTT(debug): =============================================================
CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): socket wrote data -192
CocoaMQTT(debug): socket disconnected
Disconnect: The operation couldn’t be completed. (kCFStreamErrorDomainSSL error -9806.)
CocoaMQTT(info): Try reconnect to server after 1s
CocoaMQTT(info): Connected to 3.83.156.245 : 38658
CocoaMQTT(info): Enable backgrounding socket successfully
CocoaMQTT(debug): Call the SSL/TLS manually validating function
- Я прочитал так много вопросов, блогов и документов относительно порта MQTT Over WebSocket и SSL, но ничего не работает.
К вашему сведению: – из-за проблемы с GeoFence в React-Native мы разработали приложения на Native Swift и Kotlin
Подробнее здесь: https://stackoverflow.com/questions/787 ... -ios-swift
Мобильная версия