HttpRequestDemo.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2008-2016 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/HttpRequest.h>
  26. #include <Urho3D/UI/Font.h>
  27. #include <Urho3D/UI/Text.h>
  28. #include <Urho3D/UI/UI.h>
  29. #include "HttpRequestDemo.h"
  30. #include <Urho3D/DebugNew.h>
  31. URHO3D_DEFINE_APPLICATION_MAIN(HttpRequestDemo)
  32. HttpRequestDemo::HttpRequestDemo(Context* context) :
  33. Sample(context)
  34. {
  35. }
  36. void HttpRequestDemo::Start()
  37. {
  38. // Execute base class startup
  39. Sample::Start();
  40. // Create the user interface
  41. CreateUI();
  42. // Subscribe to basic events such as update
  43. SubscribeToEvents();
  44. // Set the mouse mode to use in the sample
  45. Sample::InitMouseMode(MM_FREE);
  46. }
  47. void HttpRequestDemo::CreateUI()
  48. {
  49. ResourceCache* cache = GetSubsystem<ResourceCache>();
  50. // Construct new Text object
  51. text_ = new Text(context_);
  52. // Set font and text color
  53. text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  54. text_->SetColor(Color(1.0f, 1.0f, 0.0f));
  55. // Align Text center-screen
  56. text_->SetHorizontalAlignment(HA_CENTER);
  57. text_->SetVerticalAlignment(VA_CENTER);
  58. // Add Text instance to the UI root element
  59. GetSubsystem<UI>()->GetRoot()->AddChild(text_);
  60. }
  61. void HttpRequestDemo::SubscribeToEvents()
  62. {
  63. // Subscribe HandleUpdate() function for processing HTTP request
  64. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HttpRequestDemo, HandleUpdate));
  65. }
  66. void HttpRequestDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
  67. {
  68. if (httpRequest_.Null())
  69. httpRequest_ = new HttpRequest("http://httpbin.org/ip", "GET", {}, "");
  70. else
  71. {
  72. // Initializing HTTP request
  73. if (httpRequest_->GetState() == HTTP_INITIALIZING)
  74. return;
  75. // An error has occured
  76. else if (httpRequest_->GetState() == HTTP_ERROR)
  77. {
  78. text_->SetText("An error has occured.");
  79. UnsubscribeFromEvent("Update");
  80. }
  81. // Get message data
  82. else
  83. {
  84. if (httpRequest_->GetAvailableSize() > 0)
  85. message_ += httpRequest_->ReadLine();
  86. else
  87. {
  88. text_->SetText("Processing...");
  89. SharedPtr<JSONFile> json(new JSONFile(context_));
  90. json->FromString(message_);
  91. JSONValue val = json->GetRoot().Get("origin");
  92. if (val.IsNull())
  93. text_->SetText("Invalid string.");
  94. else
  95. text_->SetText("Your IP is: " + val.GetString());
  96. UnsubscribeFromEvent("Update");
  97. }
  98. }
  99. }
  100. }