作为 this 问题的扩展并根据答案,我使用 websockets
连接到 MQTT 代理,使用 dash_mqtt
以 this app script 作为参考。但我无法使用以下 app.py
中的配置发送或接收数据。
import dash_mqtt
import dash
from dash.dependencies import Input, Output, State
import dash_html_components as html
import dash_core_components as dcc
import flask
import json
import paho.mqtt.client as mqtt
TEST_SERVER = '172.17.0.2'
TEST_SERVER_PORT = 1883
TEST_SERVER_PATH = 'mqtt'
MESSAGE_OUT_TOPIC = 'testtopic'
MESSAGE_IN_TOPIC = 'testtopic'
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
server = flask.Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True
app.layout = html.Div([
dash_mqtt.DashMqtt(
id='mqtt',
broker_url=TEST_SERVER,
broker_port = TEST_SERVER_PORT,
broker_path = TEST_SERVER_PATH,
topics=[MESSAGE_IN_TOPIC]
),
html.H1('MQTT echo'),
html.P('MQTT echo server to ' + TEST_SERVER + ' on port ' + str(TEST_SERVER_PORT)),
dcc.Input(
id='message_to_send',
placeholder='message to send',
debounce = True),
html.Button('Send',id='send'),
html.Div(id='return_message')
])
@app.callback(
Output('mqtt', 'message'),
Input('send', 'n_clicks'),
State('message_to_send', 'value')
)
def display_output(n_clicks, message_payload):
if n_clicks:
return {
'topic': MESSAGE_OUT_TOPIC,
'payload' : message_payload
}
return dash.no_update
if __name__ == '__main__':
app.run_server(host='0.0.0.0',port=8050,debug=True)
简而言之,
eclipse mqtt
代理
在与 IP 地址关联的 docker 容器中
172.17.0.2
监听端口 1883
。8050
。mosquitto.config 文件的所有基本配置都已完成,以便它侦听和响应外部通信。此外,此应用程序运行成功,日志中没有错误,但无法与 MQTT 代理进行通信。
我应该进行哪些配置更改才能连接到代理?
谢谢
编辑:浏览器控制台显示WebSocket connection to 'ws://172.17.0.2:1883/mqtt' failed:
mosquitto.conf
# Config file for mosquitto
# =================================================================
# Listeners
# =================================================================
# listener port-number [ip address/host name/unix socket path]
listener 1883
# =================================================================
# Persistence
# =================================================================
persistence True
# =================================================================
# Security
# =================================================================
# Defaults to false, unless there are no listeners defined in the configuration
# file, in which case it is set to true, but connections are only allowed from
# the local machine.
allow_anonymous True
答案 0 :(得分:0)
如评论中所述,
对于 TCP 上的原生 MQTT 和 Websocket 上的 MQTT,您不能使用相同的端口,因此您需要向 mosquitto.conf
添加第二个侦听器
例如
listener 1884
protocol websockets
并将应用中的端口更改为 1884