MOsquitto意外断开
我正在使用python mosquitto(paho)库。我遇到了问题Unexpected disconnection.
我发现不断丢弃并连接服务器。 。我无法找到问题。有时它会显示[错误104]连接被对等方重置。我主持我的经纪人在亚马逊Web服务...... 我的错误是---MOsquitto意外断开
已连接到XX.XX.XX.XX:1883年
意外断开。
已连接到XX.XX.XX.XX:1883年
意外断开。
已连接到XX.XX.XX.XX:1883年
我传递QOS = 2,保留=真实,干净的会话=假,没有用户名和密码,LWT =无,端口= 1883年,IP =服务器托管在AWS上,客户端ID =发行
我的代码包括---
import time
import paho.mqtt.client as mqtt
class MqttCommunication(object):
def __init__(self):
self.current_module = "Mqtt Communication class"
self.clientID = clientId
self.name = thread_name
self.DEBUG = True
self.MQTT_HOST = ""
self.MQTT_PORT = 1883
self.MQTT_USERNAME = ""
self.MQTT_PASSWORD = ""
self.MQTT_CLIENT_ID = self.clientID
self.MQTT_TOPIC = ""
self.MQTT_QOS = 0
self.MQTT_RETAIN = None
self.MQTT_CLEAN_SESSION = None
self.MQTT_LWT = ""
self.client = mqtt.Client(self.MQTT_CLIENT_ID, clean_session=self.MQTT_CLEAN_SESSION)
self.on_connect = None
self.on_disconnect = None
self.on_message = None
self.on_subscribe = None
self.on_unsubscribe = None
self.on_publish = None
self.client.on_connect = self.mqtt_on_connect
#self.client.on_message = self.mqtt_on_message
self.client.on_disconnect = self.mqtt_on_disconnect
self.client.on_subscribe = self.mqtt_on_subscribe
self.client.on_unsubscribe = self.mqtt_on_unsubscribe
self.client.on_publish = self.mqtt_on_publish
def connectHost(self,mqtt_host,mqtt_port,mqtt_username,mqtt_password):
self.MQTT_USERNAME = mqtt_username
self.MQTT_PASSWORD = mqtt_password
self.MQTT_HOST = mqtt_host
self.MQTT_PORT = mqtt_port
try:
self.client.username_pw_set(self.MQTT_USERNAME, self.MQTT_PASSWORD)
print self.client.connect(self.MQTT_HOST,self.MQTT_PORT,60)
print self.MQTT_HOST
except Exception, e:
print "Error connecting to %s:%d: %s" % (mqtt_host, mqtt_port, str(e))
return True
def disconnectHost(self):
self.client.disconnect()
return True
def mqttSettings(self,qos,mqtt_retain,mqtt_clean_session,mqtt_lwt):
self.MQTT_QOS = qos
self.MQTT_RETAIN = mqtt_retain
self.MQTT_CLEAN_SESSION = mqtt_clean_session
self.MQTT_LWT = mqtt_lwt
return True
def subscribeTopic(self,topic):
self.MQTT_TOPIC = topic
self.client.subscribe(self.MQTT_TOPIC, qos=self.MQTT_QOS)
return True
def unsubscribeTopic(self,topic):
self.client.unsubscribe(self.MQTT_TOPIC)
return True
def setClientId(self,clientID):
self.MQTT_CLIENT_ID= clientID
return True
def getClientId(self):
return self.MQTT_CLIENT_ID
def publishData(self,topic,message,qos):
self.client.publish(topic,message,qos)
return True
# The callback for when the client receives a CONNACK response from the server.
def mqtt_on_connect(self,client, userdata, flags, rc):
if rc == 0:
print "Connected to %s:%s" % (self.MQTT_HOST, self.MQTT_PORT)
time.sleep(3)
elif rc == 1:
print "Connection refused - unacceptable protocol version"
elif rc == 2:
print "Connection refused - identifier rejected"
elif rc == 3:
print "Connection refused - server unavailable"
elif rc == 4:
print "Connection refused - bad user name or password"
elif rc == 5:
print "Connection refused - not authorised"
else:
print "Connection failed - result code %d" % (rc)
# The callback for when a PUBLISH message is received from the server.
def mqtt_on_message(self , client, userdata, msg):
#print msg
print(msg.topic+" : "+str(msg.payload))
def mqtt_on_disconnect(self, client, userdata, rc):
if rc != 0:
print("Unexpected disconnection.")
else:
print('hello from disconnect')
def mqtt_on_publish(self, client, userdata, mid):
"""
What to do when a message is published
"""
#print "publish"
def mqtt_on_subscribe(self,client, userdata, mid, granted_qos):
"""
What to do in the event of subscribing to a topic"
"""
#logging.debug("Subscribe with mid " + str(mid) + " received.")
def mqtt_on_unsubscribe(self, client, userdata, mid):
"""
What to do in the event of unsubscribing from a topic
"""
#logging.debug("Unsubscribe with mid " + str(mid) + " received.")
共享从日志文件中的一些日志....
1483168399: Client publisher already connected, closing old connection.
1483168399: Client publisher disconnected.
1483168399: New client connected from xx.xx.xx.xx as publisher (c0, k60).
1483168401: New connection from xx.xx.xx.xx on port 1883.
1483168401: Client publisher already connected, closing old connection.
1483168401: Client publisher disconnected.
1483168401: New client connected from xx.xx.xx.xx as publisher (c0, k60).
1483168404: New connection from xx.xx.xx.xx on port 1883.
1483168404: Client publisher already connected, closing old connection.
1483168404: Client publisher disconnected.
1483168404: New client connected from xx.xx.xx.xx as publisher (c0, k60).
1483168405: New connection from xx.xx.xx.xx on port 1883.
1483168405: Client publisher already connected, closing old connection.
1483168405: Client publisher disconnected.
1483168405: New client connected from xx.xx.xx.xx as publisher (c0, k60).
1483168408: New connection from xx.xx.xx.xx on port 1883.
1483168408: Client publisher already connected, closing old connection.
1483168408: Client publisher disconnected.
1483168408: New client connected from xx.xx.xx.xx as publisher (c0, k60).
1483168410: New connection from 27
您有多个使用相同client_id的连接。 client_id必须是唯一的,经纪人一次只允许一个连接与一个client_id,并且在新连接形成时将代理从代理中踢出。如果您有重新连接逻辑,那么旧客户端会尝试重新连接,从而导致新的客户端关闭,最终以反馈的形式看到客户端无法与代理保持连接。
看起来你已经将日志中的client_id硬编码为“publisher”,需要将其更改为唯一值,通常我会将其设置为像“pub_”这样的前缀,然后添加一个随机数,时间戳或其他标识符(例如thead number/name)
我也试过。现在我从蚊子日志表中得到这个错误。 1483418758:客户端mqttjs_e0cbe1a发生套接字错误,断开连接。 1483418793:客户端2017-01-03 09:54:40.930093已超出超时,断开连接$ 1483418793:客户端上的套接字错误2017-01-03 09:54:40.930093,断开连接。 1483418822:在端口1883上从xx.xx.xx.xx进行新连接。 –
这是单独的问题,很可能是因为您不停留网络循环。用更新后的代码和日志输出问一个新问题 – hardillb
使用代码更新问题,以便我们可以看到您如何使用该库以及有关所有客户端是否位于同一台机器或网络上的一些信息 – hardillb
客户端连接后,需要处理客户端和蚊子代理之间的网络流量。这将避免连接由于超时而关闭。所以如果你使用paho,你需要在调用'self.client.connect(...)'函数后添加'self.client.loop_start()'。 – Hicaro
@Hicaro我把它包括了,但我又面临同样的问题.. –