index.html 3.2 KB

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