ITADN

`react-native-client-1745308461485client_initialize` is not a supported event type for MqttModule. Supported events are: `CUSTOM_EVENT`

#22OpenJidahan 创建于 2025-04-22
J
Jidahancommented
After executing pod install on ios, yarn ios reports an error when starting the app directly same problem: https://github.com/dream-sports-labs/d11-react-native-mqtt/issues/8#issue-2717892227 ```javascript "@d11/react-native-mqtt": "^0.0.3", "react": "18.2.0", "react-native": "0.77.0", ``` error message: ``` javascript `react-native-client-1745308461485client_initialize` is not a supported event type for MqttModule. Supported events are: `CUSTOM_EVENT` -[RCTEventEmitter sendEventWithName:body:] RCTEventEmitter.m:60 -[MqttModule sendEventToJs:param:] -[MqttEventEmitter sendEvent:param:] $s8d11_mqtt4MqttC25triggerNativeEventEmitter_6paramsySS_SDySSypGSgtF $s8d11_mqtt4MqttC06createC0_4host4port15enableSslConfigySS_SSSiSbtFySS_SDySSypGSgtcACcfu_ySS_AItcfu0_ $s8d11_mqtt10MqttHelperC_4host4port15enableSslConfig12emitJsiEventACSS_SSSiSbySS_SDySSypGSgtctcfc $s8d11_mqtt10MqttHelperC_4host4port15enableSslConfig12emitJsiEventACSS_SSSiSbySS_SDySSypGSgtctcfC $s8d11_mqtt11MqttManagerC06createC0_4host4port15enableSslConfig12emitJsiEventySS_SSSiSbySS_SDySSypGSgtctFyyYbcfU_ $s8d11_mqtt11MqttManagerC06createC0_4host4port15enableSslConfig12emitJsiEventySS_SSSiSbySS_SDySSypGSgtctFyyYbcfU_TA $sIegh_IeyBh_TR _dispatch_call_block_and_release _dispatch_client_callout _dispatch_lane_serial_drain _dispatch_lane_invoke _dispatch_root_queue_drain_deferred_wlh _dispatch_workloop_worker_thread _pthread_wqthread start_wqthread ``` hooks code: ```javascript // mqtt.ts import {useState, useEffect, useCallback} from 'react'; import {createMqttClient} from '@d11/react-native-mqtt'; import {MqttClient} from '@d11/react-native-mqtt/dist/Mqtt/MqttClient'; import address from '../../api/address'; enum MqttQos { AT_MOST_ONCE = 0, AT_LEAST_ONCE = 1, EXACTLY_ONCE = 2, } type SubscribeParams = { topic: string; onMessage: (payload: string) => void; timeout?: number; }; const mqttConfig = { clientId: 'react-native-client-' + new Date().getTime().toString(), host: address.mqttUrl, port: 1883, options: { enableSslConfig: false, autoReconnect: true, keepAlive: 60, cleanSession: true, }, }; const useMqtt = () => { const [client, setClient] = useState<MqttClient | null>(null); const [isConnected, setIsConnected] = useState(false); const [activeSubscriptions, setActiveSubscriptions] = useState( new Map<string, {remove: () => void}>(), ); // 初始化MQTT客户端 useEffect(() => { const initClient = async () => { try { const newClient = await createMqttClient({ clientId: mqttConfig.clientId, host: mqttConfig.host, port: mqttConfig.port, options: mqttConfig.options, }); setClient(newClient); // 连接时传入回调 newClient?.connect(); setIsConnected(true); } catch (error) { console.error('MQTT初始化失败:', error); setIsConnected(false); } }; if (!client) { initClient(); } return () => { client?.disconnect(); }; }, []); // 取消订阅 const unsubscribe = useCallback( (topic: string) => { const subscription = activeSubscriptions.get(topic); if (subscription) { subscription.remove(); setActiveSubscriptions(prev => { const newMap = new Map(prev); newMap.delete(topic); return newMap; }); console.log(`取消订阅: ${topic}`); } }, [activeSubscriptions], ); // 订阅主题(带超时处理) const subscribe = useCallback( async ({ topic, onMessage, timeout = 20000, }: SubscribeParams): Promise<boolean> => { if (!client || !isConnected) { console.log('MQTT客户端未就绪'); return false; } return new Promise(resolve => { // 超时处理 const timeoutId = setTimeout(() => { unsubscribe(topic); console.log(`订阅超时: ${topic}`); resolve(false); }, timeout); // 执行订阅 const subscription = client.subscribe({ topic, qos: MqttQos.AT_LEAST_ONCE, onSuccess: () => { console.log(`订阅成功: ${topic}`); }, onError: error => { console.log(`订阅失败: ${topic}`, error); clearTimeout(timeoutId); resolve(false); }, onEvent: ({payload}) => { try { // 收到消息 clearTimeout(timeoutId); onMessage(payload.toString()); // unsubscribe(topic); resolve(true); } catch (e) { console.log('消息处理失败:', e); resolve(false); } }, }); // 记录当前订阅 setActiveSubscriptions(prev => new Map(prev).set(topic, subscription)); }); }, [client, isConnected, unsubscribe], ); return { isConnected, subscribe, unsubscribe, }; }; export default useMqtt; ```
1 条评论