From 6161018409431bb1050a3abca29a84bbecc4e42b Mon Sep 17 00:00:00 2001 From: gustavo Date: Sun, 28 Aug 2016 23:37:24 +0200 Subject: [PATCH] stomp, shared worker improvements --- .../src/stomp/app/example/example.controller.js | 4 +--- .../app/shared-stomp/shared-stomp.controller.js | 20 +++++++--------- .../src/stomp/app/shared-stomp/shared-stomp.html | 2 ++ .../app/shared-stomp/shared-worker.service.js | 4 ++-- angularjs/stomp/src/stomp/index.html | 4 ++-- angularjs/stomp/src/stomp/workers/shared.js | 27 ++++++++++++++++++---- 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/angularjs/stomp/src/stomp/app/example/example.controller.js b/angularjs/stomp/src/stomp/app/example/example.controller.js index 35c31c5..cce4e6c 100644 --- a/angularjs/stomp/src/stomp/app/example/example.controller.js +++ b/angularjs/stomp/src/stomp/app/example/example.controller.js @@ -45,8 +45,6 @@ id: 123456 }, null, 4); - - vm.connect = function () { vm.debugLog = ''; // use SockJS implementation instead of the browser's native implementation @@ -102,7 +100,7 @@ } function stompClientDebug(string) { - vm.debugLog = vm.debugLog + string + '\n'; + vm.debugLog = vm.debugLog + string + '\n'; } } diff --git a/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.controller.js b/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.controller.js index 8eb57f0..1f7ee75 100644 --- a/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.controller.js +++ b/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.controller.js @@ -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({ @@ -42,14 +44,12 @@ 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 () { @@ -69,18 +69,14 @@ 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() { diff --git a/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.html b/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.html index b0d92c6..fadf45e 100644 --- a/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.html +++ b/angularjs/stomp/src/stomp/app/shared-stomp/shared-stomp.html @@ -7,6 +7,8 @@
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 c36a5ea..d6f0c11 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 @@ -138,10 +138,10 @@ _connectSuccessCallback(); break; case 'connectErrorCallback': - _connectErrorCallback(data.connectError); + _connectErrorCallback(data.jsonConnectErrorAsString); break; case 'subscribeCallback': - _subscribeCallback(data.message); + _subscribeCallback(data.jsonMessageAsString); break; case 'disconnectCallback': _disconnectCallback(); diff --git a/angularjs/stomp/src/stomp/index.html b/angularjs/stomp/src/stomp/index.html index 185172d..fdd6587 100644 --- a/angularjs/stomp/src/stomp/index.html +++ b/angularjs/stomp/src/stomp/index.html @@ -55,12 +55,12 @@ - - + + diff --git a/angularjs/stomp/src/stomp/workers/shared.js b/angularjs/stomp/src/stomp/workers/shared.js index d8b07d5..a5c96b0 100644 --- a/angularjs/stomp/src/stomp/workers/shared.js +++ b/angularjs/stomp/src/stomp/workers/shared.js @@ -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'); -- 2.1.4