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.

138 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: NetworkingDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class NetworkingDemo final : public Component,
  37. private Thread
  38. {
  39. public:
  40. NetworkingDemo()
  41. : Thread ("Network Demo")
  42. {
  43. setOpaque (true);
  44. addAndMakeVisible (urlBox);
  45. urlBox.setText ("https://www.google.com");
  46. urlBox.onReturnKey = [this] { fetchButton.triggerClick(); };
  47. addAndMakeVisible (fetchButton);
  48. fetchButton.onClick = [this] { startThread(); };
  49. addAndMakeVisible (resultsBox);
  50. setSize (500, 500);
  51. }
  52. void paint (Graphics& g) override
  53. {
  54. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  55. }
  56. void resized() override
  57. {
  58. auto area = getLocalBounds();
  59. {
  60. auto topArea = area.removeFromTop (40);
  61. fetchButton.setBounds (topArea.removeFromRight (180).reduced (8));
  62. urlBox .setBounds (topArea.reduced (8));
  63. }
  64. resultsBox.setBounds (area.reduced (8));
  65. }
  66. void run() override
  67. {
  68. auto result = getResultText (urlBox.getText());
  69. MessageManagerLock mml (this);
  70. if (mml.lockWasGained())
  71. resultsBox.loadContent (result);
  72. }
  73. String getResultText (const URL& url)
  74. {
  75. StringPairArray responseHeaders;
  76. int statusCode = 0;
  77. if (auto stream = url.createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress)
  78. .withConnectionTimeoutMs (10000)
  79. .withResponseHeaders (&responseHeaders)
  80. .withStatusCode (&statusCode)))
  81. {
  82. return (statusCode != 0 ? "Status code: " + String (statusCode) + newLine : String())
  83. + "Response headers: " + newLine
  84. + responseHeaders.getDescription() + newLine
  85. + "----------------------------------------------------" + newLine
  86. + stream->readEntireStreamAsString();
  87. }
  88. if (statusCode != 0)
  89. return "Failed to connect, status code = " + String (statusCode);
  90. return "Failed to connect!";
  91. }
  92. private:
  93. TextEditor urlBox;
  94. TextButton fetchButton { "Download URL Contents" };
  95. CodeDocument resultsDocument;
  96. CodeEditorComponent resultsBox { resultsDocument, nullptr };
  97. void lookAndFeelChanged() override
  98. {
  99. urlBox.applyFontToAllText (urlBox.getFont());
  100. }
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NetworkingDemo)
  102. };