Browse Source

enable urho networking for web builds, http request class tweaked for emscripten (wip)

Arnis Lielturks 5 years ago
parent
commit
ae75a8bcf7

+ 1 - 1
CMake/Modules/UrhoCommon.cmake

@@ -147,7 +147,7 @@ option (URHO3D_ANGELSCRIPT "Enable AngelScript scripting support" TRUE)
 option (URHO3D_IK "Enable inverse kinematics support" TRUE)
 option (URHO3D_LUA "Enable additional Lua scripting support" TRUE)
 option (URHO3D_NAVIGATION "Enable navigation support" TRUE)
-cmake_dependent_option (URHO3D_NETWORK "Enable networking support" TRUE "NOT WEB" FALSE)
+option (URHO3D_NETWORK "Enable networking support" TRUE)
 option (URHO3D_WEBSOCKETS "Enable Websockets support" FALSE)
 option (URHO3D_PHYSICS "Enable physics support" TRUE)
 option (URHO3D_URHO2D "Enable 2D graphics and physics support" TRUE)

+ 39 - 0
Source/Urho3D/Network/HttpRequest.cpp

@@ -26,7 +26,11 @@
 #include "../IO/Log.h"
 #include "../Network/HttpRequest.h"
 
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#else
 #include <Civetweb/civetweb.h>
+#endif
 
 #include "../DebugNew.h"
 
@@ -118,6 +122,40 @@ void HttpRequest::ThreadFunction()
             headersStr += header + "\r\n";
     }
 
+#ifdef __EMSCRIPTEN__
+    EM_ASM({
+        let protocol = UTF8ToString($0);
+        let host = UTF8ToString($1);
+        let port = $2;
+        let path = UTF8ToString($3);
+        console.log('Http request params:');
+        console.log('protocol: ', protocol);
+        console.log('host: ', host);
+        console.log('port: ', port);
+        console.log('path: ', path);
+        let url = protocol + '://' + host + ':' + port + '/' + path;
+        console.log('result: ', url);
+
+        let response = await fetch(url);
+
+        fetch(url)
+          .then(response => response.arrayBuffer())
+          .then(data => {
+              // Copy data to Emscripten heap (directly accessed from Module.HEAPU8)
+              const nDataBytes = data.length * data.BYTES_PER_ELEMENT
+              const dataPtr = Module._malloc(nDataBytes)
+              const dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes)
+              dataHeap.set(new Uint8Array(data.buffer))
+
+              // const float_multiply_array = Module.cwrap(
+              //   'MultiplyArray', 'number', ['number', 'number', 'number']
+              // );
+              // Call function and get result
+              //Module.MultiplyArray(2, dataHeap.byteOffset, data.length)
+              Module._free(dataHeap.byteOffset)
+          });
+    }, protocol.CString(), host.CString(), port, path.CString());
+#else
     // Initiate the connection. This may block due to DNS query
     mg_connection* connection = nullptr;
 
@@ -200,6 +238,7 @@ void HttpRequest::ThreadFunction()
     // Close the connection
     mg_close_connection(connection);
 
+#endif
     {
         MutexLock lock(mutex_);
         state_ = HTTP_CLOSED;

+ 7 - 3
Source/Urho3D/Network/WS/WSClient.cpp

@@ -123,7 +123,7 @@ static int WSCallback(struct lws *wsi, enum lws_callback_reasons reason, void *u
                     auto packet = WSClientInstance->GetOutgoingPacket(wsi);
                     if (packet)
                     {
-                        return EM_ASM_INT({
+                        int retval = EM_ASM_INT({
                             var socket = Module.__libwebsocket.socket;
                             if( ! socket ) {
                                 return -1;
@@ -141,6 +141,10 @@ static int WSCallback(struct lws *wsi, enum lws_callback_reasons reason, void *u
                             return $2;
 
                         }, packet->second_.GetData(), packet->second_.GetSize());
+                        if (retval <  packet->second_.GetSize()) {
+                            URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
+                            break;
+                        }
                         WSClientInstance->RemoveOutgoingPacket(wsi);
                     }
                 }
@@ -152,7 +156,7 @@ static int WSCallback(struct lws *wsi, enum lws_callback_reasons reason, void *u
                         memcpy(&buf[LWS_PRE],  packet->second_.GetData(),  packet->second_.GetSize());
                         int retval = lws_write(wsi, &buf[LWS_PRE],  packet->second_.GetSize(), LWS_WRITE_BINARY);
                         if (retval <  packet->second_.GetSize()) {
-                            URHO3D_LOGERRORF("Failed to write to WS, ret = %d", retval);
+                            URHO3D_LOGERRORF("Failed to write to WS, bytes written = %d", retval);
                             break;
                         }
                         WSClientInstance->RemoveOutgoingPacket(wsi);
@@ -243,7 +247,7 @@ int WSClient::Connect(const String& address, unsigned short port)
 #ifndef __EMSCRIPTEN__
     lws_sul_schedule(context, 0, &sul, connect_cb, 50);
 #else
-    EM_ASM_({
+    EM_ASM({
 		var libwebsocket = {};
 
 		libwebsocket.socket = false;