sample.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!--
  2. Lincense: Public Domain
  3. -->
  4. <html><head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Sample of web_socket.js</title>
  7. <!-- Include these three JS files: -->
  8. <script type="text/javascript" src="swfobject.js"></script>
  9. <script type="text/javascript" src="FABridge.js"></script>
  10. <script type="text/javascript" src="web_socket.js"></script>
  11. <script type="text/javascript">
  12. // Set URL of your WebSocketMain.swf here:
  13. WebSocket.__swfLocation = "WebSocketMain.swf";
  14. var ws;
  15. function init() {
  16. // Connect to Web Socket.
  17. // Change host/port here to your own Web Socket server.
  18. ws = new WebSocket("ws://localhost:10081/");
  19. // Set event handlers.
  20. ws.onopen = function() {
  21. output("onopen");
  22. };
  23. ws.onmessage = function(e) {
  24. // e.data contains received string.
  25. output("onmessage: " + e.data);
  26. };
  27. ws.onclose = function() {
  28. output("onclose");
  29. };
  30. ws.onerror = function() {
  31. output("onerror");
  32. };
  33. }
  34. function onSubmit() {
  35. var input = document.getElementById("input");
  36. // You can send message to the Web Socket using ws.send.
  37. ws.send(input.value);
  38. output("send: " + input.value);
  39. input.value = "";
  40. input.focus();
  41. }
  42. function onCloseClick() {
  43. ws.close();
  44. }
  45. function output(str) {
  46. var log = document.getElementById("log");
  47. var escaped = str.replace(/&/, "&amp;").replace(/</, "&lt;").
  48. replace(/>/, "&gt;").replace(/"/, "&quot;"); // "
  49. log.innerHTML = escaped + "<br>" + log.innerHTML;
  50. }
  51. </script>
  52. </head><body onload="init();">
  53. <form onsubmit="onSubmit(); return false;">
  54. <input type="text" id="input">
  55. <input type="submit" value="Send">
  56. <button onclick="onCloseClick(); return false;">close</button>
  57. </form>
  58. <div id="log"></div>
  59. </body></html>