HttpRequestDemo.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/ProcessUtils.h>
  5. #include <Urho3D/Input/Input.h>
  6. #include <Urho3D/Network/Network.h>
  7. #include <Urho3D/Network/HttpRequest.h>
  8. #include <Urho3D/UI/Font.h>
  9. #include <Urho3D/UI/Text.h>
  10. #include <Urho3D/UI/UI.h>
  11. #include "HttpRequestDemo.h"
  12. #include <Urho3D/DebugNew.h>
  13. URHO3D_DEFINE_APPLICATION_MAIN(HttpRequestDemo)
  14. HttpRequestDemo::HttpRequestDemo(Context* context) :
  15. Sample(context)
  16. {
  17. }
  18. void HttpRequestDemo::Start()
  19. {
  20. // Execute base class startup
  21. Sample::Start();
  22. // Create the user interface
  23. CreateUI();
  24. // Subscribe to basic events such as update
  25. SubscribeToEvents();
  26. // Set the mouse mode to use in the sample
  27. Sample::InitMouseMode(MM_FREE);
  28. }
  29. void HttpRequestDemo::CreateUI()
  30. {
  31. auto* cache = GetSubsystem<ResourceCache>();
  32. // Construct new Text object
  33. text_ = new Text(context_);
  34. // Set font and text color
  35. text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  36. text_->SetColor(Color(1.0f, 1.0f, 0.0f));
  37. // Align Text center-screen
  38. text_->SetHorizontalAlignment(HA_CENTER);
  39. text_->SetVerticalAlignment(VA_CENTER);
  40. // Add Text instance to the UI root element
  41. GetSubsystem<UI>()->GetRoot()->AddChild(text_);
  42. }
  43. void HttpRequestDemo::SubscribeToEvents()
  44. {
  45. // Subscribe HandleUpdate() function for processing HTTP request
  46. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HttpRequestDemo, HandleUpdate));
  47. }
  48. void HttpRequestDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
  49. {
  50. auto* network = GetSubsystem<Network>();
  51. if (httpRequest_.Null())
  52. #ifdef URHO3D_SSL
  53. httpRequest_ = network->MakeHttpRequest("https://api.ipify.org/?format=json");
  54. #else
  55. httpRequest_ = network->MakeHttpRequest("http://httpbin.org/ip");
  56. #endif
  57. else
  58. {
  59. // Initializing HTTP request
  60. if (httpRequest_->GetState() == HTTP_INITIALIZING)
  61. return;
  62. // An error has occurred
  63. else if (httpRequest_->GetState() == HTTP_ERROR)
  64. {
  65. text_->SetText("An error has occurred: " + httpRequest_->GetError());
  66. UnsubscribeFromEvent("Update");
  67. URHO3D_LOGERRORF("HttpRequest error: %s", httpRequest_->GetError().CString());
  68. }
  69. // Get message data
  70. else
  71. {
  72. if (httpRequest_->GetAvailableSize() > 0)
  73. message_ += httpRequest_->ReadLine();
  74. else
  75. {
  76. text_->SetText("Processing...");
  77. SharedPtr<JSONFile> json(new JSONFile(context_));
  78. json->FromString(message_);
  79. #ifdef URHO3D_SSL
  80. JSONValue val = json->GetRoot().Get("ip");
  81. #else
  82. JSONValue val = json->GetRoot().Get("origin");
  83. #endif
  84. if (val.IsNull())
  85. text_->SetText("Invalid JSON response retrieved!");
  86. else
  87. text_->SetText("Your IP is: " + val.GetString());
  88. UnsubscribeFromEvent("Update");
  89. }
  90. }
  91. }
  92. }