myxlib/examples/redis/01_client/client.hpp

68 lines
1.8 KiB
C++
Raw Normal View History

2020-04-23 20:36:49 +00:00
#include <QObject>
#include <QDebug>
#include <myx/redis/client.hpp>
namespace MR = myx::redis;
class RedisClient : public QObject
{
Q_OBJECT
MR::Client m_client;
MR::Request* m_request;
public:
RedisClient()
{
connect(&m_client, &MR::Client::connected, this, &RedisClient::slotConnected);
m_client.connectToHost("127.0.0.1");
}
virtual ~RedisClient() {}
Q_SLOT void slotConnected()
{
m_request = m_client.sendCommand("PING");
connect(m_request, &MR::Request::reply, this, &RedisClient::slotPong);
}
Q_SLOT void slotPong( const MR::Reply& reply)
{
qDebug() << static_cast<int>(reply.type());
qDebug() << const_cast<MR::Reply&>(reply).value().toString();
m_request->disconnect();
m_request->deleteLater();
}
Q_SLOT void slotSetValue( const MR::Reply& reply)
{
qDebug() << static_cast<int>(reply.type());
qDebug() << const_cast<MR::Reply&>(reply).value().toString();
m_request->disconnect();
m_request->deleteLater();
m_request = m_client.sendCommand( "GET value" );
connect(m_request, &MR::Request::reply, this, &RedisClient::slotGetValue);
}
Q_SLOT void slotGetValue( const MR::Reply& reply)
{
qDebug() << static_cast<int>(reply.type());
qDebug() << const_cast<MR::Reply&>(reply).value().toByteArray();
m_request->disconnect();
m_request->deleteLater();
m_request = m_client.sendCommand( "SUBSCRIBE test" );
connect(m_request, &MR::Request::reply, this, &RedisClient::slotSubscribeTest);
}
Q_SLOT void slotSubscribeTest( const MR::Reply& reply)
{
qDebug() << static_cast<int>(reply.type());
qDebug() << const_cast<MR::Reply&>(reply).value().toByteArray();
}
};