博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Paho JavaScript Client 基于websocket实现 mqtt客户端
阅读量:2022 次
发布时间:2019-04-28

本文共 1048 字,大约阅读时间需要 3 分钟。

mqtt服务器采用activemq实现。

  1. 首先配置activemq支持websocket.
    具体配置,。
  2. 编写客户端页面,引入Paho官方客户端js库。
    具体配置,
    贴出关键代码如下:
// Create a client instance//注意不要在client前写varclient = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");// set callback handlersclient.onConnectionLost = onConnectionLost;client.onMessageArrived = onMessageArrived;// connect the clientclient.connect({onSuccess:onConnect});// called when the client connectsfunction onConnect() {
// Once a connection has been made, make a subscription and send a message. console.log("onConnect"); client.subscribe("World"); message = new Paho.MQTT.Message("Hello"); message.destinationName = "World"; client.send(message);}// called when the client loses its connectionfunction onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) { console.log("onConnectionLost:"+responseObject.errorMessage); }}// called when a message arrivesfunction onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);}

注意不要在client前写var

否则你可能会出现一下错误:

转载地址:http://idgxf.baihongyu.com/

你可能感兴趣的文章
异常、模块、文件读写
查看>>
数据框的合并排序、描述统计、分箱
查看>>
日期时间变量的处理
查看>>
爬虫 selenium
查看>>
python爬虫
查看>>
流程控制语句
查看>>
Python 数据可视化之matpotlib画图
查看>>
决策树 Decision Tree
查看>>
用PBD制作餐饮店KPI分析仪-入门篇
查看>>
c++启动另一个软件
查看>>
无盘安装系统之Windows 7篇
查看>>
autorun.inf 配置说明
查看>>
U盘制作电脑启动钥匙
查看>>
Linux下c编程系统函数调用Signal信号的介绍
查看>>
linux内核介绍之系统调用过程
查看>>
linux内核介绍之开机启动过程
查看>>
linux下c编程之信号量semget,semop,semctl函数
查看>>
linux下c编程之内存共享shemget函数的实现及案例-bmi体重身高测试2
查看>>
enum枚举介绍
查看>>
什么是博士?
查看>>