服务端,采用 Mosquitto 来转发分发消息。

客户端自己写。

服务端 启动 mosquitto

(底下的命令是我自己放到环境变量里面的,通过alias 运行mosquitto)
IshallbeThatIshallbe:~ iamthat$ mosquitto_start
1427629906: mosquitto version 1.3.5 (build date 2014-10-27 15:12:32+0000) starting
1427629906: Config loaded from /usr/local/Cellar/mosquitto/1.3.5/etc/mosquitto/mosquitto.conf.
IshallbeThatIshallbe:~ iamthat$ 1427629906: Opening ipv4 listen socket on port 1883.
1427629906: Opening ipv6 listen socket on port 1883.

客户端代码如下: 记住导入三个ibm 的jar 包。

package mqtthelloworld;

import com.ibm.mqtt.MqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttNotConnectedException;
import com.ibm.mqtt.MqttPersistenceException; public class Client1 { private final static String CONNECTION_STRING = "tcp://127.0.0.1:1883";
private final static boolean CLEAN_START = true;
private final static short KEEP_ALIVE = 30;//低耗网络,但是又需要及时获取数据,心跳30s
private final static String CLIENT_ID = "client1";
private final static String[] SUBSCRIBE_TOPICS = {
"gabriel"
};
private final static int[] QOS_VALUES = {01};
private static final String[] PUBLISH_TOPICS = {"gabriel"};
private static final String publish_topic="gabriel";
//////////////////
private MqttClient mqttClient; public Client1(){
try {
mqttClient = new MqttClient(CONNECTION_STRING);
SimpleCallbackHandler simpleCallbackHandler = new SimpleCallbackHandler();
mqttClient.registerSimpleHandler(simpleCallbackHandler);//注册接收消息方法
mqttClient.connect(CLIENT_ID, CLEAN_START, KEEP_ALIVE);
mqttClient.subscribe(SUBSCRIBE_TOPICS, QOS_VALUES);//订阅接主题
/**
* 完成订阅后,可以增加心跳,保持网络通畅,也可以发布自己的消息
*/
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} private void publishMessage(String message) throws MqttNotConnectedException, MqttPersistenceException, IllegalArgumentException, MqttException{
mqttClient.publish(publish_topic, message.getBytes(), QOS_VALUES[0], true);
} public static void main(String[] args) throws MqttNotConnectedException, MqttPersistenceException, IllegalArgumentException, MqttException {
Client1 client1=new Client1();
for(int i=0;i<10;i++){
client1.publishMessage("testing Client1 and this is "+i+"th message published");
}
}
}
package mqtthelloworld;

import com.ibm.mqtt.MqttSimpleCallback;

public class SimpleCallbackHandler implements MqttSimpleCallback {

	  /**
* 当客户机和broker意外断开时触发
* 可以再此处理重新订阅
*/
@Override
public void connectionLost() throws Exception {
// TODO Auto-generated method stub
System.out.println("客户机和broker已经断开");
} /**
* 客户端订阅消息后,该方法负责回调接收处理消息
*/
@Override
public void publishArrived(String topicName, byte[] payload, int Qos, boolean retained) throws Exception {
// TODO Auto-generated method stub
System.out.println("订阅主题: " + topicName);
System.out.println("消息数据: " + new String(payload));
System.out.println("消息级别(0,1,2): " + Qos);
System.out.println("是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): " + retained);
} }

再终端开一个客户端:

IshallbeThatIshallbe:bin iamthat$ mosquitto_sub -v -t 'gabriel'

运行结果:

运行Client1 结果如下:

终端显示

IshallbeThatIshallbe:bin iamthat$ mosquitto_sub -v -t 'gabriel'
1427630077: New connection from ::1 on port 1883.
1427630077: New client connected from ::1 as mosqsub/418-IshallbeTha (c1, k60).
1427630094: New connection from 127.0.0.1 on port 1883.
1427630094: New client connected from 127.0.0.1 as client1 (c1, k30).
gabriel testing Client1 and this is 0th message published
gabriel testing Client1 and this is 1th message published
gabriel testing Client1 and this is 2th message published
gabriel testing Client1 and this is 3th message published
gabriel testing Client1 and this is 4th message published
gabriel testing Client1 and this is 5th message published
gabriel testing Client1 and this is 6th message published
gabriel testing Client1 and this is 7th message published
gabriel testing Client1 and this is 8th message published
gabriel testing Client1 and this is 9th message published

控制台显示:

订阅主题: gabriel
消息数据: testing Client1 and this is 0th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 1th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 2th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel

再开一个终端,看看订阅者如何与发布者沟通:

IshallbeThatIshallbe:bin iamthat$ mosquitto_pub -t 'gabriel' -m 'connecting to publisher'
IshallbeThatIshallbe:bin iamthat$

之前的订阅者终端:

IshallbeThatIshallbe:bin iamthat$ mosquitto_sub -v -t 'gabriel'
1427630077: New connection from ::1 on port 1883.
1427630077: New client connected from ::1 as mosqsub/418-IshallbeTha (c1, k60).
1427630094: New connection from 127.0.0.1 on port 1883.
1427630094: New client connected from 127.0.0.1 as client1 (c1, k30).
gabriel testing Client1 and this is 0th message published
gabriel testing Client1 and this is 1th message published
gabriel testing Client1 and this is 2th message published
gabriel testing Client1 and this is 3th message published
gabriel testing Client1 and this is 4th message published
gabriel testing Client1 and this is 5th message published
gabriel testing Client1 and this is 6th message published
gabriel testing Client1 and this is 7th message published
gabriel testing Client1 and this is 8th message published
gabriel testing Client1 and this is 9th message published
1427631029: New connection from ::1 on port 1883.
1427631029: New client connected from ::1 as mosqpub/468-IshallbeTha (c1, k60).
gabriel connecting to publisher

控制台显示:

订阅主题: gabriel
消息数据: testing Client1 and this is 0th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 1th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 2th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 3th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 4th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 5th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 6th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 7th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 8th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: testing Client1 and this is 9th message published
消息级别(0,1,2): 1
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false
订阅主题: gabriel
消息数据: connecting to publisher
消息级别(0,1,2): 0
是否是实时发送的消息(false=实时,true=服务器上保留的最后消息): false

可以看出订阅者成功与发布者联系交互。

个人对MQTT的理解是:

就三种角色: 订阅者,服务代理,发布者。

客户端订阅者订阅主题---》  服务代理broker-----> 反馈发布者消息给订阅者(消息的传递通过主题)

发布者创建主题发布消息---》服务端broker ---》反馈给订阅者(消息传递通过主题)

订阅者发送消息---》服务端broker---> 发布者(消息传递通过主题)

MQTT客户端与服务代理的案列的更多相关文章

  1. python3 mqtt 客户端以及服务端

    pip3 install paho-mqtt client #!/usr/bin/env python #coding=utf- import json import sys import os im ...

  2. 关于IIS寄宿WCF服务,客户端不能生成代理类

    我在使用VS2010写好WCF的Web服务后,部署在IIS7.0上,可以在IE9上进行访问,并且能显示XML数据,如下图 然后我在项目的客户端进行服务的添加引用,如下图 VS2010自动生成代理类,但 ...

  3. 客户端使用自定义代理类访问WCF服务 z

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或 web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否 ...

  4. 客户端使用自定义代理类访问WCF服务

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否简 ...

  5. SpringCloud断路器(Hystrix)和服务降级案列

    断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...

  6. 大数据技术之_14_Oozie学习_Oozie 的简介+Oozie 的功能模块介绍+Oozie 的部署+Oozie 的使用案列

    第1章 Oozie 的简介第2章 Oozie 的功能模块介绍2.1 模块2.2 常用节点第3章 Oozie 的部署3.1 部署 Hadoop(CDH版本的)3.1.1 解压缩 CDH 版本的 hado ...

  7. WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...

  8. SignalR 实现web浏览器客户端与服务端的推送功能

    SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换:它将继 ...

  9. 客户端获取服务端自定义类数据 z

    客户端获取服务端自定义类数据 问题一:超时问题,在最后获取数据的时候突然提示服务超时,服务已断开 解决:配置文件添加: <bindings> <wsHttpBinding> & ...

随机推荐

  1. Lucene 4.0

    关于4.0的Update Index  ,Create Index /* * Create Index */ public static void createIndex() throws IOExc ...

  2. hbase meta表的结构

    下面看下hbase:meta 表的结构,hbase:meta表中,保存了每个表的region地址,还有一些其他信息,例如region的名字,HRegionInfo,服务器的信息.hbase:meta表 ...

  3. RxJava 平常使用

    本文转载自: http://blog.csdn.net/theone10211024/article/details/50435325 一.Scheduler线程切换 这种场景经常会在“后台线程取数据 ...

  4. keditor_php图片上传

    <script type="text/javascript" src="/statics/js/kindeditor/kindeditor-min.js" ...

  5. Crusher Django 学习笔记4 使用Model

    http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial4-using-basic.html 顺便学习一下FQ Crush ...

  6. AJAX局部更新演出排期

    <script language="javascript" type="text/javascript"> function createXMLHt ...

  7. C#中linq

    class IntroToLINQ { static void Main() { // The Three Parts of a LINQ Query: // 1. Data source. ] { ...

  8. 【转】Web标准中的常见问题

    本文转自http://www.tracefact.net/Misc/Common-Problems-Of-Web-Standard.aspx 引言 大概在2004年的时候,Web标准的概念藉由一本名为 ...

  9. 1103. Integer Factorization (30)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  10. SQLserver利用系统时间生成“2015-11-30 00:00:00.000”类型的时间

    select getdate() ---当前时间:2015-12-18 10:20:24.097 -------------------建立测试表 Create Table #Test ( ID IN ...