Add websocket support
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
#include "turnstile.h"
|
#include "turnstile.h"
|
||||||
#include "senttxstore.h"
|
#include "senttxstore.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
#include "websockets.h"
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
@@ -98,7 +99,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::createWebsocket() {
|
void MainWindow::createWebsocket() {
|
||||||
|
new WSServer(8237, true, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::restoreSavedStates() {
|
void MainWindow::restoreSavedStates() {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
void updateLabelsAutoComplete();
|
void updateLabelsAutoComplete();
|
||||||
|
|
||||||
void setDefaultPayFrom();
|
void setDefaultPayFrom();
|
||||||
|
RPC* getRPC() { return rpc; }
|
||||||
|
|
||||||
Ui::MainWindow* ui;
|
Ui::MainWindow* ui;
|
||||||
|
|
||||||
|
|||||||
10
src/rpc.cpp
10
src/rpc.cpp
@@ -712,17 +712,17 @@ void RPC::refreshBalances() {
|
|||||||
|
|
||||||
// 1. Get the Balances
|
// 1. Get the Balances
|
||||||
getBalance([=] (json reply) {
|
getBalance([=] (json reply) {
|
||||||
auto balT = QString::fromStdString(reply["transparent"]).toDouble();
|
balT = QString::fromStdString(reply["transparent"]).toDouble();
|
||||||
auto balZ = QString::fromStdString(reply["private"]).toDouble();
|
balZ = QString::fromStdString(reply["private"]).toDouble();
|
||||||
auto tot = QString::fromStdString(reply["total"]).toDouble();
|
balTotal = QString::fromStdString(reply["total"]).toDouble();
|
||||||
|
|
||||||
ui->balSheilded ->setText(Settings::getZECDisplayFormat(balZ));
|
ui->balSheilded ->setText(Settings::getZECDisplayFormat(balZ));
|
||||||
ui->balTransparent->setText(Settings::getZECDisplayFormat(balT));
|
ui->balTransparent->setText(Settings::getZECDisplayFormat(balT));
|
||||||
ui->balTotal ->setText(Settings::getZECDisplayFormat(tot));
|
ui->balTotal ->setText(Settings::getZECDisplayFormat(balTotal));
|
||||||
|
|
||||||
ui->balSheilded ->setToolTip(Settings::getUSDFormat(balZ));
|
ui->balSheilded ->setToolTip(Settings::getUSDFormat(balZ));
|
||||||
ui->balTransparent->setToolTip(Settings::getUSDFormat(balT));
|
ui->balTransparent->setToolTip(Settings::getUSDFormat(balT));
|
||||||
ui->balTotal ->setToolTip(Settings::getUSDFormat(tot));
|
ui->balTotal ->setToolTip(Settings::getUSDFormat(balTotal));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. Get the UTXOs
|
// 2. Get the UTXOs
|
||||||
|
|||||||
@@ -71,6 +71,10 @@ public:
|
|||||||
Turnstile* getTurnstile() { return turnstile; }
|
Turnstile* getTurnstile() { return turnstile; }
|
||||||
Connection* getConnection() { return conn; }
|
Connection* getConnection() { return conn; }
|
||||||
|
|
||||||
|
double balT;
|
||||||
|
double balZ;
|
||||||
|
double balTotal;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void refreshBalances();
|
void refreshBalances();
|
||||||
|
|
||||||
|
|||||||
75
src/websockets.cpp
Normal file
75
src/websockets.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
|
||||||
|
#include "websockets.h"
|
||||||
|
#include "rpc.h"
|
||||||
|
#include "settings.h"
|
||||||
|
|
||||||
|
WSServer::WSServer(quint16 port, bool debug, QObject *parent) :
|
||||||
|
QObject(parent),
|
||||||
|
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Direct Connection Server"),
|
||||||
|
QWebSocketServer::NonSecureMode, this)),
|
||||||
|
m_debug(debug)
|
||||||
|
{
|
||||||
|
m_mainWindow = (MainWindow *) parent;
|
||||||
|
if (m_pWebSocketServer->listen(QHostAddress::LocalHost, port)) {
|
||||||
|
if (m_debug)
|
||||||
|
qDebug() << "Echoserver listening on port" << port;
|
||||||
|
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
|
||||||
|
this, &WSServer::onNewConnection);
|
||||||
|
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &WSServer::closed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WSServer::~WSServer()
|
||||||
|
{
|
||||||
|
qDebug() << "Closing websocket";
|
||||||
|
m_pWebSocketServer->close();
|
||||||
|
qDeleteAll(m_clients.begin(), m_clients.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSServer::onNewConnection()
|
||||||
|
{
|
||||||
|
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
|
||||||
|
|
||||||
|
connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::processTextMessage);
|
||||||
|
connect(pSocket, &QWebSocket::binaryMessageReceived, this, &WSServer::processBinaryMessage);
|
||||||
|
connect(pSocket, &QWebSocket::disconnected, this, &WSServer::socketDisconnected);
|
||||||
|
|
||||||
|
m_clients << pSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSServer::processTextMessage(QString message)
|
||||||
|
{
|
||||||
|
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
|
||||||
|
if (m_debug)
|
||||||
|
qDebug() << "Message received:" << message;
|
||||||
|
if (pClient) {
|
||||||
|
QJsonDocument json(QJsonObject {
|
||||||
|
{"saplingAddress", m_mainWindow->getRPC()->getDefaultSaplingAddress()},
|
||||||
|
{"balance", m_mainWindow->getRPC()->balTotal},
|
||||||
|
{"zecprice", Settings::getInstance()->getZECPrice()}
|
||||||
|
});
|
||||||
|
|
||||||
|
pClient->sendTextMessage(json.toJson());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSServer::processBinaryMessage(QByteArray message)
|
||||||
|
{
|
||||||
|
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
|
||||||
|
if (m_debug)
|
||||||
|
qDebug() << "Binary Message received:" << message;
|
||||||
|
if (pClient) {
|
||||||
|
pClient->sendBinaryMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WSServer::socketDisconnected()
|
||||||
|
{
|
||||||
|
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
|
||||||
|
if (m_debug)
|
||||||
|
qDebug() << "socketDisconnected:" << pClient;
|
||||||
|
if (pClient) {
|
||||||
|
m_clients.removeAll(pClient);
|
||||||
|
pClient->deleteLater();
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/websockets.h
Normal file
34
src/websockets.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef WEBSOCKETS_H
|
||||||
|
#define WEBSOCKETS_H
|
||||||
|
|
||||||
|
#include "mainwindow.h"
|
||||||
|
#include "precompiled.h"
|
||||||
|
|
||||||
|
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
|
||||||
|
QT_FORWARD_DECLARE_CLASS(QWebSocket)
|
||||||
|
|
||||||
|
class WSServer : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit WSServer(quint16 port, bool debug = false, QObject *parent = nullptr);
|
||||||
|
~WSServer();
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
|
void closed();
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void onNewConnection();
|
||||||
|
void processTextMessage(QString message);
|
||||||
|
void processBinaryMessage(QByteArray message);
|
||||||
|
void socketDisconnected();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QWebSocketServer *m_pWebSocketServer;
|
||||||
|
MainWindow *m_mainWindow;
|
||||||
|
QList<QWebSocket *> m_clients;
|
||||||
|
bool m_debug;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WEBSOCKETS_H
|
||||||
@@ -52,7 +52,8 @@ SOURCES += \
|
|||||||
src/fillediconlabel.cpp \
|
src/fillediconlabel.cpp \
|
||||||
src/addressbook.cpp \
|
src/addressbook.cpp \
|
||||||
src/logger.cpp \
|
src/logger.cpp \
|
||||||
src/addresscombo.cpp
|
src/addresscombo.cpp \
|
||||||
|
src/websockets.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
src/mainwindow.h \
|
src/mainwindow.h \
|
||||||
@@ -72,7 +73,8 @@ HEADERS += \
|
|||||||
src/fillediconlabel.h \
|
src/fillediconlabel.h \
|
||||||
src/addressbook.h \
|
src/addressbook.h \
|
||||||
src/logger.h \
|
src/logger.h \
|
||||||
src/addresscombo.h
|
src/addresscombo.h \
|
||||||
|
src/websockets.h
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
src/mainwindow.ui \
|
src/mainwindow.ui \
|
||||||
|
|||||||
Reference in New Issue
Block a user