Browse Source

Clean C++ sample and add Lua version.

cosmy 9 years ago
parent
commit
c2128e054e

+ 7 - 1
Source/Samples/43_HttpRequestDemo/HttpRequestDemo.cpp

@@ -23,6 +23,7 @@
 #include <Urho3D/Core/CoreEvents.h>
 #include <Urho3D/Core/ProcessUtils.h>
 #include <Urho3D/Input/Input.h>
+#include <Urho3D/Network/Network.h>
 #include <Urho3D/Network/HttpRequest.h>
 #include <Urho3D/UI/Font.h>
 #include <Urho3D/UI/Text.h>
@@ -81,8 +82,13 @@ void HttpRequestDemo::SubscribeToEvents()
 
 void HttpRequestDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
 {
+    Network* network = GetSubsystem<Network>();
+
+    if (!network)
+        return;
+
     if (httpRequest_.Null())
-        httpRequest_ = new HttpRequest("http://httpbin.org/ip", "GET", Vector<String>(), String::EMPTY);
+        httpRequest_ = network->MakeHttpRequest("http://httpbin.org/ip");
     else
     {
         // Initializing HTTP request

+ 93 - 0
bin/Data/LuaScripts/43_HttpRequestDemo.lua

@@ -0,0 +1,93 @@
+-- Http request example.
+-- This example demonstrates:
+--     - How to use Http request API
+
+require "LuaScripts/Utilities/Sample"
+
+local message = ""
+local text = nil
+local httpRequest = nil
+
+function Start()
+    -- Execute the common startup for samples
+    SampleStart()
+
+    -- Create the user interface
+    CreateUI()
+
+    -- Set the mouse mode to use in the sample
+    SampleInitMouseMode(MM_FREE)
+
+    -- Subscribe to basic events such as update
+    SubscribeToEvents()
+end
+
+function CreateUI()
+    -- Construct new Text object
+    text = Text:new()
+
+    -- Set font and text color
+    text:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
+    text.color = Color(1.0, 1.0, 0.0)
+
+    -- Align Text center-screen
+    text.horizontalAlignment = HA_CENTER
+    text.verticalAlignment = VA_CENTER
+
+    -- Add Text instance to the UI root element
+    ui.root:AddChild(text)
+end
+
+function SubscribeToEvents()
+    -- Subscribe HandleUpdate() function for processing HTTP request
+    SubscribeToEvent("Update", "HandleUpdate")
+end
+
+function HandleUpdate(eventType, eventData)
+    -- Create HTTP request
+    if httpRequest == nil then
+        httpRequest = network:MakeHttpRequest("http://httpbin.org/ip", "GET")
+    else
+        -- Initializing HTTP request
+        if httpRequest.state == HTTP_INITIALIZING then
+            return
+        -- An error has occured
+        elseif httpRequest.state == HTTP_ERROR then
+            text.text = "An error has occured."
+            UnsubscribeFromEvent("Update")
+        -- Get message data
+        else
+            if httpRequest.availableSize > 0 then
+                message = message + httpRequest:ReadLine()
+            else
+                text.text = "Processing..."
+
+                local json = JSONFile:new()
+                json:FromString(message)
+
+                local val = json:GetRoot():Get("origin")
+
+                if val.isNull then
+                    text.text = "Invalid string."
+                else
+                    text.text =  "Your IP is: " .. val:GetString()
+                end
+
+                val:delete()
+                httpRequest:delete()
+
+                UnsubscribeFromEvent("Update")
+            end       
+        end
+    end
+end
+
+-- Create XML patch instructions for screen joystick layout specific to this sample app
+function GetScreenJoystickPatchString()
+    return
+        "<patch>" ..
+        "    <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
+        "        <attribute name=\"Is Visible\" value=\"false\" />" ..
+        "    </add>" ..
+        "</patch>"
+end