You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

4 years ago
  1. <!doctype html>
  2. <html><head>
  3. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  4. <script src="http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"></script>
  5. <script src="http://cdn.sockjs.org/websocket-multiplex-0.1.js"></script>
  6. <style>
  7. .box {
  8. width: 300px;
  9. float: left;
  10. margin: 0 20px 0 20px;
  11. }
  12. .box div, .box input {
  13. border: 1px solid;
  14. -moz-border-radius: 4px;
  15. border-radius: 4px;
  16. width: 100%;
  17. padding: 0px;
  18. margin: 5px;
  19. }
  20. .box div {
  21. border-color: grey;
  22. height: 300px;
  23. overflow: auto;
  24. }
  25. .box input {
  26. height: 30px;
  27. }
  28. h1 {
  29. margin-left: 75px;
  30. }
  31. body {
  32. background-color: #F0F0F0;
  33. font-family: "Arial";
  34. }
  35. </style>
  36. </head><body lang="en">
  37. <h1>SockJS Multiplex example</h1>
  38. <div id="first" class="box">
  39. <div></div>
  40. <form><input autocomplete="off" value="Type here..."></input></form>
  41. </div>
  42. <div id="second" class="box">
  43. <div></div>
  44. <form><input autocomplete="off"></input></form>
  45. </div>
  46. <div id="third" class="box">
  47. <div></div>
  48. <form><input autocomplete="off"></input></form>
  49. </div>
  50. <script>
  51. // Pipe - convenience wrapper to present data received from an
  52. // object supporting WebSocket API in an html element. And the other
  53. // direction: data typed into an input box shall be sent back.
  54. var pipe = function(ws, el_name) {
  55. var div = $(el_name + ' div');
  56. var inp = $(el_name + ' input');
  57. var form = $(el_name + ' form');
  58. var print = function(m, p) {
  59. p = (p === undefined) ? '' : JSON.stringify(p);
  60. div.append($("<code>").text(m + ' ' + p));
  61. div.append($("<br>"));
  62. div.scrollTop(div.scrollTop() + 10000);
  63. };
  64. ws.onopen = function() {print('[*] open', ws.protocol);};
  65. ws.onmessage = function(e) {print('[.] message', e.data);};
  66. ws.onclose = function() {print('[*] close');};
  67. form.submit(function() {
  68. print('[ ] sending', inp.val());
  69. ws.send(inp.val());
  70. inp.val('');
  71. return false;
  72. });
  73. };
  74. var sockjs_url = '/multiplex';
  75. var sockjs = new SockJS(sockjs_url);
  76. var multiplexer = new WebSocketMultiplex(sockjs);
  77. var ann = multiplexer.channel('ann');
  78. var bob = multiplexer.channel('bob');
  79. var carl = multiplexer.channel('carl');
  80. pipe(ann, '#first');
  81. pipe(bob, '#second');
  82. pipe(carl, '#third');
  83. $('#first input').focus();
  84. </script>
  85. </body></html>