//全局使用 mqtt
import useUserStore from '@/store/modules/user'

function doSubscribe(topic, qos){
  useUserStore().mqttClient.subscribe(
    topic,
    { qos },
    (error,granted)=>{
      if (error) {
        console.log("订阅失败:", error);
        return;
      }
      // console.log("订阅成功:", JSON.stringify(granted));
    }
  )
}

export function mqttOnMessage(topic, qos, fun){
  //订阅主题
  doSubscribe(topic, qos)
  //监听消息
  useUserStore().mqttClient.on("message", fun);
}
