Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.addEventListener('DOMContentLoaded', function () {
- var Share = {
- worker: (function () {
- // CREATE SHARED WORKER AND RETURN IT
- return new SharedWorker('sharedworker.multi-connect.worker.js');
- })(),
- logTo: document.getElementById('shared-worker-log'),
- reportTo: document.getElementById('shared-worker-connection-log')
- };
- // REFLECT Share OBJECT
- console.log(Share);
- // LISTEN ON THE SHAREDWORKER'S PORT FOR NEW MESSAGES
- Share.worker.port.addEventListener('message', function(event) {
- // INITIAL CONNECTION
- if ( event.data.connected ) {
- var workerLog = 'ConnectionId #' + event.data.connectionId +
- ' ' + event.data.pathName +
- ' - Connected: ' + event.data.connected ;
- // APPEND TO LOG FIELD
- Share.logTo.textContent += "\n" + workerLog;
- return;
- }
- // REPORTING CONNECTIONS TO SHARED WORKER
- if ( event.data.connections ) {
- var connectionPaths = event.data.connections;
- console.log('Total Connections: ' + connectionPaths.length);
- for ( var id in connectionPaths ) {
- if ( id !== 'length' ) {
- var connectionLog = '#' + id + ' ' + connectionPaths[id];
- // WRITE TO CONSOLE
- console.log( connectionLog );
- // APPEND TO REPORT FIELD
- Share.reportTo.textContent += "\n" + connectionLog;
- }
- }
- return;
- }
- }, false);
- // START THE CONNECTION TO SHAREDWORKER
- // REQUIRED WHEN USING "addEventListener()"
- Share.worker.port.start();
- // FIRE CONNECTING MESSAGE TO SHAREDWORKER
- Share.worker.port.postMessage({
- 'pathName': location.pathname,
- 'connected' : false
- });
- }, false);
Add Comment
Please, Sign In to add comment