close_observer.html 742 B

123456789101112131415161718192021222324252627282930
  1. <!doctype html>
  2. <title>Observe the connection from a closed tab "Going Away"</title>
  3. <script>
  4. 'use strict';
  5. let protocol = location.protocol.replace('http', 'ws');
  6. let url = protocol + '//' + location.host + '/close-observer?role=observer';
  7. // Do connection test.
  8. let ws = new WebSocket(url);
  9. const id = setTimeout(() => {
  10. console.log('close_observer.html had timeout');
  11. document.title = 'FAIL';
  12. }, 3000);
  13. ws.onmessage = e => {
  14. clearTimeout(id);
  15. console.log('close_observer.html got message: ' + e.data);
  16. document.title = (e.data === 'OK' ? 'PASS' : 'FAIL');
  17. ws.onclose = null;
  18. }
  19. ws.onclose = () => {
  20. clearTimeout(id);
  21. console.log('close_observer.html saw close with no message');
  22. document.title = 'FAIL';
  23. }
  24. </script>