Aditya Kulkarni
5 years ago
7 changed files with 125 additions and 8 deletions
@ -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(); |
||||
|
} |
||||
|
} |
@ -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
|
Loading…
Reference in new issue