用阿里的示例代码,连接总是断开,也没办法订阅和接收:
import hmac import base64 from hashlib import sha1 import time from paho.mqtt.client import MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING, MQTT_LOG_ERR, MQTT_LOG_DEBUG from paho.mqtt import client as mqtt
client_id='XXXXXX'
topic = 'al001'
brokerUrl='XXXXX.mqtt.aliyuncs.com'
def on_connect(client, userdata, flags, rc): print('Connected with result code ' + str(rc)) client.subscribe(topic, 0) for i in range(1, 11): print(i) rc = client.publish(topic, str(i), qos=0) print ('rc: %s' % rc) time.sleep(0.1) def on_message(client, userdata, msg): print(msg.topic + ' ' + str(msg.payload))
def on_disconnect(client, userdata, rc): if rc != 0: print('Unexpected disconnection %s' % rc)
client = mqtt.Client(client_id, protocol=mqtt.MQTTv311, clean_session=True) client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect
userName = "XXX" password = "XXX"
client.connect(brokerUrl, 1883, 60) client.loop_forever()
结果显示: Connected with result code 0 1 rc: (0, 2) 2 rc: (0, 3) 3 rc: (0, 4) 4 rc: (0, 5) 5 rc: (0, 6) 6 rc: (0, 7) 7 rc: (0, 8) 8 rc: (0, 9) 9 rc: (0, 10) 10 rc: (0, 11) Unexpected disconnection 7
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系产品 Serverless 化。RocketMQ 一站式学习:https://rocketmq.io/