#include "MqttProxy.h"
#include <IceUtil/Time.h>
#include <stdio.h>
MqttProxy::MqttProxy(const char* clientID) : mosqpp::mosquittopp(clientID, false), firstConn_(false)
{
}
MqttProxy::~MqttProxy()
{
}
void MqttProxy::init(const char *host, int port, int keepalive, const char* title)
{
brokerHost_ = host;
brokerPort_ = port;
brokerKeepalive_ = keepalive;
title_ = title;
}
bool MqttProxy::start()
{
if (firstConn_)
{
reconnect();
return true;
}
int ret = connect(brokerHost_.c_str(), brokerPort_, brokerKeepalive_);
if (ret != 0)
{
printf("MqttProxy::start-->connect(%s:%d) error:%s", mosqpp::strerror(ret));
return false;
}
firstConn_ = true;
return true;
}
void MqttProxy::stop()
{
//disconnect();
}
std::string MqttProxy::get_error(int ret)
{
return mosqpp::strerror(ret);
}
void MqttProxy::on_connect(int rc)
{
printf("MqttProxy::on_connect:%s", get_error(rc).c_str());
subscribe(NULL, title_.c_str(), 2);
}
void MqttProxy::on_disconnect(int rc)
{
LOG_ERROR("MqttProxy::on_disconnect:%s", get_error(rc).c_str());
unsubscribe(NULL, title_.c_str());
}
void MqttProxy::on_publish(int mid)
{
printf("MqttProxy::on_publish-->mid:%d", mid);
}
void MqttProxy::on_message(const struct mosquitto_message *message)
{
if (message == NULL || message->payloadlen <= 0)
{
printf("MqttProxy::on_message:len is null\n");
return;
}
LOG_DEBUG("MqttProxy::on_message-->mid[%d], topic[%s], payload[%d]", message->mid, message->topic, message->payloadlen);
}
void MqttProxy::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
LOG_DEBUG("MqttProxy::on_subscribe-->mid[%d], qos[%d], granted_qos[%d]", mid, qos_count, granted_qos);
}
void MqttProxy::on_unsubscribe(int mid)
{
printf("MqttProxy::on_unsubscribe-->mid[%d]\n", mid);
}
void MqttProxy::on_log(int level, const char *str)
{
printf("MqttProxy::on_log-->mid[%d], [%s]\n", level, str);
}
void MqttProxy::on_error()
{
}
没有合适的资源?快使用搜索试试~ 我知道了~
mosquitto简单封装
共6个文件
h:3个
cpp:3个
3星 · 超过75%的资源 需积分: 42 67 下载量 106 浏览量
2017-04-26
16:15:34
上传
评论 1
收藏 3KB RAR 举报
温馨提示
mosquitto简单封装,实现消息发布及接收
资源推荐
资源详情
资源评论
收起资源包目录
code.rar (6个子文件)
MqttThread.h 621B
MqttProxy.cpp 2KB
MqttThread.cpp 1KB
BaseThread.cpp 446B
MqttProxy.h 913B
BaseThread.h 519B
共 6 条
- 1
资源评论
- 华山沦贱2018-09-04不是windows的,依赖两个库<IceUtil/Time.h>/<IceUtil/Time.h>
gavin1203
- 粉丝: 5
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功