首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MQTT订阅在Bluemix容器中丢失。

MQTT订阅在Bluemix容器中丢失。
EN

Stack Overflow用户
提问于 2017-03-30 06:48:49
回答 1查看 90关注 0票数 0

我正在使用蓝光IoT服务。我的程序由以下元素组成:

  1. 出版商(本地机器)
  2. 订阅(Bluemix)
  3. 出版商(布卢米克斯)
  4. 用户(本地机器)

我目前正在执行以下步骤: Publisher (本地机器)>订户(Bluemix) > Publisher (Bluemix) >订户(本地机器)

我面临的问题是,当我尝试同时使用两个订阅者时,服务就会从两端取消订阅。如果我只保留订户,那么步骤就完美了。我使用的主题如下:

topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json“ "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json“= topic2

有人能指点我这里做错了什么吗?

编辑:添加代码

本地机器上的Publisher是一个python文件,由典型的连接和发布方法组成。每次发布之后,我都会断开与IoT服务的连接。

在Bluemix上的用户代码:

代码语言:javascript
复制
# -*- coding: utf-8 -*-

#!/usr/bin/env python

import paho.mqtt.client as mqtt
import os, json
import time

organization = "xel7"
username = ""
password = ""


#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"
topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotData/fmt/json"

deviceType = "mymqttdevice"

topic2 = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"

clientID = "a:" + organization + ":appId"

broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)

if username is not "":
 mqttc.username_pw_set(username, password=password)


def on_connect(client, userdata, flags, rc):
 print("Connected with result code "+str(rc))

def on_subscribe(mosq, obj, mid, granted_qos):
 print("Subscribed: " + str(mid) + " " + str(granted_qos))


def on_message(client, userdata, msg):
    with open('indurator.txt', 'w') as fd:
     txt = (msg.payload.decode('string_escape'))
     fd.write(txt)
     #print txt
     fd.close()
     mqttc.publish(topic2,msg.payload);

mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)

mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message

mqttc.loop_forever()

本地机器上的订阅服务器代码,以接收从Bluemix订阅服务器发布的文件:

-编码: utf-8 -

代码语言:javascript
复制
#!/usr/bin/env python

import paho.mqtt.client as mqtt
import os, json
import time

organization = "xel7"
username = ""
password = ""


#Set the variables for connecting to the iot service
broker = ""
devicename = "mynewdev"

deviceType = "mymqttdevice"

topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json"

clientID = "a:" + organization + ":appId"

broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)

if username is not "":
 mqttc.username_pw_set(username, password=password)


def on_connect(client, userdata, flags, rc):
 print("Connected with result code "+str(rc))

def on_subscribe(mosq, obj, mid, granted_qos):
 print("Subscribed: " + str(mid) + " " + str(granted_qos))


def on_message(client, userdata, msg):
    with open('receivednew.txt', 'w') as fd:
     txt = (msg.payload.decode('string_escape'))
     fd.write(txt)
     #print txt
     fd.close()

mqttc.connect(host=broker, port=1883, keepalive=60)
test = mqttc.subscribe(topic,0)

mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.on_message = on_message

mqttc.loop_forever()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-30 14:31:54

很高兴你找到了解决办法。概括地说,正如上面提到的hardillb和amadain一样,每个Watson IoT Platform 文档不应该同时使用相同的客户机ID。

如果重复使用客户端ID,则当您试图连接到IoT平台时,您的设备或应用程序将收到错误。这可能表明您的断开连接是由于clientID被重复使用或“被盗”。

如果您有两个设备连接相同的clientId和凭据-这将导致clientId窃取。每个clientID只允许一个唯一的连接;您不能使用相同的ID拥有两个并发连接。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43110499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档