From: gustavo Date: Sun, 22 May 2016 21:17:25 +0000 (+0200) Subject: Shared STOMP: importScripts working in Shared Worker when code inlined in Blob X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=1b4adba0dd7d1eb2fa859b81ada8449d62bfbdcb;p=JavaScriptForFun Shared STOMP: importScripts working in Shared Worker when code inlined in Blob --- diff --git a/angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js b/angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js index 8ec3f6b..c36a5ea 100644 --- a/angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js +++ b/angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js @@ -10,17 +10,19 @@ * @name app.shared-stomp.sharedWorker * * @requires $window + * @requires $location * *

*
* {@link https://docs.angularjs.org/api/ng/service/$window $window}
+ * {@link https://docs.angularjs.org/api/ng/service/$location $location} *

* * @description * sharedWorker service. */ /* @ngInject */ - function sharedWorker($window) { + function sharedWorker($window, $location) { var _messagePort; var _connectSuccessCallback; var _connectErrorCallback; @@ -64,6 +66,7 @@ var message = { command: 'connect', url: url, + urlImports: $location.protocol() + '://' + $location.host(), connectHeaders: connectHeaders }; @@ -151,16 +154,17 @@ } function makeSharedWorker($window) { + var sharedWorkerName = 'shared-stomp'; var sharedWorker; //Make sure blob and create object URL are supported if (workers && $window.Blob && $window.URL.createObjectURL) { //worker's string was loaded successfully var blob = new Blob([workers['shared.js']], {type: 'application/javascript'}); - sharedWorker = new $window.SharedWorker($window.URL.createObjectURL(blob)); + sharedWorker = new $window.SharedWorker($window.URL.createObjectURL(blob), sharedWorkerName); } else { //Fallback! Can be used for debugging purposes. - sharedWorker = new $window.SharedWorker('scripts/workers/shared.js'); + sharedWorker = new $window.SharedWorker('scripts/workers/shared.js', sharedWorkerName); } return sharedWorker; diff --git a/angularjs/stomp/src/stomp/workers/shared.js b/angularjs/stomp/src/stomp/workers/shared.js index 85f7edb..cb19cfc 100644 --- a/angularjs/stomp/src/stomp/workers/shared.js +++ b/angularjs/stomp/src/stomp/workers/shared.js @@ -1,8 +1,5 @@ 'use strict'; -importScripts('sockjs.js'); -importScripts('stomp.min.js'); - var messagePorts = []; var client; var subscription; @@ -38,6 +35,7 @@ function postMessage(message) { function callCommand(data) { switch(data.command) { case 'connect': + doImports(data.urlImports); connect(data.url, data.connectHeaders); break; case 'subscribe': @@ -115,3 +113,8 @@ function disconnectCallback() { command: 'disconnectCallback' }); } + +function doImports(url) { + importScripts(url + '/stomp/js/sockjs.js'); + importScripts(url + '/stomp/js/stomp.min.js'); +}