From e2766ff9bd770791154d07813efa93162aacd998 Mon Sep 17 00:00:00 2001 From: gustavo Date: Mon, 25 Apr 2016 02:25:47 +0200 Subject: [PATCH] stomp: improvements in client --- .../src/stomp/app/example/example.controller.js | 37 ++++++++++++++++++---- angularjs/stomp/src/stomp/app/example/example.html | 9 +++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/angularjs/stomp/src/stomp/app/example/example.controller.js b/angularjs/stomp/src/stomp/app/example/example.controller.js index cdcfce8..4f7cb99 100644 --- a/angularjs/stomp/src/stomp/app/example/example.controller.js +++ b/angularjs/stomp/src/stomp/app/example/example.controller.js @@ -26,16 +26,23 @@ var vm = this; var client; + var subscription; - // vm.connectHeaders = {}; - // vm.clientDestination = {}; // vm.serverDestination = {}; // vm.payload = {}; // vm.headers = {}; vm.connect = function () { // use SockJS implementation instead of the browser's native implementation - var ws = new SockJS(vm.url); + var options = { + debug: true, + devel: true, + //jscs:disable + protocols_whitelist: ['websocket', 'xdr-streaming', 'xhr-streaming'], + //jscs:enable + transports: ['websocket', 'xdr-streaming', 'xhr-streaming'] + }; + var ws = new SockJS(vm.url, undefined, options); client = Stomp.over(ws); client.heartbeat.outgoing = 20000; // client will send heartbeats every 20000ms client.heartbeat.incoming = 0; // client does not want to receive heartbeats from the server @@ -43,25 +50,43 @@ }; vm.subscribe = function () { + subscription = client.subscribe(vm.clientDestination, subscribeCallback); + }; + + vm.unsubscribe = function () { + subscription.unsubscribe(); }; vm.send = function () { + client.send(vm.serverDestination, vm.headers, vm.payload); }; vm.disconnect = function() { - client.disconnect(function() { - alert('See you next time!'); - }); + client.disconnect(disconnectCallback); }; function connectCallback() { // called back after the client is connected and authenticated to the STOMP server + alert('got connection'); } function errorCallback(error) { // display the error's message header: alert(error.headers.message); } + + function subscribeCallback(message) { + // 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'); + } + } + + function disconnectCallback() { + alert('See you next time!'); + } } })(); diff --git a/angularjs/stomp/src/stomp/app/example/example.html b/angularjs/stomp/src/stomp/app/example/example.html index b6797fd..752c43e 100644 --- a/angularjs/stomp/src/stomp/app/example/example.html +++ b/angularjs/stomp/src/stomp/app/example/example.html @@ -41,7 +41,14 @@
- 4. Send
+ 4. Unsubscribe
+ +
+ +
+ +
+ 5. Send

-- 2.1.4