mirror of
https://gitlab.com/theadib/JSonRPCPlugin.git
synced 2026-08-29 23:50:27 +08:00
53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
#ifndef JSONRPCSERVER_H
|
|
#define JSONRPCSERVER_H
|
|
|
|
#include <QWebSocketServer>
|
|
#include <QWebSocket>
|
|
#include <QObject>
|
|
#include <QMap>
|
|
#include <functional>
|
|
#include "ccStdPluginInterface.h"
|
|
|
|
class JsonRPCResult {
|
|
public:
|
|
static JsonRPCResult error(int code, QString message) {
|
|
JsonRPCResult result = { .isError = true, .error_code = code, .error_message = message };
|
|
return result;
|
|
}
|
|
static JsonRPCResult success(QVariant value) {
|
|
JsonRPCResult result = { .isError = false, .result = value };
|
|
return result;
|
|
}
|
|
bool isError{true};
|
|
int error_code{-32601};
|
|
QString error_message = "Method not found";
|
|
QVariant result;
|
|
|
|
};
|
|
|
|
class JsonRPCServer : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit JsonRPCServer(QObject *parent = nullptr);
|
|
~JsonRPCServer();
|
|
|
|
void listen(unsigned int port);
|
|
void close();
|
|
|
|
signals:
|
|
JsonRPCResult execute(QString method, QMap<QString, QVariant> params);
|
|
private slots:
|
|
void onNewConnection();
|
|
void onClosed();
|
|
void processTextMessage(QString message);
|
|
void processBinaryMessage(QByteArray message);
|
|
void socketDisconnected();
|
|
|
|
private:
|
|
QWebSocketServer *ws_server{nullptr};
|
|
QList<QWebSocket * > connections;
|
|
};
|
|
|
|
#endif // JSONRPCSERVER_H
|