HttpRequestDemo.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Core/ProcessUtils.h>
  24. #include <Urho3D/Input/Input.h>
  25. #include <Urho3D/Network/Network.h>
  26. #include <Urho3D/Network/HttpRequest.h>
  27. #include <Urho3D/UI/Font.h>
  28. #include <Urho3D/UI/Text.h>
  29. #include <Urho3D/UI/UI.h>
  30. #include "HttpRequestDemo.h"
  31. #include <Urho3D/DebugNew.h>
  32. URHO3D_DEFINE_APPLICATION_MAIN(HttpRequestDemo)
  33. HttpRequestDemo::HttpRequestDemo(Context* context) :
  34. Sample(context)
  35. {
  36. }
  37. void HttpRequestDemo::Start()
  38. {
  39. // Execute base class startup
  40. Sample::Start();
  41. // Create the user interface
  42. CreateUI();
  43. // Subscribe to basic events such as update
  44. SubscribeToEvents();
  45. // Set the mouse mode to use in the sample
  46. Sample::InitMouseMode(MM_FREE);
  47. }
  48. void HttpRequestDemo::CreateUI()
  49. {
  50. auto* cache = GetSubsystem<ResourceCache>();
  51. // Construct new Text object
  52. text_ = new Text(context_);
  53. // Set font and text color
  54. text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  55. text_->SetColor(Color(1.0f, 1.0f, 0.0f));
  56. // Align Text center-screen
  57. text_->SetHorizontalAlignment(HA_CENTER);
  58. text_->SetVerticalAlignment(VA_CENTER);
  59. // Add Text instance to the UI root element
  60. GetSubsystem<UI>()->GetRoot()->AddChild(text_);
  61. }
  62. void HttpRequestDemo::SubscribeToEvents()
  63. {
  64. // Subscribe HandleUpdate() function for processing HTTP request
  65. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HttpRequestDemo, HandleUpdate));
  66. }
  67. void HttpRequestDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
  68. {
  69. auto* network = GetSubsystem<Network>();
  70. if (httpRequest_.Null())
  71. httpRequest_ = network->MakeHttpRequest("http://httpbin.org/ip");
  72. else
  73. {
  74. // Initializing HTTP request
  75. if (httpRequest_->GetState() == HTTP_INITIALIZING)
  76. return;
  77. // An error has occurred
  78. else if (httpRequest_->GetState() == HTTP_ERROR)
  79. {
  80. text_->SetText("An error has occurred.");
  81. UnsubscribeFromEvent("Update");
  82. }
  83. // Get message data
  84. else
  85. {
  86. if (httpRequest_->GetAvailableSize() > 0)
  87. message_ += httpRequest_->ReadLine();
  88. else
  89. {
  90. text_->SetText("Processing...");
  91. SharedPtr<JSONFile> json(new JSONFile(context_));
  92. json->FromString(message_);
  93. JSONValue val = json->GetRoot().Get("origin");
  94. if (val.IsNull())
  95. text_->SetText("Invalid string.");
  96. else
  97. text_->SetText("Your IP is: " + val.GetString());
  98. UnsubscribeFromEvent("Update");
  99. }
  100. }
  101. }
  102. }