From: gustavo Date: Sun, 28 Aug 2016 23:00:18 +0000 (+0200) Subject: stomp shared worker improvements X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=0505b81ec27c117b9b18a533db3730b87e12b0d9;p=JavaScriptForFun stomp shared worker improvements --- 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 1f7ee75..7bc47f7 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 @@ -69,14 +69,14 @@ alert('got connection'); } - function connectErrorCallback(jsonConnectErrorAsString) { + function connectErrorCallback(error) { // display the error's message header: - alert('error call back: \n' + jsonConnectErrorAsString); + alert('error call back: \n' + JSON.stringify(error, null, 4)); } - function subscribeCallback(jsonMessageAsString) { + function subscribeCallback(message) { // called when the client receives a STOMP message from the server - alert('subscribe message: \n' + jsonMessageAsString); + alert('subscribe message: \n' + JSON.stringify(message, null, 4)); } function disconnectCallback() { 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 d6f0c11..fdc0988 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 @@ -72,7 +72,7 @@ _connectSuccessCallback = connectSuccessCallback; _connectErrorCallback = connectErrorCallback; - _messagePort.postMessage(message); + doPostMessage(message); } function subscribe(clientDestination, subscribeCallback, subscribeHeaders) { @@ -83,7 +83,7 @@ }; _subscribeCallback = subscribeCallback; - _messagePort.postMessage(message); + doPostMessage(message); } function unSubscribe() { @@ -91,7 +91,7 @@ command: 'unSubscribe' }; - _messagePort.postMessage(message); + doPostMessage(message); } function send(serverDestination, sendHeaders, payload) { @@ -102,7 +102,7 @@ payload: payload }; - _messagePort.postMessage(message); + doPostMessage(message); } function disconnect(disconnectCallback) { @@ -111,15 +111,18 @@ }; _disconnectCallback = disconnectCallback; - _messagePort.postMessage(message); + doPostMessage(message); } + function doPostMessage(message) { + _messagePort.postMessage(JSON.stringify(message, null, 4)); + } function onMessage(event) { if (event.data.error) { // Error } else { - callBack(event.data); + callBack(JSON.parse(event.data)); } } // var send { @@ -128,20 +131,19 @@ // } // var result { // command: - // connectError: + // error: // message: // }; function callBack(data) { - switch(data.command) { case 'connectSuccessCallback': _connectSuccessCallback(); break; case 'connectErrorCallback': - _connectErrorCallback(data.jsonConnectErrorAsString); + _connectErrorCallback(data.error); break; case 'subscribeCallback': - _subscribeCallback(data.jsonMessageAsString); + _subscribeCallback(data.message); break; case 'disconnectCallback': _disconnectCallback(); diff --git a/angularjs/stomp/src/stomp/index.html b/angularjs/stomp/src/stomp/index.html index fdd6587..185172d 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 a5c96b0..fba6a8a 100644 --- a/angularjs/stomp/src/stomp/workers/shared.js +++ b/angularjs/stomp/src/stomp/workers/shared.js @@ -34,12 +34,13 @@ self.onerror = function (msg, url, lineNo, columnNo, error) { }; function onMessage(event) { - callCommand(event.data); + callCommand(JSON.parse(event.data)); } function postMessage(message) { + var jsonMessageAsString = JSON.stringify(message, null, 4); messagePorts.forEach(function (messagePort) { - messagePort.postMessage(message); + messagePort.postMessage(jsonMessageAsString); }); } @@ -115,18 +116,16 @@ function connectSuccessCallback() { function connectErrorCallback(error) { isConnected = false; - var jsonConnectErrorAsString = JSON.stringify(error, null, 4); postMessage({ command: 'connectErrorCallback', - jsonConnectErrorAsString: jsonConnectErrorAsString + error: error }); } function subscribeCallback(message) { - var jsonMessageAsString = JSON.stringify(message, null, 4); postMessage({ command: 'subscribeCallback', - jsonMessageAsString: jsonMessageAsString + message: message }); }