html_ws_client.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  4. <title>Websocket client</title>
  5. <script src="/static/jquery.min.js"></script>
  6. <script type="text/javascript">
  7. var websocket;
  8. $(document).ready(init);
  9. function init() {
  10. if(!("WebSocket" in window)){
  11. $('#status').append('<p><span style="color: red;">websockets are not supported </span></p>');
  12. $("#navigation").hide();
  13. } else {
  14. $('#status').append('<p><span style="color: green;">websockets are supported </span></p>');
  15. connect();
  16. };
  17. $("#connected").hide();
  18. $("#content").hide();
  19. };
  20. function connect()
  21. {
  22. wsHost = $("#server").val()
  23. websocket = new WebSocket(wsHost);
  24. showScreen('<b>Connecting to: ' + wsHost + '</b>');
  25. websocket.onopen = function(evt) { onOpen(evt) };
  26. websocket.onclose = function(evt) { onClose(evt) };
  27. websocket.onmessage = function(evt) { onMessage(evt) };
  28. websocket.onerror = function(evt) { onError(evt) };
  29. };
  30. function disconnect() {
  31. websocket.close();
  32. };
  33. function toggle_connection(){
  34. if(websocket.readyState == websocket.OPEN){
  35. disconnect();
  36. } else {
  37. connect();
  38. };
  39. };
  40. function sendTxt() {
  41. if(websocket.readyState == websocket.OPEN){
  42. txt = $("#send_txt").val();
  43. websocket.send(txt);
  44. showScreen('sending: ' + txt);
  45. } else {
  46. showScreen('websocket is not connected');
  47. };
  48. };
  49. function onOpen(evt) {
  50. showScreen('<span style="color: green;">CONNECTED </span>');
  51. $("#connected").fadeIn('slow');
  52. $("#content").fadeIn('slow');
  53. };
  54. function onClose(evt) {
  55. showScreen('<span style="color: red;">DISCONNECTED </span>');
  56. };
  57. function onMessage(evt) {
  58. showScreen('<span style="color: blue;">RESPONSE: ' + evt.data+ '</span>');
  59. };
  60. function showScreen(txt) {
  61. $('#output').prepend('<p>' + txt + '</p>');
  62. };
  63. function clearScreen()
  64. {
  65. $('#output').html("");
  66. };
  67. </script>
  68. </head>
  69. <body>
  70. <div id="header">
  71. <h1>Websocket client</h1>
  72. <div id="status"></div>
  73. </div>
  74. <div id="navigation">
  75. <p id="connecting">
  76. <input type='text' id="server" value="ws://localhost:8080/websocket"></input>
  77. <button type="button" onclick="toggle_connection()">connection</button>
  78. </p>
  79. <div id="connected">
  80. <p>
  81. <input type='text' id="send_txt" value=></input>
  82. <button type="button" onclick="sendTxt();">send</button>
  83. </p>
  84. </div>
  85. <div id="content">
  86. <button id="clear" onclick="clearScreen()" >Clear text</button>
  87. <div id="output"></div>
  88. </div>
  89. </div>
  90. </body>
  91. </html>