怎么分析emqttd客户端

mqtt 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放,简单,轻量级,且易于实现,这些优点使得他受用于任何环境
该协议的特点有:
&使用发布/订阅消息的模式,提供一对多的消息发布,解除应用程序耦合
对负载内容屏蔽的消息传输
使用TCP/IO 提供的网络连接
有三种消息发布服务质量:
& &至多一次&,消息发布完全依赖底层TCP/IP 网络,会发生消息丢失或者重复,这一级别可用于如下情况,环境,传感器数据,丢失一次度记录无所谓,因为不久之后会有第二次发送
& &至少一次& 确保消息到达,但消息重复可能发生
& “只有一次&,确保消息到达一次,这一级别可用于如下情况,在计费系统中,消息重复或者丢失导致不正确的结果
小型传输,开销很小(固定床都的头部是2个字节),协议变换最小化,以降低网络流量
使用Last will和Testament 特性通知有关客户端异常中断的机制
1:配置环境
2:服务器端程序
package com.
import org.eclipse.paho.client.mqttv3.MqttC
import org.eclipse.paho.client.mqttv3.MqttConnectO
import org.eclipse.paho.client.mqttv3.MqttDeliveryT
import org.eclipse.paho.client.mqttv3.MqttE
import org.eclipse.paho.client.mqttv3.MqttM
import org.eclipse.paho.client.mqttv3.MqttPersistenceE
import org.eclipse.paho.client.mqttv3.MqttT
import org.eclipse.paho.client.mqttv3.persist.MemoryP
* Description
服务器向多个客户端推送主题,即不同客户端可向服务器订阅相同主题
* Company Beijing
* date createTime:16/7/13
public class Server {
public static final String HOST = &tcp://101.200.133.189:1883&;
public static final String TOPIC = &toclient/124&;
public static final String TOPIC125 = &toclient/125&;
private static final String clientid = &server&;
private MqttC
private MqttT
private MqttTopic topic125;
private String userName = &admin&;
private String passWord = &password&;
private MqttM
public Server() throws MqttException {
// MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
connect();
private void connect() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName(userName);
options.setPassword(passWord.toCharArray());
// 设置超时时间
options.setConnectionTimeout(10);
// 设置会话心跳时间
options.setKeepAliveInterval(20);
client.setCallback(new PushCallback());
client.connect(options);
topic = client.getTopic(TOPIC);
topic125 = client.getTopic(TOPIC125);
} catch (Exception e) {
e.printStackTrace();
public void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException,
MqttException {
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
System.out.println(&message is published completely! &
+ token.isComplete());
public static void main(String[] args) throws MqttException {
Server server = new Server();
server.message = new MqttMessage();
server.message.setQos(2);
server.message.setRetained(true);
server.message.setPayload(&给客户端124推送的信息:wuyouxuan&.getBytes());
server.publish(server.topic, server.message);
server.message = new MqttMessage();
server.message.setQos(2);
server.message.setRetained(true);
server.message.setPayload(&给客户端125推送的信息:wuyouxuan&.getBytes());
server.publish(server.topic125, server.message);
System.out.println(server.message.isRetained() + &------ratained状态&);
package com.
import org.eclipse.paho.client.mqttv3.IMqttDeliveryT
import org.eclipse.paho.client.mqttv3.MqttC
import org.eclipse.paho.client.mqttv3.MqttM
* 发布消息的回调类
* 必须实现MqttCallback的接口并实现对应的相关接口方法CallBack 类将实现 MqttCallBack。
* 每个客户机标识都需要一个回调实例。在此示例中,构造函数传递客户机标识以另存为实例数据。
* 在回调中,将它用来标识已经启动了该回调的哪个实例。
* 必须在回调类中实现三个方法:
public void messageArrived(MqttTopic topic, MqttMessage message)接收已经预订的发布。
public void connectionLost(Throwable cause)在断开连接时调用。
public void deliveryComplete(MqttDeliveryToken token))
接收到已经发布的 QoS 1 或 QoS 2 消息的传递令牌时调用。
由 MqttClient.connect 激活此回调。
public class PushCallback implements MqttCallback {
public void connectionLost(Throwable cause) {
// 连接丢失后,一般在这里面进行重连
System.out.println(&连接断开,可以做重连&);
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println(&deliveryComplete---------& + token.isComplete());
public void messageArrived(String topic, MqttMessage message) throws Exception {
// subscribe后得到的消息会执行到这里面
System.out.println(&接收消息主题 : & + topic);
System.out.println(&接收消息Qos : & + message.getQos());
System.out.println(&接收消息内容 : & + new String(message.getPayload()));
3:客户端程序
package com.
import org.eclipse.paho.client.mqttv3.MqttC
import org.eclipse.paho.client.mqttv3.MqttConnectO
import org.eclipse.paho.client.mqttv3.MqttE
import org.eclipse.paho.client.mqttv3.MqttT
import org.eclipse.paho.client.mqttv3.persist.MemoryP
import java.util.concurrent.ScheduledExecutorS
* Description
客户端程序
* Company Beijing
* date createTime:16/7/13
public class Client {
public static final String HOST = &tcp://101.200.133.189:1883&;
public static final String TOPIC = &toclient/124&;
private static final String clientid = &client124&;
private MqttC
private MqttConnectO
private String userName = &admin&;
private String passWord = &password&;
private ScheduledExecutorS
private void start() {
// host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
// MQTT的连接设置
options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(true);
// 设置连接的用户名
options.setUserName(userName);
// 设置连接的密码
options.setPassword(passWord.toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
// 设置回调
client.setCallback(new PushCallback());
MqttTopic topic = client.getTopic(TOPIC);
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
options.setWill(topic, &close&.getBytes(), 2, true);
client.connect(options);
//订阅消息
String[] topic1 = {TOPIC};
client.subscribe(topic1, Qos);
} catch (Exception e) {
e.printStackTrace();
public static void main(String[] args) throws MqttException {
Client client = new Client();
client.start();
参考网站:
/a/9450#articleHeader0
github 开源项目 &&/fusesource/mqtt-client
本文已收录于以下专栏:
相关文章推荐
最近因为工作需要,需要使用C# 语言编写一个通过MQTT协议 ,上传数据到云端的工具。因为之前没有用过MQTT,所以 使用的时候遇到很多问题.下面将会把我遇到的问题一一解释。1.引用源码库地址
许久不更新了,最近这段时间发生挺多事,说破了也就应届生那点破事,什么技术方向,等待offer,签约毁约的。最终还是老老实实回到公司继续实习,为了以后再拼搏一次吧。
最近公司做的项目中有用到消息推送,...
MQTT基础——Part 2. 发布/订阅模式作者:chszs,未经博主允许不得转载。经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs本文是《MQTT基础》系列博...
互联网推送消息的方式很常见,特别是移动互联网上,手机每天都能收到好多推送消息,经过研究发现,这些推送服务的原理都是维护一个长连接(要不不可能达到实时效果),但普通的socket连接对服务器的消耗太大了...
这段时间学习了推送技术,对xmpp和mqtt 协议做了下比较。
xmpp基于xml信息传递,所以传输信息量比较大,在保持长链接情况下功耗会比较大。
可能还是比较适合用来做聊天之类的通讯应用,而对于...
Internet Protocol 网络层协议,网络之间互联的协议。FTP
File Transfer Protocol 文本传输协议TCP
Transmission Control Pr...
互联网推送消息的方式很常见,特别是移动互联网上,手机每天都能收到好多推送消息,经过研究发现,这些推送服务的原理都是维护一个长连接(要不不可能达到实时效果),但普通的socket连接对服务器的消耗太大了...
MQTT实现消息推送
MQTT实现消息接收(接收消息需实现MqttSimpleCallback接口并实现它的publishArrived方法)必须注册接收消息方法
mqttClient....
上一篇文章已经介绍了mqtt+activemq实现消息推送移动端的实现,也介绍了利用自带的web console进行消息发布的方法。但是在具体的项目应用中,当我们将需要将该消息推送模块嵌入到一个后台管...
他的最新文章
讲师:姜飞俊
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)mqtt各种broker如何选择? - 知乎73被浏览13898分享邀请回答102 条评论分享收藏感谢收起36 条评论分享收藏感谢收起查看更多回答2 个回答被折叠()The Massively Scalable MQTT Broker for IoT and Mobile Applications
EMQ (Erlang MQTT Broker) is a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OTP.
EMQ is fully open source and licensed under the Apache Version 2.0. EMQ implements both MQTT V3.1 and V3.1.1 protocol specifications, and supports MQTT-SN, CoAP, WebSocket, STOMP and SockJS at the same time.
Benefits and Features
Fully Open Source
Licensed under the Apache Version 2.0
Massively Scalable
C1000K MQTT Connections Support
MQTT V3.1.1 Support
Full MQTT V3.1.1 Protocol Support
Easy To Install
Cross-platform deployment on Linux, FreeBSD, Mac, Windows and even Raspberry Pi
Cluster or Bridge
Cluster or Bridge brokers on Distributed Nodes
Modules and Plugins
LDAP, MySQL, PostgreSQL, Redis, MongoDB Plugins
Get Started
provides a scalable, enterprise grade, extensible open-source MQTT broker for IoT, M2M, Smart Hardware, Mobile Messaging and HTML5 Web Messaging Applications.
Sensors, Mobiles, Browsers and Application Servers can be connected by EMQ brokers with asynchronous MQTT messages.
Embrace Erlang/OTP! The Concurrent, Soft-Realtime, Low-Latency and Fault-Tolerant Platform.
Route MQTT Messages among clustered nodes by Topic Trie Match and Routing Table Lookup.
The Massively Scalable MQTT Broker powering your IoT, M2M, Smart Hardware, and Mobile Messaging Applications
Mobile Messaging
Build large-scale mobile messenger with Erlang and MQTT Technologies, like
Smart Hardware, Smart Appliances
Smart Hardware, Smart Appliances can be connected to the cloud service and the Mobile Applications with asynchronous bidirectional MQTT messages.
Connected Car, Internet of Vehicles
Connect Cars to cloud service with MQTT Protocol. Collect and Analyze the big sensor data producted by car sensors.
builds the data collection and analysis platform for Electric Cars based on EMQ Broker.
IoT, M2M and BigData
The IoT, M2M Sensors and Actuators can be connected by the clustered EMQ brokers which gather the sensor data and stream to various backends at the same time. connects any kind of Sensors to EMQ Broker for scalability and data level interoperability
Smart City, Smart Healthcare
The Sensors and Gateways can publish various sensor metrics to the EMQ broker cluster on cloud.
Electricity, Oil and Energy Industry
Sensors of the Electricity, Oil and Energy Industry communiate to the EMQ brokers in cloud via satellite link.
Android Push
Push notifications with an extremely lightweight, low power usage and low bandwidth required MQTT Connection.
implements the C500K+ push service based on EMQ Broker.
SCADA and Industry 4.0
Build the New Generation SCADA System based on MQTT and IoT Technologies. Introduce MQTT Protocol and Erlang/OTP Platform to Industry 4.0.
hosts a public emqttd cluster on :
team has more than ten years' experience in building large IoT and Instant Messaging applications with Erlang and MQTT technologies. We provide commercial support and services for and around the open source EMQ
project. Get in touch with Us:
GitHub Issues
feng.emqtt.io
Contributors< - 站长之家
404 Error: 抱歉, 您所查找的页面不存在, 可能已被删除或您输错了网址!
没有发现你要找的页面, 经砖家仔细研究结果如下:
贵玉手输入地址时可能存在键入错误
小蜗牛把页面落家里忘记带了
不排除阁下人品问题
电信网通那头接口生锈了

我要回帖

更多关于 emqttd客户端 的文章

 

随机推荐