echo_local.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Web Sockets echo test</title>
  5. <meta charset="UTF-8">
  6. <script src="include/mootools.js"></script>
  7. <script src="include/mootools-more.js"></script>
  8. <script>
  9. var cnt = 0;
  10. var s = null;
  11. var timer = null;
  12. function debug(str) {
  13. cell = $('debug');
  14. cell.innerHTML += str + "<br/>";
  15. }
  16. function send_data() {
  17. debug(">> send_data: testing " + cnt);
  18. s.send("testing " + cnt);
  19. cnt ++;
  20. }
  21. window.onload = function() {
  22. debug(">> window.onload");
  23. var uri = new URI(window.location);
  24. var host = uri.getData("host");
  25. var port = uri.getData("port");
  26. if ((!host) || (!port)) {
  27. debug("must set host and port");
  28. return;
  29. }
  30. var location = "ws://" + host + ":" + port
  31. debug("connecting to " + location);
  32. s = new WebSocket(location);
  33. s.onmessage = function(e) {
  34. debug(">> onmessage: " + e.data);
  35. };
  36. s.onopen = function(e) {
  37. debug(">> onopen" + e);
  38. timer = send_data.periodical(1000);
  39. };
  40. s.onclose = function(e) {
  41. debug(">> onclose: " + e);
  42. if (timer) {
  43. timer = $clear(timer);
  44. }
  45. debug("<< onclose: " + e);
  46. }
  47. debug("<< window.onload");
  48. };
  49. </script>
  50. </head>
  51. <body>
  52. Debug:
  53. <div id="debug" style="width:600px;height:300px"></div>
  54. </body>
  55. </html>