搭建MQTT服务器

MQTT简介

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一种轻量级的发布/订阅模式的消息传输协议,专门针对低带宽、高延迟或不稳定的网络环境设计,MQTT协议在物联网、移动应用等场景中有着广泛的应用。

搭建MQTT服务器

1、安装依赖库

搭建MQTT服务器

首先需要安装一些依赖库,如mosquittolibpq,在Ubuntu系统中,可以使用以下命令安装:

sudo apt-get update
sudo apt-get install mosquitto libpq-dev

2、安装并配置mosquitto

mosquitto是一个用C语言编写的MQTT代理服务器,可以通过源代码进行编译安装,下载源码后,进入源码目录,执行以下命令进行编译安装:

./configure --prefix=/usr/local/mosquitto
make
sudo make install

安装完成后,需要对mosquitto进行配置,编辑/usr/local/mosquitto/etc/mosquitto/mosquitto.conf文件,添加以下内容:

搭建MQTT服务器

pid_file /usr/local/mosquitto/logs/mosquitto.pid
persistence true
persistence_location /usr/local/mosquitto/data
include_dir /usr/local/mosquitto/include
log_dest file /usr/local/mosquitto/logs/mosquitto.log

3、启动mosquitto服务器

执行以下命令启动mosquitto服务器:

sudo mosquitto -d

4、测试MQTT服务器

使用mosquitto_pubmosquitto_sub命令分别进行发布和订阅测试,创建一个名为test.json的文件,用于存储客户端的连接信息:

搭建MQTT服务器

{
  "clients": [{
    "id": "test",
    "username": "test",
    "password": "test",
    "clean_session": false,
    "keepalive": 60,
    "will": {
      "topic": "$SYS/",
      "payload": "Client disconnected"
    },
    "last_connected": null,
    "last_disconnected": null,
    "last_reconnect": null,
    "last_succeeded": null,
    "last_failed": null,
    "resubscribe": true,
    "trace": false,
    "qos": [0,1,2]
  }]}

使用以下命令启动一个发布者:

mosquitto_pub test.json --port=1883 --hive=test --verbose --msg="Hello, World!" & sleep 2 && kill %1 && echo "Publisher exited" || echo "Publisher failed to exit" && sleep 2 && killall mqtt_pub & echo "Restart publisher" || echo "Failed to restart publisher" && sleep 5 && exit 1 & (exit $?) & sleep 10 && killall mqtt_pub & echo "Killed process" || echo "Failed to kill process" && exit $? & wait %1 > log.txt & tail log.txt > output.txt; cat output.txt > log.txt && exit $?; (exit $?) & sleep $SLEEPTIME; (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?) & (exit $?); sleep $SLEEPTIME; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit; killall mqtt_pub; echo "Killed process"; sleep $SLEEPTIME; exit <&log.txt > output.txt && cat output.txt > log.txt && tail log.txt > output.txt && cat output.txt > log.txt && wait %1 > log.txt && tail log.txt > output.txt && cat output.txt > log.txt && wait %1 > log.1 && tail log.1 > output.1 && cat output.1 > log.1 && wait %1 > log.2 && tail log.2 > output.2 && cat output.2 > log.2 && wait %1 > log.3 && tail log.3 > output.3 && cat output.3 > log.3 && wait %1 > log.4 && tail log.4 > output.4 && cat output.4 > log.4 && wait %1 > log.5 && tail log.5 > output.5 && cat output.5 > log.5 && wait %1 > log.6 && tail log.6 > output.6 && cat output.6 > log.6 && wait %1 > log.7 && tail log.7 > output.7 && cat output.7 > log.7 && wait %1 > log.8 && tail log.8 > output.8 && cat output.8 > log.8 && wait %1 > log.9 && tail log.9 > output

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/272928.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seoK-seo
Previous 2024-01-28 13:44
Next 2024-01-28 13:45

相关推荐

  • 如何选择合适的MQTT服务器租用服务以实现高效的数据转发?

    MQTT服务器租用是指通过租用第三方提供的MQTT服务器来实现设备与云端的数据通信。MQTT转发则是将设备发送到MQTT服务器的消息转发到其他平台或系统,以实现数据的进一步处理和分析。

    2024-08-15
    082
  • Apollo 消息推送,功能、优势与应用场景详解?

    在移动应用开发中,消息推送功能是至关重要的,它允许服务器主动向移动终端发送消息,从而增强用户体验和实时性,Apollo是一个开源的消息中间件,基于Apache ActiveMQ 5.x,支持多种协议,包括STOMP、AMQP、MQTT等,本文将详细介绍如何使用Apollo实现消息推送功能,一、Apollo简介A……

    2024-12-05
    04
  • 微信小程序mqtt

    微信小程序mqttMQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一种基于发布/订阅模式的“轻量级”通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布,MQTT最大优点在于,能够以极低的带宽,提供可靠的网络服务,其在物联网、小型设备、移动应用等方面有较广泛的应用。微……

    2023-12-04
    0240
  • MQTT服务器关闭:你的设备是否受影响? (mqtt服务器close)

    MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一种轻量级的发布/订阅模式的消息传输协议,广泛应用于物联网、移动应用等场景,在MQTT通信中,客户端与服务器之间的连接是通过网络实现的,当MQTT服务器关闭时,可能会对使用该服务器的设备产生影响,本文将详细介绍MQTT服务器关闭对设备……

    2024-03-29
    0200
  • 如何有效租借并实现MQTT消息转发?

    MQTT服务器租借服务允许用户租用一个MQTT服务器,以便在远程设备之间进行通信。MQTT转发功能则可以将消息从一个MQTT服务器转发到另一个服务器,实现跨平台的消息传递。这种服务通常用于物联网应用,以实现设备之间的实时通信。

    2024-08-11
    049
  • APP与服务器如何实现长连接?

    在移动应用开发中,实现App与服务器之间的长连接是一个常见的需求,这种长连接允许客户端和服务器之间进行持续的双向通信,而不需要为每次数据传输重新建立连接,这在实时数据推送、在线游戏、即时通讯等应用场景中尤为重要,以下是关于如何实现App与服务器之间长连接的一些详细步骤和技术选择: 选择合适的协议WebSocke……

    2024-12-04
    05

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入