惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

diss带码

datart系列04:基于threejs自定义插件3D-MAP datart系列03:图表插件开发 datart系列02:图表插件开发作品 datart系列01:图表插件开发作品大赛 我的2021年新Mac设置 小程序:Painter画布 小程序:webview + PDF预览 小程序:数十年Lite 验证码:五福临门 开源论坛:社区选择Discourse 牛年开篇:网站拜年 github自定义主页秀 Davinci-二次开发系列08:mongoDB(JDBC查询)四种解决方案 Davinci-二次开发系列07:BI新元素尝试 Davinci-二次开发系列06:导出excel合并单元格 Metabase-BI系列12:0.35版本表达式是真的香 Davinci-二次开发系列05:echarts-gl map3D扩展 Davinci-二次开发系列04:自定义水印扩展 Davinci-二次开发系列03:区域地图下钻与选择
小程序:mqtt+webview控制显示内容
2021-07-22 · via diss带码

发表于 | 分类于 | |

看到mqtt+webview似乎不知道能做什么,mqtt微消息服务更适用iot物联网,这个应该熟悉,但是似乎还是得从webview说起。webview的场景不仅仅是手机端的APP或者小程序用到,好多基于android主板显示的设备、大屏等webview都发挥了很大的作用。这里我们一是验证小程序的mqtt,二是通过mqtt控制设备自动切换显示内容,这样试想一下,其实就是远程操控设备显示内容的一种很好的方式。

效果

01

mqtt小程序端

需要mqtt.js客户端库:https://github.com/mqttjs/MQTT.js

小程序配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const app = getApp()
var mqtt = require('../../../utils/mqtt.min')
var client = null

Page({

/**
* 页面的初始数据
*/
data: {
webUrl: 'https://www.baidu.com'
},

/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.connnectMqtt()
},

connnectMqtt: function (){
var that = this
const options = {
connectTimeout: 4000, // 超时时间
clientId: 'mqtt_' + Math.random().toString(16).substr(2, 8),
port: 8083, //重点注意这个坑
}

client = mqtt.connect("wx://xx.xx.xx.xx/mqtt", options);
client.on('reconnect', (error) => {
console.log('正在重连:', error)
})

client.on('error', (error) => {
console.log('连接失败:', error)
})
client.on('connect', (e) => {
console.log('成功连接服务器')
       //订阅一个主题
client.subscribe('test', {
qos: 0
}, function(err) {
if (!err) {
console.log("订阅成功")
}
})
})
client.on('message', function (topic, message) {
console.log('received msg:' + message.toString());
that.setData({
webUrl: message.toString()
})
console.log(that.data.webUrl)
})
}
})
1
<web-view src="{{webUrl}}" bindmessage="getmessage"></web-view>

mqtt服务端安装

EMQ X 是一款完全开源,高度可伸缩,高可用的分布式 MQTT 消息服务器

git地址:https://gitee.com/emqx/emqx

docker安装步骤

1
$ docker search emqx // 查看版本
1
$ docker pull emqx/emqx // 拉取镜像
1
$ docker run -dit --name emqx -p 18083:18083 -p 1883:1883 -p 8083:8083 -p 8084:8084 emqx/emqx:latest // 运行
1
$ docker exec -it  emqx /bin/sh // 进入命名

web管理界面

http://127.0.0.1:18083

#账号: admin

#密码: public

端口介绍

1883:MQTT 协议端口

8883:MQTT/SSL 端口

8083:MQTT/WebSocket 端口

8080:HTTP API 端口

18083:Dashboard 管理控制台端口

mqtt客户端工具

我们没必要写后台代码,直接用个mqtt客户端工具做测试,用的MQTTX,这个就根据个人习惯选了

MQTTX地址:https://github.com/emqx/MQTTX/releases

安装完成配置验证即可。

代码

diss

已提交github,扫码关注公众号(diss带码),回复:webview,获得源码github地址