阿里云学习之IOT物联网套件(客户端与服务端的后台数据传输)

设备端代码(mqttClient):https://help.aliyun.com/document_detail/42648.html?spm=5176.doc30579.6.569.ZEgA1g
服务端代码(aliyun-java-sdk-iot):https://help.aliyun.com/document_detail/30579.html?spm=5176.doc42693.6.597.WhnZQa
消息代码(aliyun-sdk-mns-samples):https://help.aliyun.com/document_detail/27508.html?spm=5176.doc27509.6.648.9hGlrb
https://help.aliyun.com/document_detail/42693.html?spm=5176.doc50633.6.573.wlKHyi
https://help.aliyun.com/document_detail/27509.html?spm=5176.product27412.6.652.uIO9HW
一、设备端发送消息到服务端流程
1、终端发送消息到物联网套件
SimpleClient4IOT/connectMqtt:sampleClient.publish(pubTopic,message);
String content = "客户端发送消息到服务端";
MqttMessage message = new MqttMessage(content.getBytes("utf-8"));
message.setQos();//消息qos 0:最多一次,1:至少一次
sampleClient.publish(pubTopic, message);//发送数据到某个topic
System.out.println("发送消息成功");
2、物联网套件发送消息到消息服务
通过上一篇 阿里云学习之IOT物联网套件(配置篇),讲解物联网套件和消息服务的具体配置流程和走向
规则引擎——>主题——>订阅——>队列
3、消息服务发送消息到服务端,服务端接收消息
ComsumerDemo/main:queue.popMessage();
MNSClient client = account.getMNSClient(); //this client need only initialize once
CloudQueue queue = client.getQueueRef("aliyun-iot-UGlr70uPYVM");// replace with your queue name
System.out.println("开始调试...");
for (int i = ; i < ; i++)
{
Message popMsg = queue.popMessage();
System.out.println("消息为空...");
if (popMsg != null){
System.out.println("message handle: " + popMsg.getReceiptHandle());
System.out.println("message body: " + popMsg.getMessageBodyAsString());
System.out.println("message id: " + popMsg.getMessageId());
System.out.println("message dequeue count:" + popMsg.getDequeueCount());
//<<to add your special logic.>> //remember to delete message when consume message successfully.
queue.deleteMessage(popMsg.getReceiptHandle());
System.out.println("delete message successfully.\n");
}
}
二、服务端发送消息到设备端流程
1、服务端发送消息到物联网套件
String accessKey = "账号key";
String accessSecret = "账号Secret";
try {
DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Iot", "iot.cn-shanghai.aliyuncs.com");
} catch (ClientException e) {
e.printStackTrace();
}
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端 PubRequest request = new PubRequest();
request.setProductKey("产品key");
request.setMessageContent(Base64.encodeBase64String("消息服务".getBytes()));
request.setTopicFullName("topic");// /UGlr70uPYVM/bulb/get
request.setQos(); //目前支持QoS0和QoS1 PubResponse response; try { response = client.getAcsResponse(request); System.out.println(response.getSuccess()); System.out.println(response.getErrorMessage()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); }
2、设备端接收消息
sampleClient.setCallback(new MqttCallback() {
@Override
/*public void connectionLost(Throwable cause) {
LogUtil.print("连接失败,原因:" + cause);
cause.printStackTrace();
}*/
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
LogUtil.print("接收到消息,来至Topic [" + topic + "] , 内容是:["
+ new String(message.getPayload(), "UTF-8") + "], ");
}
@Override
/*public void deliveryComplete(IMqttDeliveryToken token) {
//如果是QoS0的消息,token.resp是没有回复的
LogUtil.print("消息发送成功! " + ((token == null || token.getResponse() == null) ? "null"
: token.getResponse().getKey()));
}*/
});
阿里云学习之IOT物联网套件(客户端与服务端的后台数据传输)的更多相关文章
- 阿里云学习之IOT物联网套件(配置篇)
文档时间:2018.-1-24 首注:此文章是参照以下文章的整合与补充: https://bbs.aliyun.com/read/309106.html?amp;displayMode=1&p ...
- zeromq学习笔记2——简单的客户端和服务端测试程序
1.前言 zeromq提供了guide,http://zguide.zeromq.org/,可以帮助新手快速上手,提供了C\C++\PHP等多种语言. 2.测试程序 使用zeromq给的hwserve ...
- 在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现Web端直传,服务端签名直传并设置上传回调的实现流程
在OneThink(ThinkPHP3.2.3)中整合阿里云OSS的PHP-SDK2.0.4,实现本地文件上传流程 by shuijingwan · 2016/01/13 1.SDK安装 github ...
- TCP学习之五:客户端、服务端异步传输字符串
参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 消息发送接口: 消息接收接口: 客户端: 服务端: 消息发送类: ...
- TCP学习之三:客户端、服务端同步传输字符串
参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 一个客户端.发送一条消息 客户端: 服务端: 注意:Networ ...
- 【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验
[阿里云产品公测]结构化数据服务OTS之JavaSDK初体验 作者:阿里云用户蓝色之鹰 一.OTS简单介绍 OTS 是构建在阿里云飞天分布式系统之上的NoSQL数据库服务,提供海量结构化数据的存储和实 ...
- 【阿里云产品公测】高大上的搜索服务OpenSearch,你值得拥有!
[阿里云产品公测]高大上的搜索服务OpenSearch,你值得拥有! 作者:阿里云用户trcher 一.前言: 在OpenSearch没出来之前,就一直想给网站做个搜索功能,虽然网站本身自带搜索功 ...
- Docker学习笔记 - Docker客户端和服务端
学习内容: Docker客户端和服务端的通讯方式:client和自定义程序 Docker客户端和服务端的连接方式:socket 演示Docker客户端和服务端之间用remote-api通讯:nc ...
- Netty4 学习笔记之二:客户端与服务端心跳 demo
前言 在上一篇Netty demo 中,了解了Netty中的客户端和服务端之间的通信.这篇则介绍Netty中的心跳. 之前在Mina 中心跳的使用是通过继承 KeepAliveMessageFacto ...
随机推荐
- git上传项目全部流程
一.下载git 进入网址:https://git-scm.com/downloads: 点击中的Download 2.16.0 for Windows; 在中选择蓝色字段点击,根据电脑64或32位选择 ...
- php网站在服务器上邮件发送不了,在本地可以
标签: php邮箱 2015-11-27 13:58 879人阅读 评论(0) 收藏 举报 分类: php(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做phpmailer发送邮 ...
- dede 内容页文章标题显示不全的更改方法
找到include/taglib/arclist.lib.php 1.$titlelen = AttDef($titlelen,30);换成$titlelen = AttDef($titlelen,2 ...
- [知了堂学习笔记]_牵线Eclipse和Tomcat第一篇 —— 配置Java环境变量&&安装eclipse
一.先给他们提供一个"浪漫的"环境,比如传说中的"鹊桥"--java环境变量.哈哈! 配置java环境变量. 下载jdk,根据自己电脑的版本和操作位数选择不同的 ...
- es7重点笔记
1,函数绑定运算符,用来取代call,apply,bind调用,写法:并排的双冒号(::),左边是对象,右边是函数 foo :: bar; // 等同于bar.bind(foo); 双冒号返回的还是原 ...
- (1-1)SpringCloud-Eureka:服务的注册与发现
SpringCloud Eureka是SpringCloud Netflix服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能.下面来做一个示例 ...
- 云计算之路-阿里云上:部分服务器未及时续费造成docker swarm集群故障
非常非常抱歉,由于我们的疏忽 —— docker swarm 集群中的 2 台服务器没有及时续费,造成在夜里0点被自动关机,从而引发整个 docker swarm 集群故障,造成今天凌晨 0:30 ~ ...
- sping中防止定时任务多处运行解决方法
@Servicepublic class TimerJobService implements LzhTimerJobDao{ Logger logger = LoggerFactory.getLog ...
- junit4X系列--Rule
原文出处:http://www.blogjava.net/DLevin/archive/2012/05/12/377955.html.感谢作者的无私分享. 初次用文字的方式记录读源码的过程,不知道怎么 ...
- Linux指令--telnet
telnet命令通常用来远程登录.telnet程序是基于TELNET协议的远程登录客户端程序.Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户 ...