so I am adding MQTT using the cocoa matt pod into my app, however I am encountering an error I can't get round. I pass an appState class into my MQTT manager class as a var, this is where I get the error.
private var mqttClient: CocoaMQTT?
private var identifier: String!
private var host: String!
private var topic: String!
private var username: String!
private var password: String!
@Published var currentAppState = MQTTAppState()
private var anyCancellable: AnyCancellable?
// Private Init
private init() {
// Workaround to support nested Observables, without this code changes to state is not propagated
anyCancellable = currentAppState.objectWillChange.sink { [weak self] _ in
self?.objectWillChange.send()
}
}
// MARK: Shared Instance
private static let _shared = MQTTManager()
// MARK: - Accessors
class func shared() -> MQTTManager {
return _shared
}
func initializeMQTT(host: String, identifier: String, username: String? = nil, password: String? = nil) {
// If any previous instance exists then clean it
if mqttClient != nil {
mqttClient = nil
}
self.identifier = identifier
self.host = host
self.username = username
self.password = password
let clientID = "CocoaMQTT-\(identifier)-" + String(ProcessInfo().processIdentifier)
// TODO: Guard
mqttClient = CocoaMQTT(clientID: clientID, host: host, port: 1883)
// If a server has username and password, pass it here
if let finalusername = self.username, let finalpassword = self.password {
mqttClient?.username = finalusername
mqttClient?.password = finalpassword
}
mqttClient?.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
mqttClient?.keepAlive = 60
mqttClient?.delegate = self
}
func connect() {
if let success = mqttClient?.connect(), success {
currentAppState.setAppConnectionState(state: .connecting)
} else {
currentAppState.setAppConnectionState(state: .disconnected)
}
}
func subscribe(topic: String) {
self.topic = topic
mqttClient?.subscribe(topic, qos: .qos1)
}
func publish(with message: String) {
mqttClient?.publish(topic, withString: message, qos: .qos1)
}
func disconnect() {
mqttClient?.disconnect()
}
/// Unsubscribe from a topic
func unSubscribe(topic: String) {
mqttClient?.unsubscribe(topic)
}
/// Unsubscribe from a topic
func unSubscribeFromCurrentTopic() {
mqttClient?.unsubscribe(topic)
}
func currentHost() -> String? {
return host
}
func isSubscribed() -> Bool {
return currentAppState.appConnectionState.isSubscribed
}
func isConnected() -> Bool {
return currentAppState.appConnectionState.isConnected
}
func connectionStateMessage() -> String {
return currentAppState.appConnectionState.description
}
}
something funny with comments going on when pasted here.... this is the class I'm passing through.
@Published var appConnectionState: MQTTAppConnectionState = .disconnected
@Published var historyText: String = ""
private var receivedMessage: String = ""
func setReceivedMessage(text: String) {
receivedMessage = text
historyText = historyText + "\n" + receivedMessage
}
func clearData() {
receivedMessage = ""
historyText = ""
}
func setAppConnectionState(state: MQTTAppConnectionState) {
appConnectionState = state
}
}
I'm pretty new to IOS development, can anyone help send me in the right direction?