stomp, shared worker improvements
authorgustavo <gu.martinm@gmail.com>
Sun, 28 Aug 2016 21:37:24 +0000 (23:37 +0200)
committergustavo <gu.martinm@gmail.com>
Sun, 28 Aug 2016 21:37:24 +0000 (23:37 +0200)
angularjs/stomp/src/stomp/app/example/example.controller.js
angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.controller.js
angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.html
angularjs/stomp/src/stomp/app/shared-stomp/shared-worker.service.js
angularjs/stomp/src/stomp/index.html
angularjs/stomp/src/stomp/workers/shared.js

index 35c31c5..cce4e6c 100644 (file)
@@ -45,8 +45,6 @@
       id: 123456
     }, null, 4);
 
-
-
     vm.connect = function () {
       vm.debugLog = '';
       // use SockJS implementation instead of the browser's native implementation
     }
 
     function stompClientDebug(string) {
-        vm.debugLog = vm.debugLog + string + '\n';
+      vm.debugLog = vm.debugLog + string + '\n';
     }
   }
 
index 8eb57f0..1f7ee75 100644 (file)
@@ -24,7 +24,9 @@
   function SharedStompController($location, sharedWorker) {
     var vm = this;
 
-    vm.url = $location.protocol() + '://' + $location.host() + '/spring-stomp-server-full/fullportfolio';
+    vm.endpointSimple = $location.protocol() + '://' + $location.host() + '/spring-stomp-server-simple/portfolio';
+    vm.endpointFull = $location.protocol() + '://' + $location.host() + '/spring-stomp-server-full/fullportfolio';
+    vm.url = vm.endpointSimple;
     vm.clientDestination = '/topic/greeting';
     vm.serverDestination = '/app/greeting';
     vm.connectHeaders = JSON.stringify({
       id: 123456
     }, null, 4);
 
-
-
     vm.connect = function () {
       sharedWorker.connect(vm.url, JSON.parse(vm.connectHeaders), connectSuccessCallback, connectErrorCallback);
     };
 
     vm.subscribe = function () {
-      sharedWorker.subscribe(vm.clientDestination, subscribeCallback, JSON.parse(subscribeHeaders));
+      sharedWorker.subscribe(vm.clientDestination, subscribeCallback, JSON.parse(vm.subscribeHeaders));
     };
 
     vm.unSubscribe = function () {
       alert('got connection');
     }
 
-    function connectErrorCallback(error) {
+    function connectErrorCallback(jsonConnectErrorAsString) {
       // display the error's message header:
-      alert(error.headers.message);
+      alert('error call back: \n' + jsonConnectErrorAsString);
     }
 
-    function subscribeCallback(message) {
+    function subscribeCallback(jsonMessageAsString) {
       // called when the client receives a STOMP message from the server
-      if (message.body) {
-        alert('got message with body ' + message.body);
-      } else {
-        alert('got empty message');
-      }
+      alert('subscribe message: \n' + jsonMessageAsString);
     }
 
     function disconnectCallback() {
index b0d92c6..fadf45e 100644 (file)
@@ -7,6 +7,8 @@
 
     <label>
       Endpoint:<br>
+      <textarea style="width: 200%; border: none">{{vm.endpointSimple}}</textarea><br>
+      <textarea style="width: 200%; border: none">{{vm.endpointFull}}</textarea><br>
       <input value="http://localhost:8080" ng-model="vm.url" placeholder="url">
     </label>
     <br>
index c36a5ea..d6f0c11 100644 (file)
           _connectSuccessCallback();
           break;
         case 'connectErrorCallback':
-          _connectErrorCallback(data.connectError);
+          _connectErrorCallback(data.jsonConnectErrorAsString);
           break;
         case 'subscribeCallback':
-          _subscribeCallback(data.message);
+          _subscribeCallback(data.jsonMessageAsString);
           break;
         case 'disconnectCallback':
           _disconnectCallback();
index 185172d..fdd6587 100644 (file)
     <!-- Custom JavaScript -->
     <!-- build:js(.) js/app.min.js -->
     <!-- inject:js -->
-    <script src="/src/stomp/app/widgets/widgets.module.js"></script>
-    <script src="/src/stomp/app/widgets/auto-height.directive.js"></script>
     <script src="/src/stomp/app/shared-stomp/shared-stomp.module.js"></script>
     <script src="/src/stomp/app/shared-stomp/shared-worker.service.js"></script>
     <script src="/src/stomp/app/shared-stomp/shared-stomp.route.js"></script>
     <script src="/src/stomp/app/shared-stomp/shared-stomp.controller.js"></script>
+    <script src="/src/stomp/app/widgets/widgets.module.js"></script>
+    <script src="/src/stomp/app/widgets/auto-height.directive.js"></script>
     <script src="/src/stomp/app/example/example.module.js"></script>
     <script src="/src/stomp/app/example/example.route.js"></script>
     <script src="/src/stomp/app/example/example.controller.js"></script>
index d8b07d5..a5c96b0 100644 (file)
@@ -3,6 +3,7 @@
 var messagePorts = [];
 var client;
 var subscription;
+var isConnected = false;
 
 self.onconnect = function(event) {
   var messagePort = event.ports[0];
@@ -66,6 +67,12 @@ function callCommand(data) {
 }
 
 function connect(url, connectHeaders) {
+  if (!isConnected) {
+    doConnect(url, connectHeaders)
+  }
+}
+
+function doConnect(url, connectHeaders) {
   var options = {
     debug: true,
     devel: true,
@@ -78,12 +85,13 @@ function connect(url, connectHeaders) {
   var ws = new SockJS(url, undefined, options);
   client = Stomp.over(ws);
   client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms
-  client.heartbeat.incoming = 20000;     // client does not want to receive heartbeats from the server
+  client.heartbeat.incoming = 20000; // server will send heartbeats every 20000ms
+  client.debug = stompClientDebug;
   client.connect(connectHeaders, connectSuccessCallback, connectErrorCallback);
 }
 
 function subscribe(clientDestination, subscribeHeaders) {
-  subscription = client.subscribe(clientDestination, subscribeCallback, JSON.parse(subscribeHeaders));
+  subscription = client.subscribe(clientDestination, subscribeCallback, subscribeHeaders);
 }
 
 function unSubscribe() {
@@ -91,7 +99,7 @@ function unSubscribe() {
 }
 
 function send(serverDestination, sendHeaders, payload) {
-  client.send(serverDestination, JSON.parse(sendHeaders), payload);
+  client.send(serverDestination, sendHeaders, payload);
 }
 
 function disconnect() {
@@ -99,31 +107,40 @@ function disconnect() {
 }
 
 function connectSuccessCallback() {
+  isConnected = true;
   postMessage({
     command: 'connectSuccessCallback'
   });
 }
 
 function connectErrorCallback(error) {
+  isConnected = false;
+  var jsonConnectErrorAsString = JSON.stringify(error, null, 4);
   postMessage({
     command: 'connectErrorCallback',
-    connectError: error
+    jsonConnectErrorAsString: jsonConnectErrorAsString
   });
 }
 
 function subscribeCallback(message) {
+  var jsonMessageAsString = JSON.stringify(message, null, 4);
   postMessage({
     command: 'subscribeCallback',
-    message: message
+    jsonMessageAsString: jsonMessageAsString
   });
 }
 
 function disconnectCallback() {
+  isConnected = false;
   postMessage({
     command: 'disconnectCallback'
   });
 }
 
+function stompClientDebug(string) {
+  self.console.log(string);
+}
+
 function doImports(url) {
   importScripts(url + '/stomp/js/sockjs.js');
   importScripts(url + '/stomp/js/stomp.min.js');