From bb48faaf477178bd2b387bab05ff5dfa99786373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Mon, 7 Jun 2021 17:12:55 +0800 Subject: [PATCH] add notification plugin --- doric-Qt/example/doric/doric.pro | 2 + .../doric/plugin/DoricNotificationPlugin.cpp | 46 +++++++++++++++++++ .../doric/plugin/DoricNotificationPlugin.h | 25 ++++++++++ 3 files changed, 73 insertions(+) create mode 100644 doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp create mode 100644 doric-Qt/example/doric/plugin/DoricNotificationPlugin.h diff --git a/doric-Qt/example/doric/doric.pro b/doric-Qt/example/doric/doric.pro index 97e6fedd..c3d7babf 100644 --- a/doric-Qt/example/doric/doric.pro +++ b/doric-Qt/example/doric/doric.pro @@ -38,6 +38,7 @@ SOURCES += \ engine/v8/V8Executor.cpp \ plugin/DoricModalPlugin.cpp \ plugin/DoricNetworkPlugin.cpp \ + plugin/DoricNotificationPlugin.cpp \ plugin/DoricPopoverPlugin.cpp \ plugin/DoricShaderPlugin.cpp \ plugin/DoricStoragePlugin.cpp \ @@ -124,6 +125,7 @@ HEADERS += \ plugin/DoricModalPlugin.h \ plugin/DoricNativePlugin.h \ plugin/DoricNetworkPlugin.h \ + plugin/DoricNotificationPlugin.h \ plugin/DoricPopoverPlugin.h \ plugin/DoricShaderPlugin.h \ plugin/DoricStoragePlugin.h \ diff --git a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp new file mode 100644 index 00000000..5c8dc26f --- /dev/null +++ b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.cpp @@ -0,0 +1,46 @@ +#include "DoricNotificationPlugin.h" + +#include "engine/DoricPromise.h" + +#include +#include +#include + +void DoricNotificationPlugin::publish(QString jsValueString, + QString callbackId) { + QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); + QJsonValue jsValue = document.object(); + + getContext()->getDriver()->asyncCall( + [this, callbackId] { + QVariantList args; + DoricPromise::resolve(getContext(), callbackId, args); + }, + DoricThreadMode::JS); +} + +void DoricNotificationPlugin::subscribe(QString jsValueString, + QString callbackId) { + QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); + QJsonValue jsValue = document.object(); + + getContext()->getDriver()->asyncCall( + [this, callbackId] { + QVariantList args; + DoricPromise::resolve(getContext(), callbackId, args); + }, + DoricThreadMode::JS); +} + +void DoricNotificationPlugin::unsubscribe(QString jsValueString, + QString callbackId) { + QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); + QJsonValue jsValue = document.object(); + + getContext()->getDriver()->asyncCall( + [this, callbackId] { + QVariantList args; + DoricPromise::resolve(getContext(), callbackId, args); + }, + DoricThreadMode::JS); +} diff --git a/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h new file mode 100644 index 00000000..3422352d --- /dev/null +++ b/doric-Qt/example/doric/plugin/DoricNotificationPlugin.h @@ -0,0 +1,25 @@ +#ifndef DORICNOTIFICATIONPLUGIN_H +#define DORICNOTIFICATIONPLUGIN_H + +#include "DoricExport.h" + +#include "DoricNativePlugin.h" + +#include + +class DORIC_EXPORT DoricNotificationPlugin : public DoricNativePlugin { + Q_OBJECT + +private: + QUdpSocket udpSocket; + +public: + using DoricNativePlugin::DoricNativePlugin; + + Q_INVOKABLE void publish(QString jsValueString, QString callbackId); + + Q_INVOKABLE void subscribe(QString jsValueString, QString callbackId); + + Q_INVOKABLE void unsubscribe(QString jsValueString, QString callbackId); +}; +#endif // DORICNOTIFICATIONPLUGIN_H