Bladeren bron

采用回调

dong_dl 1 jaar geleden
bovenliggende
commit
475697d3f3
4 gewijzigde bestanden met toevoegingen van 38 en 28 verwijderingen
  1. 13 0
      inc/proto/ProtoParser.h
  2. 2 1
      src/proto/CMakeLists.txt
  3. 23 7
      src/proto/ProtoParser.cpp
  4. 0 20
      src/proto/library_callback.js

+ 13 - 0
inc/proto/ProtoParser.h

@@ -24,6 +24,10 @@ typedef void (*parserCallback)(const char *topic, const char *json, void *userDa
 
 class ProtoParserPrivate;
 
+typedef void (*onConnectedCallback)();
+typedef void (*onDisconnectedCallback)();
+typedef void (*onMessageCallback)(int cmd, int rc, int msgId);
+
 class CLASSAPI ProtoParser
 {
 public:
@@ -179,6 +183,9 @@ public:
     std::string searchConversation(std::string keyword, std::string types, std::string lines);
 
 #ifdef USE_WASM
+    void setOnConnected(int ptr);
+    void setOnDisconnected(int ptr);
+    void setOnMessage(int ptr);
     void setUrl(std::string url)
     {
         _url = url;
@@ -274,6 +281,9 @@ private:
     std::string                  _topic;
     std::string                  _payload;
     parserCallback               _callback = nullptr;
+    onConnectedCallback          _onConnected = nullptr;
+    onDisconnectedCallback       _onDisconnected = nullptr;
+    onMessageCallback            _onMessage = nullptr;
     void                        *_userData;
     uint32_t                     _msgId     = 0;
     uint32_t                     _respMsgId = 0;
@@ -361,6 +371,9 @@ EMSCRIPTEN_BINDINGS(Proto)
         .function("getConversationInfo", &ProtoParser::getConversationInfo)
         .function("getMessages", &ProtoParser::getMessages)
         .function("searchConversation", &ProtoParser::searchConversation)
+        .function("setOnConnected", &ProtoParser::setOnConnected)
+        .function("setOnMessage", &ProtoParser::setOnMessage)
+        .function("setOnDisconnected", &ProtoParser::setOnDisconnected)
 
         .property("timeout", &ProtoParser::getTimeout, &ProtoParser::setTimeout)
         .property("secret", &ProtoParser::getSecret)

+ 2 - 1
src/proto/CMakeLists.txt

@@ -31,11 +31,12 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
     --bind 
     --no-entry 
     -s ALLOW_MEMORY_GROWTH=1 
+    -s EXTRA_EXPORTED_RUNTIME_METHODS=\"['addFunction']\" 
+    -s ALLOW_TABLE_GROWTH 
     -s WASM=1 
     -s ASSERTIONS=0 
     -s FORCE_FILESYSTEM=1 
     -lwebsocket.js 
-    --js-library=${CMAKE_CURRENT_LIST_DIR}/library_callback.js 
     --shell-file=${CMAKE_CURRENT_LIST_DIR}/Proto.html")
 ELSE()
     add_library(${LIB_NAME} SHARED ${ALL_FILES})

+ 23 - 7
src/proto/ProtoParser.cpp

@@ -1535,11 +1535,17 @@ std::string ProtoParser::getMessages(std::string requestJson)
 
 #ifdef USE_WASM
 
-extern "C"
+void ProtoParser::setOnConnected(int ptr)
 {
-    extern void onMessage(int r);
-    extern void onConnected();
-    extern void onDisconnected();
+    _onConnected = (onConnectedCallback)ptr;
+}
+void ProtoParser::setOnDisconnected(int ptr)
+{
+    _onDisconnected = (onDisconnectedCallback)ptr;
+}
+void ProtoParser::setOnMessage(int ptr)
+{
+    _onMessage = (onMessageCallback)ptr;
 }
 
 int ProtoParser::reconnectVal()
@@ -1591,7 +1597,11 @@ int ProtoParser::connectVal(std::string requestJson)
 EM_BOOL ProtoParser::onWebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent, void *userData)
 {
     MqttLog(MQTT_LOG_ERROR, "web socket closed");
-    onDisconnected();
+    ProtoParser *parser = (ProtoParser *)userData;
+    if (parser->_onDisconnected != nullptr)
+    {
+        parser->_onDisconnected();
+    }
     return EM_TRUE;
 }
 EM_BOOL ProtoParser::onWebSocketError(int eventType, const EmscriptenWebSocketErrorEvent *websocketEvent, void *userData)
@@ -1606,7 +1616,10 @@ EM_BOOL ProtoParser::onWebSocketOpen(int eventType, const EmscriptenWebSocketOpe
     auto res = parser->connectPack(parser->_uid);
     parser->send(res);
 
-    onConnected();
+    if (parser->_onConnected != nullptr)
+    {
+        parser->_onConnected();
+    }
     return EM_TRUE;
 }
 
@@ -1621,7 +1634,10 @@ EM_BOOL ProtoParser::onWebSocketMessage(int eventType, const EmscriptenWebSocket
     ProtoParser         *parser = (ProtoParser *)userData;
     std::vector<uint8_t> data(websocketEvent->data, websocketEvent->data + websocketEvent->numBytes);
     int                  r = parser->parse(data);
-    onMessage(r);
+    if (parser->_onMessage != nullptr)
+    {
+        parser->_onMessage(r, parser->_rc, parser->_respMsgId);
+    }
     return EM_TRUE;
 }
 

+ 0 - 20
src/proto/library_callback.js

@@ -1,20 +0,0 @@
-mergeInto(LibraryManager.library, {
-    onMessage: function (r) {
-		if (wfc)
-			wfc.onMessage(r)
-		else if (window.wfc)
-			window.wfc.onMessage(r)
-    },
-    onDisconnected: function () {
-		if (wfc)
-			wfc.onDisconnected()
-		else if (window.wfc)
-			window.wfc.onDisconnected()
-    },
-	onConnected: function () {
-		if (wfc)
-			wfc.onConnected()
-		else if (window.wfc)
-			window.wfc.onConnected()
-    },
-});