Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
Eclipse Foundation
paho.mqtt.python
提交
0f7cd9c1
P
paho.mqtt.python
项目概览
镜像
/
Eclipse Foundation
/
paho.mqtt.python
10 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
paho.mqtt.python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0f7cd9c1
编写于
7月 07, 2021
作者:
R
Roger Light
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
git://github.com/koho/paho.mqtt.python
into koho-master
上级
9a4941ef
980b91b9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
55 addition
and
0 deletion
+55
-0
examples/client_sub-class.py
examples/client_sub-class.py
+3
-0
src/paho/mqtt/client.py
src/paho/mqtt/client.py
+33
-0
tests/test_mqttv5.py
tests/test_mqttv5.py
+19
-0
未找到文件。
examples/client_sub-class.py
浏览文件 @
0f7cd9c1
...
...
@@ -25,6 +25,9 @@ class MyMQTTClass(mqtt.Client):
def
on_connect
(
self
,
mqttc
,
obj
,
flags
,
rc
):
print
(
"rc: "
+
str
(
rc
))
def
on_connect_fail
(
self
,
mqttc
,
obj
):
print
(
"Connect failed"
)
def
on_message
(
self
,
mqttc
,
obj
,
msg
):
print
(
msg
.
topic
+
" "
+
str
(
msg
.
qos
)
+
" "
+
str
(
msg
.
payload
))
...
...
src/paho/mqtt/client.py
浏览文件 @
0f7cd9c1
...
...
@@ -475,6 +475,8 @@ class Client(object):
Decorator: @client.connect_callback() (```client``` is the name of the
instance which this callback is being attached to)
on_connect_fail(client, userdata): called when the client failed to connect to the broker.
on_disconnect(client, userdata, rc): called when the client disconnects from the broker.
The rc parameter indicates the disconnection state:
MQTT_ERR_SUCCESS: the callback was called in response to a disconnect() call.
...
...
@@ -689,6 +691,7 @@ class Client(object):
# No default callbacks
self
.
_on_log
=
None
self
.
_on_connect
=
None
self
.
_on_connect_fail
=
None
self
.
_on_subscribe
=
None
self
.
_on_message
=
None
self
.
_on_publish
=
None
...
...
@@ -1809,6 +1812,7 @@ class Client(object):
try
:
self
.
reconnect
()
except
(
socket
.
error
,
OSError
,
WebsocketConnectionError
):
self
.
_handle_on_connect_fail
()
if
not
retry_first_connection
:
raise
self
.
_easy_log
(
...
...
@@ -1847,6 +1851,7 @@ class Client(object):
try
:
self
.
reconnect
()
except
(
socket
.
error
,
OSError
,
WebsocketConnectionError
):
self
.
_handle_on_connect_fail
()
self
.
_easy_log
(
MQTT_LOG_DEBUG
,
"Connection failed, retrying"
)
...
...
@@ -1960,6 +1965,25 @@ class Client(object):
return
func
return
decorator
@
property
def
on_connect_fail
(
self
):
"""If implemented, called when the client failed to connect
to the broker."""
return
self
.
_on_connect_fail
@
on_connect_fail
.
setter
def
on_connect_fail
(
self
,
func
):
""" Define the connection failure callback implementation
Expected signature is:
on_connect_fail(client, userdata)
client: the client instance for this callback
userdata: the private user data as set in Client() or userdata_set()
"""
with
self
.
_callback_mutex
:
self
.
_on_connect_fail
=
func
@
property
def
on_subscribe
(
self
):
"""If implemented, called when the broker responds to a subscribe
...
...
@@ -3544,6 +3568,15 @@ class Client(object):
if
not
self
.
suppress_exceptions
:
raise
def
_handle_on_connect_fail
(
self
):
if
self
.
on_connect_fail
:
with
self
.
_in_callback_mutex
:
try
:
self
.
on_connect_fail
(
self
,
self
.
_userdata
)
except
Exception
as
err
:
self
.
_easy_log
(
MQTT_LOG_ERR
,
'Caught exception in on_connect_fail: %s'
,
err
)
def
_thread_main
(
self
):
self
.
loop_forever
(
retry_first_connection
=
True
)
...
...
tests/test_mqttv5.py
浏览文件 @
0f7cd9c1
...
...
@@ -41,6 +41,7 @@ class Callbacks:
self
.
unsubscribeds
=
[]
self
.
disconnecteds
=
[]
self
.
connecteds
=
[]
self
.
conn_failures
=
[]
def
__str__
(
self
):
return
str
(
self
.
messages
)
+
str
(
self
.
messagedicts
)
+
str
(
self
.
publisheds
)
+
\
...
...
@@ -54,6 +55,9 @@ class Callbacks:
self
.
connecteds
.
append
({
"userdata"
:
userdata
,
"flags"
:
flags
,
"reasonCode"
:
reasonCode
,
"properties"
:
properties
})
def
on_connect_fail
(
self
,
client
,
userdata
):
self
.
conn_failures
.
append
({
"userdata"
:
userdata
})
def
wait
(
self
,
alist
,
timeout
=
2
):
interval
=
0.2
total
=
0
...
...
@@ -62,6 +66,9 @@ class Callbacks:
total
+=
interval
return
alist
.
pop
(
0
)
# if len(alist) > 0 else None
def
wait_connect_fail
(
self
):
return
self
.
wait
(
self
.
conn_failures
,
timeout
=
10
)
def
wait_connected
(
self
):
return
self
.
wait
(
self
.
connecteds
)
...
...
@@ -105,6 +112,7 @@ class Callbacks:
client
.
on_unsubscribe
=
self
.
unsubscribed
client
.
on_message
=
self
.
on_message
client
.
on_disconnect
=
self
.
on_disconnect
client
.
on_connect_fail
=
self
.
on_connect_fail
client
.
on_log
=
self
.
on_log
...
...
@@ -224,6 +232,17 @@ class Test(unittest.TestCase):
callback
.
clear
()
aclient
.
loop_stop
()
def
test_connect_fail
(
self
):
clientid
=
"connection failure"
fclient
,
fcallback
=
self
.
new_client
(
clientid
)
fclient
.
user_data_set
(
1
)
fclient
.
connect_async
(
"localhost"
,
1
)
response
=
fcallback
.
wait_connect_fail
()
self
.
assertEqual
(
response
[
"userdata"
],
1
)
fclient
.
loop_stop
()
def
test_retained_message
(
self
):
qos0topic
=
"fromb/qos 0"
qos1topic
=
"fromb/qos 1"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录