MQTT message push实现研究

本文详细介绍了MQTT协议的原理和特性,通过开源软件Mosquitto进行消息推送的分析。讲解了如何在Linux系统上安装和配置Mosquitto服务器,以及如何在Android客户端进行测试。同时,提到了Android客户端在测试过程中可能出现的错误及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文利用开源软件对MQTT协议进行了消息推送进行分析。其中引用了网上的部分内容;

其中的一些图片请参见下面链接中的完整文件:

http://download.youkuaiyun.com/detail/junglefly/9168135


IBM® WebSphere® MQ Telemetry Transport(简称 MQTT)是一种基于 TCP/IP 的轻量级发布 预订协议,用于连接大量的远程传感器和控制设备。它可以工作在低带宽,不可靠的通信并且占用较少内存的设备上。

MQTT 产品作为 WebSphere MQ 产品的扩展,使用了 MQTT V3.1 版本的协议。它提供了一些小型客户机库,可以将这些客户机库嵌入到运行于不同设备平台上的智能设备中。使用客户机构建的应用程序使用 MQ Telemetry Transport(MQTT) 和 WebSphere MQ Telemetry 服务并借助 WebSphere MQ 来可靠地发布和预订消息。一个高级 MQTT 客户机(即设备的 WebSphere MQ Telemetry 守护程序)可以运行于多种平台上。它可以充当一个网络集中器,能够将更多的 MQTT 客户机连接至单个队列管理器。对于在网络发生短暂中断期间无法缓存消息的小型设备,它还可以为这些小型设备提供存储转发功能。

 

特点:

l 协议简单,MQTT_V3.1_Protocol_Specific总共只有42页。相比来说XMPP看起来就要复杂很多;

l 基于 TCP/IP 的轻量级发布 预订协议,用于连接大量的远程传感器和控制设备。

l 固定头长度仅为2字节,因此传输快速,省流量;可以工作在低带宽,不可靠的通信并且占用较少内存的设备上;

l 网上有大量的源码客户端以及开源lib,方便各种平台的客户端开发。其中有C语言编写的,适合嵌入式系统;同时也有android上的实现。

l Apache ActiveMQApache Apollo这分布式开源服务器均支持MQTT协议,使用JAVA语言实现,支持多平台。Server端搭建起来相对来说比较简单,server支持集群,消息缓存等机制。

 


1. 推送原理分析



实际上,其他推送系统(包括GCMXMPP方案)的原理都与此类似。

 

 

2. Mosquitto 、android客户端、PHP客户端交互测试

消息中转服务端准备

开源项目Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案。

 

获取&安装


Mosquitto提供了WindowsLinux以及qnx系统的版本,安装文件可从http://mosquitto.org/files/binary/地址中获取(建议使用最新的1.1.x版本)。Windows系统下的安装过程非常简单,我们甚至可以把Mosquitto直接安装成为系统服务;但是,在实际应用中,我们更倾向于使用Linux系统的服务器,接下来我们就将重点介绍LinuxMosquitto的安装方法。

Linux系统上安装Mosquitto,本人建议大家使用源码安装模式,最新的源码可从http://mosquitto.org/files/source/地址中获取。解压之后,我们可以在源码目录里面找到主要的配置文件config.mk,其中包含了所有Mosquitto的安装选项,详细的参数说明如下:

 

[root@109 mosquitto-1.3.2]# cat config.mk 

# =============================================================================

# User configuration section.

#

# Largely, these are options that are designed to make mosquitto run more

# easily in restrictive environments by removing features.

#

# Modify the variable below to enable/disable features.

#

# Can also be overriden at the command line, e.g.:

#

# make WITH_TLS=no

# =============================================================================

 

# Uncomment to compile the broker with tcpd/libwrap support.

#WITH_WRAP:=yes

 

# Comment out to disable SSL/TLS support in the broker and client.

# Disabling this will also mean that passwords must be stored in plain text. It

# is strongly recommended that you only disable WITH_TLS if you are not using

# password authentication at all.

#WITH_TLS:=yes

 

# Comment out to disable TLS/PSK support in the broker and client. Requires

# WITH_TLS=yes.

# This must be disabled if using openssl < 1.0.

#WITH_TLS_PSK:=yes

 

# Comment out to disable client client threading support.

#WITH_THREADING:=yes

 

# Uncomment to compile the broker with strict protocol support. This means that

# both the client library and the broker will be very strict about protocol

# compliance on incoming data. Neither of them will return an error on

# incorrect "remaining length" values if this is commented out. The old

# behaviour (prior to 0.12) is equivalent to compiling with

# WITH_STRICT_PROTOCOL defined and means that clients will be immediately

# disconnected from the broker on non-compliance.

#WITH_STRICT_PROTOCOL:=yes

 

# Comment out to remove bridge support from the broker. This allow the broker

# to connect to other brokers and subscribe/publish to topics. You probably

# want to leave this included unless you want to save a very small amount of

# memory size and CPU time.

#WITH_BRIDGE:=yes

 

# Comment out to remove persistent database support from the broker. This

# allows the broker to store retained messages and durable subscriptions to a

# file periodically and on shutdown. This is usually desirable (and is

# suggested by the MQTT spec), but it can be disabled if required.

#WITH_PERSISTENCE:=yes

 

# Comment out to remove memory tracking support from the broker. If disabled,

# mosquitto won't track heap memory usage nor export '$SYS/broker/heap/current

# size', but will use slightly less memory and CPU time.

#WITH_MEMORY_TRACKING:=yes

 

# Compile with database upgrading support? If disabled, mosquitto won't

# automatically upgrade old database versions.

# Not currently supported.

#WITH_DB_UPGRADE:=yes

 

# Comment out to remove publishing of the $SYS topic hierarchy containing

# information about the broker state.

WITH_SYS_TREE:=yes

 

# Build with Python module. Comment out if Python is not installed, or required

# Python modules are not available.

WITH_PYTHON:=yes

 

# Build with SRV lookup support.

#WITH_SRV:=yes

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值