gpt4 book ai didi

node.js - 无法使用 Node.js mqtt 库连接到 Eclipse Mosquitto 代理

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:02 28 4
gpt4 key购买 nike

我正在 172.17.0.2:1883 上的本地 Docker 容器上运行 Eclipse Mosquitto

我能够使用 MQTT.fx 客户端发布/订阅,但现在尝试从 Node 连接到代理,但无法连接。

主题是“传感器”

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
e747c86b6ec1 eclipse-mosquitto "/docker-entrypoint.…" 5 hours ago Up 5 hours 0.0.0.0:1883->1883/tcp, 0.0.0.0:9001->9001/tcp adoring_varahamihira

当我检查IP地址时

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' e747c86b6ec1
172.17.0.2

我正在尝试使用以下 Node 服务器连接到我的 MQTT 代理,但“连接”事件似乎从未发生。

// Note: I have an express server running but didn't include 
const mqtt = require("mqtt")

var MQTT_TOPIC = "sensors";
var MQTT_ADDR = "mqtt://172.17.0.2";
var MQTT_PORT = 1883;

var client = mqtt.connect(MQTT_ADDR, {
port: MQTT_PORT,
clientId: 'bgtestnodejs232323',
protocolId: 'MQIsdp',
protocolVersion: 3,
connectTimeout: 1000,
debug: true
});

client.on('connect', function () {
console.log("connected!")
});

client.on('error', function (err) {
console.log(err)``
client.end()
})

当我运行时:

console.log(util.inspect(client))

输出为:

MqttClient {
options:
{ protocol: 'mqtt',
slashes: true,
auth: null,
host: '172.17.0.2',
port: 1883,
hostname: '172.17.0.2',
hash: null,
search: null,
query: [Object: null prototype] {},
pathname: null,
path: null,
href: 'mqtt://172.17.0.2',
clientId: 'bgtestnodejs232323',
protocolId: 'MQIsdp',
protocolVersion: 3,
connectTimeout: 1000,
debug: true,
defaultProtocol: 'mqtt',
keepalive: 60,
reschedulePings: true,
reconnectPeriod: 1000,
clean: true,
resubscribe: true,
customHandleAcks: [Function] },
streamBuilder: [Function: wrapper],
outgoingStore: Store { options: { clean: true }, _inflights: Map {} },
incomingStore: Store { options: { clean: true }, _inflights: Map {} },
queueQoSZero: true,
_resubscribeTopics: {},
messageIdToTopic: {},
pingTimer: null,
connected: false,
disconnecting: false,
queue: [],
connackTimer:
Timeout {
_called: false,
_idleTimeout: 1000,
_idlePrev: [TimersList],
_idleNext: [TimersList],
_idleStart: 478,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: null,
_destroyed: false,
[Symbol(unrefed)]: false,
[Symbol(asyncId)]: 9,
[Symbol(triggerId)]: 1 },
reconnectTimer: null,
_storeProcessing: false,
_packetIdsDuringStoreProcessing: {},
nextId: 26898,
outgoing: {},
_firstConnection: true,
_events:
[Object: null prototype] {
close: [ [Function], [Function], [Function] ],
connect: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
stream:
Socket {
connecting: true,
_hadError: false,
_handle:
TCP {
reading: false,
onread: [Function: onStreamRead],
onconnection: null,
[Symbol(owner)]: [Circular] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [Writable],
pipesCount: 1,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: true,
paused: false,
emitClose: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
_events:
[Object: null prototype] {
end: [Array],
data: [Function: ondata],
error: [Function: nop],
close: [Function] },
_eventsCount: 4,
_maxListeners: 1000,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 34,
writing: false,
corked: 1,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: [Object],
lastBufferedRequest: [Object],
pendingcb: 9,
prefinished: false,
errorEmitted: false,
emitClose: false,
bufferedRequestCount: 9,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 5,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } }

我一直在尝试这个答案中的建议:Why is MQTT not connecting with NodeJS?

感谢您的帮助!

最佳答案

由 Docker 引擎分发给 Docker 容器的 IP 地址范围 172.17.0.0/16 是 RFC1918 的一部分。供私有(private)使用的套装。

对于 Docker,它们在内部“桥接”网络上使用,并且只能从其他 Docker 容器或托管 Docker 引擎的计算机进行访问。

mosquitto 容器信息的 0.0.0.0:1883->1883/tcp, 0.0.0.0:9001->9001/tcp 输出显示这些端口已公开并映射到主机 IP 地址。如果您将托管 Docker 引擎的计算机的 IP 地址更改为 172.17.0.2,您应该能够连接。

关于node.js - 无法使用 Node.js mqtt 库连接到 Eclipse Mosquitto 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60268030/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com