entertaining-mines/online/onlinecontroller.cpp

105 lines
2.7 KiB
C++
Raw Normal View History

2019-11-12 01:51:58 -05:00
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2019 Victor Tran
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* *************************************/
#include "onlinecontroller.h"
#include <QJsonObject>
#include <online/onlinewebsocket.h>
struct OnlineControllerPrivate {
OnlineController* instance = nullptr;
2019-11-12 09:56:48 -05:00
OnlineWebSocket* ws = nullptr;
2019-11-12 02:11:11 -05:00
QString discordJoinSecret;
2019-11-12 01:51:58 -05:00
};
OnlineControllerPrivate* OnlineController::d = new OnlineControllerPrivate();
OnlineController::OnlineController(QObject *parent) : QObject(parent)
{
}
OnlineController*OnlineController::instance()
{
if (!d->instance) d->instance = new OnlineController();
return d->instance;
}
2019-11-12 09:56:48 -05:00
bool OnlineController::isConnected()
{
return d->ws != nullptr;
}
2019-11-12 01:51:58 -05:00
#include <QDebug>
void OnlineController::attachToWebSocket(OnlineWebSocket*ws)
{
d->ws = ws;
connect(ws, &OnlineWebSocket::disconnected, this, [=] {
emit disconnected(ws->closeCode());
//Clear out the WebSocket
d->ws->deleteLater();
d->ws = nullptr;
});
connect(ws, &OnlineWebSocket::jsonMessageReceived, this, &OnlineController::jsonMessage);
connect(ws, &OnlineWebSocket::jsonMessageReceived, this, [=](QJsonDocument doc) {
qDebug() << doc.toJson();
});
2019-11-26 08:38:11 -05:00
connect(ws, &OnlineWebSocket::pingChanged, this, &OnlineController::pingChanged);
2019-11-12 01:51:58 -05:00
}
void OnlineController::sendJson(QJsonDocument doc)
{
d->ws->sendJson(doc);
}
void OnlineController::sendJsonO(QJsonObject object)
{
if (d->ws == nullptr) {
qWarning() << "Tried to send a message to an unopen socket";
return;
}
2019-11-12 01:51:58 -05:00
d->ws->sendJsonO(object);
}
2019-11-12 02:11:11 -05:00
void OnlineController::setDiscordJoinSecret(QString joinSecret)
{
d->discordJoinSecret = joinSecret;
}
QString OnlineController::discordJoinSecret()
{
QString discordJoinSecret = d->discordJoinSecret;
d->discordJoinSecret = "";
return discordJoinSecret;
}
2019-11-26 08:38:11 -05:00
int OnlineController::ping()
{
return d->ws->ping();
}
2019-11-12 01:51:58 -05:00
void OnlineController::close()
{
d->ws->close();
}