npm install mqtt -S
utils下面新建mqtt页面
- import { MqttClient, OnMessageCallback } from 'mqtt';
- import mqtt from 'mqtt';
-
- class MQTT {
- url: string; // mqtt地址
- topic: string; //订阅地址
- client!: MqttClient;
- constructor(topic: string) {
- this.topic = topic;
- // 虽然是mqtt但是在客户端这里必须采用websock的链接方式
- this.url = 'ws://www.liufengtec.com:8083/mqtt';
- }
-
- //初始化mqtt
- init() {
- const options = {
- host: 'www.liufengtec.com',
- port: 8083,
- endpoint: '/mqtt',
- clean: true, // 保留会话
- connectTimeout: 4000, // 超时时间
- reconnectPeriod: 4000, // 重连时间间隔
- // 认证信息
- clientId: 'mqttjs_3be2c321',
- username: 'admin',
- password: '3Ha86294',
- };
- this.client = mqtt.connect(this.url, options);
- this.client.on('error', (error: any) => {
- console.log(error);
- });
- this.client.on('reconnect', (error: Error) => {
- console.log(error);
- });
- }
- //取消订阅
- unsubscribes() {
- this.client.unsubscribe(this.topic, (error: Error) => {
- if (!error) {
- console.log(this.topic, '取消订阅成功');
- } else {
- console.log(this.topic, '取消订阅失败');
- }
- });
- }
- //连接
- link() {
- this.client.on('connect', () => {
- this.client.subscribe(this.topic, (error: any) => {
- if (!error) {
- console.log('订阅成功');
- } else {
- console.log('订阅失败');