javascript-mqtt
js client使用paho-mqtt,官网地址:http://www.eclipse.org/paho/,参考http://www.eclipse.org/paho/clients/js/官网给出例子Getting Started,写出下面简单的测试代码。
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
57
58
59
60
61
62
63
|
<!DOCTYPE html> <html> <head> <meta charset= "utf-8" > <title>Centos7 mosquitto test</title> <script src= "jquery-3.2.1.min.js" ></script> <script src= "paho-mqtt-min.js" ></script> <script type= "text/javascript" > var hostname = "csxyz.xyz" ; //主机IP 106.14.181.32 var hostPort = 9001; //port var sub_count = 0; var client; function MQTTconnect() { client = new Paho.MQTT.Client(hostname, hostPort, "test" ); client.onMessageArrived = onMessageArrived; client.connect({onSuccess:onConnect}); console.log( "Host=" + hostname + ", port=" + hostPort); }; // called when the client connects function onConnect() { console.log( "onConnect succeeds" ); // client.subscribe('topic'); client.subscribe( '#' ); //订阅所有主题 } // called when a message arrives function onMessageArrived(message) { var topic = message.destinationName; var payload = message.payloadString; console.log( "onMessageArrived:" + payload); sub_count++; $( '#sub_count' ).val( '收到订阅' + sub_count + '条' ); $( '#sub_message' ).prepend( '<li>' + topic + ' = ' + payload + '</li>' ); } function button_onclick(){ var topic = $( '#topicsend' ).val(); var text = $( '#textsend' ).val(); if (topic== '' || text== '' ) { alert( "noTopic or noText" ); return ; } var message = new Paho.MQTT.Message(text); message.destinationName = topic; message.qos=0; client.send(message); }; $(document).ready( function () { MQTTconnect(); }); </script> </head> <body> <h1>mosquitto pub</h1> topic:<input type= 'text' id= 'topicsend' size= "60" /> <br> text :<input type= 'text' id= 'textsend' size= "60" /> <input type= 'button' value= "send" id= 'btn' onclick= "button_onclick()" /> </br> <h1>mosquitto sub</h1> <input type= 'text' id= 'sub_count' disabled /> <ul id= 'sub_message' ></ul> </body> </html> |
html在本地运行,测试结果:
在censtos的apache空间根目录下创建文件夹iot,将index.html和需要包含的js文件拷贝过去,使用浏览器打开csxyz.xyz/iot,也可以得到上面的结果。注意,文件上传的时候是在root用户下,需要更改权限,否则运行会报错的。
javascript-mqtt的更多相关文章
- javascript mqtt 发布订阅消息
js client使用paho-mqtt,官网地址:http://www.eclipse.org/paho/,参考http://www.eclipse.org/paho/clients/js/官网给出 ...
- 使用 WebSocket 客户端连接 MQTT 服务器
简介 近年来随着 Web 前端的快速发展,浏览器新特性层出不穷,越来越多的应用可以在浏览器端或通过浏览器渲染引擎实现,Web 应用的即时通信方式 WebSocket 得到了广泛的应用. WebSock ...
- getting-started-with-mqtt
来自:https://dzone.com/refcardz/getting-started-with-mqtt SECTION 1 Why MQTT? The Internet of Things ( ...
- 未读消息(小红点),前端与 RabbitMQ实时消息推送实践,贼简单~
前几天粉丝群里有个小伙伴问过:web 页面的未读消息(小红点)怎么实现比较简单,刚好本周手头有类似的开发任务,索性就整理出来供小伙伴们参考,没准哪天就能用得上呢. 之前在 <springboot ...
- 使用javascript连接mqtt协议(自动重连问题)
因为之前是在rabbitmq的插件"RabbitMQ Web MQTT plugin "中看到使用了mqttws31.js的实例,由于对mqttws31不了解,网上下载了连接成功, ...
- JavaScript使用MQTT
1.MQTT Server使用EMQTTD开源库,自行安装配置: 2.JS使用Websocket连接通信. 3.JS的MQTT库为paho-mqtt,git地址:https://github.com/ ...
- TCP/IP, WebSocket 和 MQTT
按照OSI网络分层模型,IP是网络层协议,TCP是传输层协议,而HTTP和MQTT是应用层的协议.在这三者之间, TCP是HTTP和MQTT底层的协议.大家对HTTP很熟悉,这里简要介绍下MQTT.M ...
- MQTT 消息 发布 订阅
当连接向一个mqtt服务器时,clientId必须是唯一的.设置一样,导致client.setCallback总是走到 connectionLost回调.报connection reset.调查一天才 ...
- MQTT和WebSocket
严格来说,MQTT跟WebSocket关系不大.他们不是在一个层级的. MQTT和TCP.WebSocket的关系可以用下图一目了然: 参考资料: http://www.zhihu.com/q ...
- 在线聊天室的实现(1)--websocket协议和javascript版的api
前言: 大家刚学socket编程的时候, 往往以聊天室作为学习DEMO, 实现简单且上手容易. 该Demo被不同语言实现和演绎, 网上相关资料亦不胜枚举. 以至于很多技术书籍在讲解网络相关的编程时, ...
随机推荐
- regular.js
//手机号验证 var regMobile=/^1[3,5,8]\d{9}$/; //固定电话 var regPhone=/^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|( ...
- csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate
Domain: FluentNhibernateLocalSessionFactoryObject.cs using System; using System.Collections.Generic; ...
- Schwartz–Zippel lemma
鬼知道老师从哪儿扒的这东西啊,.... 百度了一下毛都没有啊,维基百科看不懂啊.. 定理 一个$m$元$n$次多项式,在域$F$内随机给每个变量赋值 等于零的概率小于$\dfrac{n}{|F|}$ ...
- imooc《JavaScript深入浅出》上的一个 arraysSimilar 函数
任务 请在 index.html 文件中,编写 arraysSimilar 函数,实现判断传入的两个数组是否相似.具体需求: 数组中的成员类型相同,顺序可以不同.例如 [1, true] 与 [fal ...
- 七牛云java(服务端)通用工具类
前言 需要安装lombok插件. 功能列表 上传本地文件 上传Base64图片 获取文件访问地址 上传MultipartFile 代码 pom.xml <dependency> <g ...
- 项目启动时发生NOT found
一直想记录一下这个小问题 情景: 我昨晚美滋滋的做完功能,测了测没bug提交到git上之后就屁颠屁颠的回家了,结果今天早上来就失了智,git pull拉了一下代码后,一运行,我去,我的页面呢,页面上直 ...
- 转:drupal常用api
drupal常用api 最短的函数 // 语言字串,除了可以获取对应语言外,还可以设置字串变量.可以是!var, @var或 %var,%var就添加元素外层.@var会过滤HTML,!var会原 ...
- SGCC_UAP启动停留在initializing java tooling(1%)
找到uap的安装目录,eclipse文件夹下的eclipse.ini,用EditPlus打开,添加下面两行 -vmC:\Program Files\Java\jdk1.6.0_43\bin\ 在-vm ...
- [Android] 设置AlertDialog中按钮的可用(Enable)状态
弹出一个保存文件的对话框,要控制输入内容限制,同时内容为空时保存按钮不可用. 原文地址请保留http://www.cnblogs.com/rossoneri/p/4140184.html 直接上代码: ...
- Microsoft .NET Core 1.0.0 VS 2015 Tooling Preview 2 Uninstall Failed
卸载过程中总是卸载失败报0x80070001:函数不明确错误.转遍了各大论坛和QQ,最终还是在stackoverflow上找到了答案... 原因是我卸载时选择的DotNetCore.1.0.0-VS2 ...