MQTT网页客户端连接MQTT服务器的问题

MQTT网页客户端连接MQTT服务器的问题WebSocket connection to ‘ws://XXX:1883/’ failed: Connection closed before receiving a handshake response

改为使用WebSocket_URL连接

<template>
  <div>4</div>
</template>
<script>
import mqtt from "mqtt";
// <script src="https://unpkg.com/mqtt/dist/mqtt.min.js">
// const mqtt = require("mqtt");
// WebSocket 连接字符串
const WebSocket_URL = "ws://127.0.0.1:8083/mqtt";
// TCP/TLS 连接字符串,仅限 Node.js 环境
const TCP_URL = "mqtt://127.0.0.1:1883";
const TCP_TLS_URL = "mqtts://127.0.0.1:8883";
// 连接选项
const options = {
  // 超时时间
  connectTimeout: 4000,
  // 认证信息
  clientId: "test1",
  username: "客户端1",
  // password: 'emqx',
  // 心跳时间
  keepalive: 60,
  clean: true
};
const client = mqtt.connect(WebSocket_URL, options);
// 连接成功后初始化订阅
client.on("connect", () => {
  console.log("Connected to", WebSocket_URL);
  // 订阅主题
  client.subscribe("gczd/+/data", err => {
    console.log(err || "订阅成功");
  });
});
// 为 message 时间添加处理函数
client.on("message", (topic, message) => {
  console.log("收到来自", topic, "的消息:", message.toString());
});
export default {
  name: "App"
};

</script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常感谢您的提问。对于您的问题,我很乐意提供帮助。 一个基本的mqtt网页客户端包括以下几个步骤: 1. 设置mqtt服务器相关参数,如地址、端口、用户名、密码等。 2. 创建mqtt客户端连接mqtt服务器。 3. 订阅mqtt主题或发布mqtt消息。 4. 处理mqtt消息的回调函数。 下面是一个简单的mqtt网页客户端的代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>MQTT Web Client</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js"></script> </head> <body> <div> <label for="server-url">Server URL:</label> <input id="server-url" type="text" value="ws://broker.hivemq.com:8000/mqtt"> </div> <div> <label for="topic">Topic:</label> <input id="topic" type="text" value="my/topic"> </div> <div> <label for="message">Message:</label> <input id="message" type="text"> <button id="publish-button">Publish</button> </div> <div id="messages"></div> <script> var serverUrl = document.getElementById("server-url").value; var topic = document.getElementById("topic").value; var client = new Paho.MQTT.Client(serverUrl, "mqtt-web-client-" + Math.random().toString(16).substr(2, 8)); client.onConnectionLost = onConnectionLost; client.onMessageArrived = onMessageArrived; client.connect({ onSuccess: onConnect, onFailure: onConnectFailure, userName: "username", password: "password" }); function onConnect() { console.log("Connected to " + serverUrl); client.subscribe(topic); } function onConnectFailure(error) { console.log("Connection failed: " + error.errorMessage); } function onConnectionLost(responseObject) { console.log("Connection lost: " + responseObject.errorMessage); } function onMessageArrived(message) { console.log("Received message: " + message.payloadString); var div = document.createElement("div"); div.textContent = message.payloadString; document.getElementById("messages").appendChild(div); } document.getElementById("publish-button").addEventListener("click", function() { var message = document.getElementById("message").value; var payload = new Paho.MQTT.Message(message); payload.destinationName = topic; client.send(payload); }); </script> </body> </html> ``` 需要注意的是,以上代码仅为示例代码,具体实现需要根据具体的业务场景进行调整。同时,为了保证mqtt客户端的安全性,建议在实际开发中对mqtt服务器进行认证和授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值