Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. document.addEventListener('DOMContentLoaded', function () {
  2.  
  3. var Share = {
  4. worker: (function () {
  5. // CREATE SHARED WORKER AND RETURN IT
  6. return new SharedWorker('sharedworker.multi-connect.worker.js');
  7. })(),
  8. logTo: document.getElementById('shared-worker-log'),
  9. reportTo: document.getElementById('shared-worker-connection-log')
  10. };
  11.  
  12.  
  13. // REFLECT Share OBJECT
  14. console.log(Share);
  15.  
  16. // LISTEN ON THE SHAREDWORKER'S PORT FOR NEW MESSAGES
  17. Share.worker.port.addEventListener('message', function(event) {
  18.  
  19. // INITIAL CONNECTION
  20. if ( event.data.connected ) {
  21. var workerLog = 'ConnectionId #' + event.data.connectionId +
  22. ' ' + event.data.pathName +
  23. ' - Connected: ' + event.data.connected ;
  24.  
  25. // APPEND TO LOG FIELD
  26. Share.logTo.textContent += "\n" + workerLog;
  27.  
  28. return;
  29. }
  30.  
  31. // REPORTING CONNECTIONS TO SHARED WORKER
  32. if ( event.data.connections ) {
  33. var connectionPaths = event.data.connections;
  34.  
  35. console.log('Total Connections: ' + connectionPaths.length);
  36.  
  37. for ( var id in connectionPaths ) {
  38.  
  39. if ( id !== 'length' ) {
  40.  
  41. var connectionLog = '#' + id + ' ' + connectionPaths[id];
  42.  
  43. // WRITE TO CONSOLE
  44. console.log( connectionLog );
  45.  
  46. // APPEND TO REPORT FIELD
  47. Share.reportTo.textContent += "\n" + connectionLog;
  48. }
  49. }
  50. return;
  51. }
  52.  
  53.  
  54. }, false);
  55.  
  56. // START THE CONNECTION TO SHAREDWORKER
  57. // REQUIRED WHEN USING "addEventListener()"
  58. Share.worker.port.start();
  59. // FIRE CONNECTING MESSAGE TO SHAREDWORKER
  60. Share.worker.port.postMessage({
  61. 'pathName': location.pathname,
  62. 'connected' : false
  63. });
  64.  
  65.  
  66. }, false);
Add Comment
Please, Sign In to add comment