pahoはMQTTClientを実装してメッセージを公開します
次に、Pahoオープンソースプロジェクトを使用して、メッセージを公開するためのmqttクライアントを実装します。この記事では、主にCのMQTTクライアントライブラリについて説明します。Pahoは、クライアントを作成するために次の同様の手順を提供します。
1、安装
//从github上下载项目
git clone https://github.com/eclipse/paho.mqtt.c.git
//进入文件夹
cd paho.mqtt.c
//编译
make
//安装
sudo make install
2、目录介绍
src:源文件、库文件目录
build:编译过后的动态库及执行文件
2.1、srcディレクトリ
srcには、公開、サブスクライブ、同期、非同期ファイルなど、多くのソースファイルがあります。
2.2、ビルドディレクトリ
ビルドファイルは、コンパイル後の実行可能ファイルであり、同期、非同期、公開、サブスクライブメッセージなどの機能も備えています。
3、实现过程
1.创建一个客户端对象;
2.设置连接MQTT服务器的选项;
3.如果多线程(异步模式)操作被使用则设置回调函数(详见 Asynchronous >vs synchronous client applications);
4.订阅客户端需要接收的任意话题;
5.重复以下操作直到结束:
a.发布客户端需要的任意信息;
b.处理所有接收到的信息;
6.断开客户端连接;
7.释放客户端使用的所有内存。
4、文件操作
paho自身のファイルを直接使用し、最初にsrcのMQTTClient_publish.cのソースコードを変更します。
4.1、情報を変更する
//mqtt服务器地址
#define ADDRESS “tcp://m2m.eclipse.org:1883”
//客户端号
#define CLIENTID “ExampleClientPub”
//主题
#define TOPIC “MQTT Examples”
//消息
#define PAYLOAD “Hello World!”
4.2、情報を追加する
サーバーにユーザー名とパスワードを追加する必要があります
char *username= "test"; //添加的用户名
char *password = "test"; //添加的密码
クライアントオプションに書き込む
conn_opts.username = username; //将用户名写入连接选项中
conn_opts.password = password; //将密码写入连接选项中
5、源码
ソースコードについて詳しく説明しました。上記のファイルで機能の多くを自分で確認する必要があります。以下は非同期ではなく同期リリースです。非同期コードもあります。上記のWebサイトにアクセスできます。
/*******************************************************************************
* Copyright (c) 2012, 2020 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* https://www.eclipse.org/legal/epl-2.0/
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial contribution
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"
#define ADDRESS "59.110.42.24:1883"
#define CLIENTID "0bd981c5-a055-4196-8b7f-efb9f7a4d6ac"
#define TOPIC "test"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int main(int argc, char* argv[])
{
//声明mqtt客户端
MQTTClient client;
//初始化客户端选项 conn_opts
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
//消息初始化 pubmsg
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
int rc;
char *username = "test";
char *password = "test";
if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to create client, return code %d\n", rc);
exit(EXIT_FAILURE);
}
//保持心跳20
conn_opts.keepAliveInterval = 20;
//清理会话
conn_opts.cleansession = 1;
//客户端的用户名和密码
conn_opts.username = username;
conn_opts.password = password;
//创建连接
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
//消息负载(内容)
pubmsg.payload = PAYLOAD;
//消息长度
pubmsg.payloadlen = (int)strlen(PAYLOAD);
//消息质量分为0:不重要的消息比如温度,可以多次上传丢失一次没事。1:可能会丢失1次 2:永远不会丢失
pubmsg.qos = QOS;
//有true和false 判断消息是否保留
pubmsg.retained = 0;
//发布消息 token是消息发布后,传递令牌将返回客户端检查令牌是否已成功传递到其目的地
if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to publish message, return code %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: %s\n",
(int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
//阻塞函数,等待消息发布
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
printf("Failed to disconnect, return code %d\n", rc);
//断开连接
MQTTClient_destroy(&client);
return rc;
}
6、实现效果
mqttテストソフトウェアによってサブスクライブされたメッセージは、サーバーで公開することによって受信できます。