Shared STOMP: importScripts working in Shared Worker when code inlined in Blob
authorgustavo <gu.martinm@gmail.com>
Sun, 22 May 2016 21:17:25 +0000 (23:17 +0200)
committergustavo <gu.martinm@gmail.com>
Sun, 22 May 2016 21:17:25 +0000 (23:17 +0200)
angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js
angularjs/stomp/src/stomp/workers/shared.js

index 8ec3f6b..c36a5ea 100644 (file)
    * @name app.shared-stomp.sharedWorker
    *
    * @requires $window
+   * @requires $location
    *
    * <p>
    * <br>
    * {@link https://docs.angularjs.org/api/ng/service/$window $window} <br>
+   * {@link https://docs.angularjs.org/api/ng/service/$location $location}
    * </p>
    *
    * @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
       };
 
   }
 
   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;
index 85f7edb..cb19cfc 100644 (file)
@@ -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');
+}