websockets_string.html 612 B

123456789101112131415161718192021222324
  1. <!-- websockets.html -->
  2. <input id="input" type="text" />
  3. <button onclick="send()">Send</button>
  4. <pre id="output"></pre>
  5. <script>
  6. var input = document.getElementById("input");
  7. var output = document.getElementById("output");
  8. var socket = new WebSocket("ws://localhost:30002/ping");
  9. socket.onopen = function () {
  10. output.innerHTML += "Status: Connected\n";
  11. };
  12. socket.onmessage = function (e) {
  13. output.innerHTML += "Server: " + e.data + "\n";
  14. };
  15. function send() {
  16. socket.send(input.value);
  17. //socket.send(json);
  18. input.value = "";
  19. }
  20. </script>