1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-07 11:32:42 +01:00
ScreenPlay/ScreenPlaySDK/screenplaysdk.cpp
2018-03-12 16:20:48 +01:00

68 lines
2.1 KiB
C++

#include "screenplaysdk.h"
ScreenPlaySDK::ScreenPlaySDK(QQuickItem *parent):
QQuickItem(parent)
{
// By default, QQuickItem does not draw anything. If you subclass
// QQuickItem to create a visual item, you will need to uncomment the
// following line and re-implement updatePaintNode()
// setFlag(ItemHasContents, true);
m_socket = QSharedPointer<QLocalSocket>(new QLocalSocket());
m_socket.data()->setServerName("ScreenPlay");
QObject::connect(m_socket.data(), &QLocalSocket::connected, this, &ScreenPlaySDK::connected);
QObject::connect(m_socket.data(), &QLocalSocket::disconnected, this, &ScreenPlaySDK::disconnected);
QObject::connect(m_socket.data(), &QLocalSocket::bytesWritten, this, &ScreenPlaySDK::bytesWritten);
QObject::connect(m_socket.data(), &QLocalSocket::readyRead, this, &ScreenPlaySDK::readyRead);
QObject::connect(m_socket.data(), QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &ScreenPlaySDK::error);
m_socket.data()->connectToServer();
}
ScreenPlaySDK::~ScreenPlaySDK()
{
}
void ScreenPlaySDK::connected()
{
setIsConnected(true);
emit sdkConnected();
}
void ScreenPlaySDK::disconnected()
{
setIsConnected(false);
emit sdkDisconnected();
}
void ScreenPlaySDK::bytesWritten(qint64 bytes)
{
}
void ScreenPlaySDK::readyRead()
{
QString tmp = m_socket.data()->readAll();
QJsonParseError err;
auto doc = QJsonDocument::fromJson(QByteArray::fromStdString(tmp.toStdString()),&err);
if (!(err.error == QJsonParseError::NoError)) {
emit incommingMessageError(err.errorString());
return;
}
QJsonObject ob = doc.object();
QJsonObject::iterator iterator;
for (iterator = ob.begin(); iterator != ob.end(); iterator++) {
qDebug() << iterator.key() << ob.value(iterator.key()).toString();
emit incommingMessage(iterator.key(), ob.value(iterator.key()).toString());
}
}
void ScreenPlaySDK::error(QLocalSocket::LocalSocketError socketError)
{
emit sdkSocketError("Error");
if(socketError == QLocalSocket::LocalSocketError::ConnectionRefusedError){
//QCoreApplication::quit();
}
}