对象: REPL on MQTT
约 448 字大约 1 分钟
2026-03-19
构建固件和导入到JS
mqtt 对象由 repl 提供:
import { mqtt } from 'repl'简介
MQTT REPL 对象
提供通过 MQTT 协议进行 REPL 远程控制的功能。 适用于物联网场景下的远程设备管理。
示例:
import * as repl from "repl"
import { MQTT } from "mqtt"
// 创建 MQTT 客户端
const mqtt = new MQTT()
mqtt.setup({
host: "mqtt.eclipse.org",
port: 1883,
clientId: "beshell-device"
})
// 启动 MQTT REPL
repl.mqtt.start(mqtt)
// 停止 MQTT REPL
repl.mqtt.stop()对象方法
函数 start
原型: start (mqtt:MQTT, topicIn:string, topicOut:string)
启动 MQTT REPL
将 MQTT 连接设置为 REPL 通信通道。 默认使用主题格式:beshell/repl/in/<mac> 和 beshell/repl/out/<mac>
示例:
import * as repl from "repl"
import { MQTT } from "mqtt"
const mqtt = new MQTT()
mqtt.setup({ host: "broker.hivemq.com", port: 1883 })
// 使用默认主题(基于设备 MAC 地址)
repl.mqtt.start(mqtt)
// 或使用自定义主题
repl.mqtt.start(mqtt, "mydevice/cmd", "mydevice/resp")参数:
mqtt
类型MQTT
参数说明MQTT 客户端实例
topicIn
类型string
参数说明输入主题(命令),默认为
beshell/repl/in/<mac>topicOut
类型string
参数说明输出主题(响应),默认为
beshell/repl/out/<mac>
异常:
- MQTT REPL 已启动时抛出错误
返回值:
类型undefined
函数 stop
原型: stop ()
停止 MQTT REPL
关闭 MQTT REPL 通道并取消订阅相关主题。
示例:
import * as repl from "repl"
// 停止 MQTT REPL
repl.mqtt.stop()返回值:
类型undefined
