The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: NetworkingDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases networking features.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: NetworkingDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. class NetworkingDemo : public Component,
  36. private Thread
  37. {
  38. public:
  39. NetworkingDemo()
  40. : Thread ("Network Demo")
  41. {
  42. setOpaque (true);
  43. addAndMakeVisible (urlBox);
  44. urlBox.setText ("https://www.google.com");
  45. urlBox.onReturnKey = [this] { fetchButton.triggerClick(); };
  46. addAndMakeVisible (fetchButton);
  47. fetchButton.onClick = [this] { startThread(); };
  48. addAndMakeVisible (resultsBox);
  49. setSize (500, 500);
  50. }
  51. void paint (Graphics& g) override
  52. {
  53. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  54. }
  55. void resized() override
  56. {
  57. auto area = getLocalBounds();
  58. {
  59. auto topArea = area.removeFromTop (40);
  60. fetchButton.setBounds (topArea.removeFromRight (180).reduced (8));
  61. urlBox .setBounds (topArea.reduced (8));
  62. }
  63. resultsBox.setBounds (area.reduced (8));
  64. }
  65. void run() override
  66. {
  67. auto result = getResultText (urlBox.getText());
  68. MessageManagerLock mml (this);
  69. if (mml.lockWasGained())
  70. resultsBox.loadContent (result);
  71. }
  72. String getResultText (const URL& url)
  73. {
  74. StringPairArray responseHeaders;
  75. int statusCode = 0;
  76. std::unique_ptr<InputStream> stream (url.createInputStream (false, nullptr, nullptr, {},
  77. 10000, // timeout in millisecs
  78. &responseHeaders, &statusCode));
  79. if (stream.get() != nullptr)
  80. return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String())
  81. + "Response headers: " + newLine
  82. + responseHeaders.getDescription() + newLine
  83. + "----------------------------------------------------" + newLine
  84. + stream->readEntireStreamAsString();
  85. if (statusCode != 0)
  86. return "Failed to connect, status code = " + String (statusCode);
  87. return "Failed to connect!";
  88. }
  89. private:
  90. TextEditor urlBox;
  91. TextButton fetchButton { "Download URL Contents" };
  92. CodeDocument resultsDocument;
  93. CodeEditorComponent resultsBox { resultsDocument, nullptr };
  94. void lookAndFeelChanged() override
  95. {
  96. urlBox.applyFontToAllText (urlBox.getFont());
  97. }
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkingDemo)
  99. };