index.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Websocket test</title>
  6. </head>
  7. <body>
  8. <header>
  9. <h1>Websocket test</h1>
  10. <p>change <b>/ws</b> to <b>/ws_mutex</b> for mutex example<br>use <b>disconnect();</b> and <b>connect();</b> in browser console</p>
  11. <div id="status"></div>
  12. </header>
  13. <nav>
  14. <div id="connecting">
  15. <input type="text" id="server" value=""></input>
  16. <button type="button" onclick="toggle_connection()">connection</button>
  17. </div>
  18. <div id="connected">
  19. <input type="text" id="message" value=""></input>
  20. <button type="button" onclick="sendTxt()">send</button>
  21. </div>
  22. </nav>
  23. <main id="content">
  24. <button id="clear" onclick="clearScreen()" >Clear text</button>
  25. <div id="output"></div>
  26. </main>
  27. <script>
  28. async function generateCookie(){
  29. const idLength = 15; // 15 bytes = 120 bits
  30. const randomBytes = new Uint8Array(idLength);
  31. window.crypto.getRandomValues(randomBytes); // crypto strong random bytes
  32. const hashBuffer = await window.crypto.subtle.digest("SHA-256", randomBytes.buffer); // sha-256 hash
  33. const idBytes = new Uint8Array(hashBuffer.slice(0, 15)); // take first 15 bytes of hash
  34. const base64 = btoa(String.fromCharCode.apply(null, idBytes)) // base64 encode (url safe)
  35. .replace(/\+/g, '-')
  36. .replace(/\//g, '_')
  37. .replace(/=+$/, '');
  38. return base64;
  39. }
  40. var cookie_user_id = localStorage.getItem('user_id');
  41. if(!(cookie_user_id)){
  42. generateCookie().then(value => { localStorage.setItem('user_id', value); cookie_user_id = value; });
  43. }
  44. var websocket;
  45. var server = document.getElementById('server');
  46. var message = document.getElementById('message');
  47. var connecting = document.getElementById('connecting');
  48. var connected = document.getElementById('connected');
  49. var content = document.getElementById('content');
  50. var output = document.getElementById('output');
  51. //server.value = 'ws://' + window.location.host + '/ws_mutex';
  52. server.value = 'ws://' + window.location.host + '/ws';
  53. connected.style.display = 'none';
  54. content.style.display = 'none';
  55. function connect(){
  56. //wsHost = server.value;
  57. wsHost = server.value + '?client_id=' + cookie_user_id;
  58. console.log('cookie_user_id = ', cookie_user_id);
  59. console.log('wsHost = ', wsHost);
  60. websocket = new WebSocket(wsHost);
  61. showScreen('<b>Connecting to: ' + wsHost + '</b>');
  62. websocket.onopen = function(evt){ onOpen(evt) };
  63. websocket.onclose = function(evt){ onClose(evt) };
  64. websocket.onmessage = function(evt){ onMessage(evt) };
  65. websocket.onerror = function(evt){ onError(evt) };
  66. }
  67. function disconnect(){
  68. websocket.close();
  69. }
  70. function toggle_connection(){
  71. if(websocket && websocket.readyState == websocket.OPEN){
  72. disconnect();
  73. }else{
  74. connect();
  75. }
  76. }
  77. function sendTxt(){
  78. if(websocket.readyState == websocket.OPEN){
  79. var msg = message.value;
  80. websocket.send(msg);
  81. showScreen('sending: ' + msg);
  82. }else{
  83. showScreen('websocket is not connected');
  84. }
  85. }
  86. function onOpen(evt){
  87. showScreen('<span style="color:green">CONNECTED </span>');
  88. connecting.style.display = 'none';
  89. connected.style.display = '';
  90. content.style.display = '';
  91. }
  92. function onClose(evt){
  93. showScreen('<span style="color:red">DISCONNECTED</span>');
  94. }
  95. function onMessage(evt){
  96. showScreen('<span style="color:blue">RESPONSE: ' + evt.data + '</span>');
  97. }
  98. function onError(evt){
  99. showScreen('<span style="color:red">ERROR: ' + evt.data + '</span>');
  100. }
  101. function showScreen(html){
  102. var el = document.createElement('p');
  103. el.innerHTML = html;
  104. output.insertBefore(el, output.firstChild);
  105. }
  106. function clearScreen(){
  107. output.innerHTML = '';
  108. }
  109. </script>
  110. </body>
  111. </html>