mqtt测试工具

为了方便测试连接mqtt,自己写了个测试的页面,界面如下图:


image.png

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="shortcut icon" href="#" />
    <link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.14.1/theme-chalk/index.css" rel="stylesheet" />
    <link rel="stylesheet" href="//at.alicdn.com/t/font_2292707_b2m1g0ybo1.css">
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            color: #fff;
            font-size: 16px;
        }

        * {
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
        }

        #app {
            height: 100%;
            text-align: center;
            overflow: hidden;
        }

        .el-container {
            height: 100%;
        }

        .el-header,
        .el-footer {
            color: #000;
            line-height: 60px;
            background-color: #d7e2f1;
        }

        .el-aside {
            height: 100%;
            color: #333;
            background-color: #D3DCE6;
        }

        .history-wrap {
            height: calc(100% - 70px);
            overflow: auto;
        }

        .history-link {
            color: #444;
            font-size: 14px;
            line-height: 40px;
            height: 40px;
            cursor: pointer;
        }

        i.el-icon-close {
            display: none;
        }

        .history-link:hover {
            color: #51a5fa;
        }

        .history-link:hover i.el-icon-close {
            display: inline-block;
        }

        .active {
            color: #1b89f7;
        }

        .el-main {
            height: 100%;
            color: #333;
            background-color: #E9EEF3;
        }

        .row {
            margin-bottom: 20px;
        }

        .el-button {
            width: 100px;
        }

        .el-icon--right {
            margin-right: 10px;
        }

        .show-content {
            height: calc(100% - 260px);
            padding: 20px;
            color: #fff;
            background-color: #000;
            text-align: left;
            max-height: 339px;
        }

        [v-clock] {
            display: none;
        }

        .text {
            text-align: left;
        }

        .tip {
            font-size: 12px;
            color: #ccc;
        }

        .message-text {
            overflow: auto;
            height: 100%;
            padding: 10px 0;
            word-break: break-all;
            color: #fff;
            background-color: #000;
        }

        .iconfont {
            font-size: 14px;
            margin-right: 5px;
        }
    </style>
    <title>mqtt-test</title>
</head>

<body>
    <div id="app" v-clock>
        <el-container>
            <el-header>MQTT测试工具</el-header>
            <el-container>
                <el-aside width="200px">
                    <h3>历史记录</h3>
                    <div class="history-wrap">
                        <div @click="onClickHistory(index)" class="history-link" :class="{active: index==currentIndex}"
                            v-for="(item, index) in historyList" :key="index">{{item.ip}} <i
                                @click.stop="onDelete(index)" class="el-icon-close"></i> </div>
                    </div>
                </el-aside>
                <el-main>
                    <div class="row">

                        <el-row class="row" :gutter="20" type="flex" align="middle">
                            <el-col :span="6">
                                <el-row type="flex" align="middle">
                                    <el-col :span="4">IP</el-col>
                                    <el-col :span="20" :push="1">
                                        <el-input v-model="ip" placeholder="请输入ip"></el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="6">
                                <el-row type="flex" align="middle">
                                    <el-col :span="4">端口</el-col>
                                    <el-col :span="20" :push="1">
                                        <el-input v-model="port" placeholder="请输入端口"></el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="6">
                                <el-row type="flex" align="middle">
                                    <el-col :span="4">路径</el-col>
                                    <el-col :span="20" :push="1">
                                        <el-input v-model="path" placeholder="请输入路径"></el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="6">
                                <el-button :disabled="hasConnect" @click="onConnect" type="primary" size="medium"><i
                                        class="iconfont icon-lianjie"></i>连接</el-button>
                                <el-button :disabled="!hasConnect" @click="onDisconnect" type="primary" size="medium"><i
                                        class="iconfont icon-duankailianjie"></i>断开</el-button>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="row">
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="6">
                                <el-row type="flex" align="middle">
                                    <el-col :span="6">订阅主题</el-col>
                                    <el-col :span="18" :push="1">
                                        <el-input v-model="sub" placeholder="请输入订阅主题"></el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="6">
                                <el-button :disabled="!hasConnect" @click="onSub" prefix-icon="" type="primary"
                                    size="medium"><i class="iconfont icon-dingyue"></i>订阅</el-button>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="row">
                        <el-row :gutter="20" type="flex" align="middle">
                            <el-col :span="6">
                                <el-row type="flex" align="middle">
                                    <el-col :span="6">发布主题</el-col>
                                    <el-col :span="18" :push="1">
                                        <el-input v-model="pub" placeholder="请输入发布订阅主题"></el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="10" :push="1">
                                <el-row type="flex" align="middle">
                                    <el-col :span="4">发布内容</el-col>
                                    <el-col :span="20" :push="1">
                                        <el-input v-model="content" type="textarea" rows="5" placeholder="请输入发布内容">
                                        </el-input>
                                    </el-col>
                                </el-row>
                            </el-col>
                            <el-col :span="6" :push="1">
                                <el-button :disabled="!hasConnect" @click="onPub" type="primary" size="medium"><i
                                        class="iconfont icon-fabu"></i>发布</el-button>
                                <el-button :disabled="!hasConnect" @click="onClearMessage" type="primary" size="medium">
                                    <i class="iconfont icon-qingchu"></i>清除</el-button>
                            </el-col>
                        </el-row>
                    </div>
                    <div class="show-content">
                        <el-row>
                            <el-col :span="12">
                                接受的消息:
                            </el-col>
                            <el-col :span="12">
                                发布的消息
                            </el-col>
                        </el-row>
                        <div class="message-text">
                            <div class="box">
                                <el-row>
                                    <el-col :span="12">
                                        <div class="sub-box">
                                            <div class="text" v-for="(item,index) in subList" :key="index">
                                                <span v-html="item"></span>
                                            </div>
                                            <div class="tip" v-if="subList.length == 0">暂未收到消息</div>
                                        </div>
                                    </el-col>
                                    <el-col :span="12">
                                        <div class="text" v-for="(item,index) in pubList" :key="index">
                                            <span v-html="item"></span>
                                        </div>
                                    </el-col>
                                </el-row>
                            </div>
                        </div>
                    </div>
                </el-main>
            </el-container>
        </el-container>
    </div>
</body>

</html>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.14.1/index.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/mqtt/4.2.6/mqtt.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data() {
            return {
                ip: 'xx.xx.xx.xx',
                port: '9001',
                path: '/mqtt',
                sub: 'test',
                pub: 'test',
                content: 'hello',
                pubList: [],
                subList: [],
                client: null,
                hasConnect: false,
                historyList: [],
                currentIndex: -1
            };
        },
        computed: {
            num() {
                return this.subList.length > 0 ? '12' : '0'
            }
        },
        watch: {
            subList(n, o) {
                this.$nextTick(() => {
                    const ele = document.querySelector('.message-text')
                    ele.scrollTop = ele.scrollHeight;
                });
            },
            historyList(n) {
                console.log(n)
            }
        },
        methods: {
            initStatus() {
                this.hasConnect = false;
                this.client && this.client.end()
                this.client = null;
            },
            onDelete(index) {
                this.historyList.splice(index, 1)
                this.setHistoryList(index - 1)
            },
            onClickHistory(index) {
                this.currentIndex = index
                if (this.historyList.length == 0) return;
                const {
                    ip,
                    port,
                    path,
                    sub,
                    pub,
                    content
                } = this.historyList[index]
                this.ip = ip
                this.port = port
                this.path = path
                this.sub = sub
                this.pub = pub
                this.content = content
                this.initStatus()
            },
            successTip(message) {
                this.$message({
                    type: 'success',
                    message
                })
            },
            now() {
                return moment().format('YYYY-MM-DD HH:mm:ss')
            },
            formatMessage(topic, message) {
                return `<span style="color: #00A000;">| ${this.now()} | 主题名称: ${topic} |</span>
                 <br>
                 <span style="word-break: break-all;line-height:1.2;padding:5px;">${message}</span>
                `
            },
            onSub() {
                this.successTip(`订阅了主题${this.sub}`)
                this.client && this.client.subscribe(this.sub)
                this.updateHistoryList()
            },
            onPub() {
                this.pubList.push(this.formatMessage(this.pub, this.content))
                this.client && this.client.publish(this.pub, this.content);
                this.updateHistoryList()
            },
            onDisconnect() {
                this.initStatus()
            },
            onConnect() {
                this.client = mqtt.connect(`ws://${this.ip}:${this.port}${this.path}`);
                this.client.on('connect', this.onConnectMqtt)
                this.client.on('error', this.onError)
                this.client.on('message', this.onMessage)
                this.client.on('close', this.onClose)
                this.historyList.unshift({
                    ip: this.ip,
                    port: this.port,
                    path: this.path
                })
                this.setHistoryList()
            },
            updateHistoryList() {
                this.historyList[this.currentIndex] = {
                    ...this.historyList[this.currentIndex],
                    pub: this.pub,
                    sub: this.sub,
                    content: this.content
                }
                this.setHistoryList(this.currentIndex)
            },
            setHistoryList(index = 0) {
                if (index < 0) index = 0;
                localStorage.setItem('history', JSON.stringify(this.historyList))
                this.currentIndex = index
            },
            onConnectMqtt(e) {
                console.log(e)
                this.hasConnect = true
                this.successTip('连接成功')
            },
            onMessage(topic, payload) {
                console.log(topic, payload.toString())
                this.subList.push(this.formatMessage(topic, payload.toString()))
            },
            onClearMessage() {
                this.subList = []
                this.pubList = []
            },
            onClose() {
                if (!this.hasConnect) return this.$message({
                    type: 'info',
                    message: '已断开连接'
                })
                this.hasConnect = false;
                this.$message.error('连接不成功,连接关闭!')
                this.client.end()
            },
            onError(err) {
                console.error(err)
                this.$message.error(`出现错误了错误信息是${JSON.stringify(err)}`)
                this.client.end()
            },
            init() {
                this.historyList = localStorage.getItem('history') ? JSON.parse(localStorage.getItem(
                    'history')) : []
                this.onClickHistory(0)
            }
        },
        mounted() {
            this.init()
        },
    });
    Vue.config.errorHandler = function (err, vm, info) {
        console.log(`Error: ${err.toString()}\nInfo: ${info}`);
        app.$message.error(err.message)
    }
</script>

插件全部为外链接,可以直接新建html文件拷入,双击打开使用

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容