console.html 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Basic Webhookd UI</title>
  5. <meta charset="UTF-8">
  6. </head>
  7. <body>
  8. <form onsubmit="return sendRequest(this)">
  9. <input name="action" type="text" value="echo" required />
  10. <button type="submit">GET</button>
  11. </form>
  12. <pre id="result">
  13. <!--Server response will be inserted here-->
  14. </pre>
  15. <script>
  16. /**
  17. * @param {HTMLFormElement} form - Form with action.
  18. */
  19. function sendRequest(form) {
  20. const action = form.elements.namedItem("action").value;
  21. const source = new EventSource(`http://localhost:8080/${action}`);
  22. source.onopen = () => {
  23. console.log('connected');
  24. };
  25. source.onmessage = (event) => {
  26. console.log(event.data);
  27. document.getElementById("result").innerHTML += event.data + "<br>";
  28. };
  29. source.onerror = event => {
  30. console.log(event);
  31. source.close()
  32. };
  33. return false;
  34. }
  35. </script>
  36. </body>
  37. </html>