├── .gitignore
├── README.md
└── angular-stomp.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | AngularStomp
2 | ============
3 |
4 | Angular service to Web-Stomp
5 |
6 | Usage
7 | -----
8 | This class relies on stomp.js that can be found at: https://github.com/jmesnil/stomp-websocket/
9 |
10 | 1. Set the socket provider for Stomp. I do this in my app config: `Stomp.WebSocketClass = SockJS;`
11 | (Here I am using SockJS: ``)
12 | 2. In your app module add 'AngularStomp'. The name of the injected service is ngstomp
13 | 3. The service is a constructor that takes the url to connect to. Here is a full example:
14 |
15 | ```js
16 | function FrontPageController($scope, $routeParams, ngstomp) {
17 | $scope.messages = [];
18 | $scope.client = ngstomp('http://localhost:15674/stomp');
19 | $scope.client.connect("guest", "guest", function(){
20 | $scope.client.subscribe("/topic/test", function(message) {
21 | $scope.messages.push(message.body);
22 | });
23 | }, function(){}, '/');
24 | ```
25 |
26 | Example HTML:
27 |
28 | ```html
29 |
30 |
31 |
Messages
32 |
{{message}}
33 |
34 |
35 | ```
36 |
37 |
--------------------------------------------------------------------------------
/angular-stomp.js:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright: 2012, V. Glenn Tarcea
3 | * MIT License Applies
4 | */
5 |
6 | angular.module('AngularStomp', []).
7 | factory('ngstomp', function($rootScope) {
8 | var stompClient = {};
9 |
10 | function NGStomp(url) {
11 | this.stompClient = Stomp.client(url);
12 | }
13 |
14 | NGStomp.prototype.subscribe = function(queue, callback) {
15 | this.stompClient.subscribe(queue, function() {
16 | var args = arguments;
17 | $rootScope.$apply(function() {
18 | callback(args[0]);
19 | })
20 | })
21 | }
22 |
23 | NGStomp.prototype.send = function(queue, headers, data) {
24 | this.stompClient.send(queue, headers, data);
25 | }
26 |
27 | NGStomp.prototype.connect = function(user, password, on_connect, on_error, vhost) {
28 | this.stompClient.connect(user, password,
29 | function(frame) {
30 | $rootScope.$apply(function() {
31 | on_connect.apply(stompClient, frame);
32 | })
33 | },
34 | function(frame) {
35 | $rootScope.$apply(function() {
36 | on_error.apply(stompClient, frame);
37 | })
38 | }, vhost);
39 | }
40 |
41 | NGStomp.prototype.disconnect = function(callback) {
42 | this.stompClient.disconnect(function() {
43 | var args = arguments;
44 | $rootScope.$apply(function() {
45 | callback.apply(args);
46 | })
47 | })
48 | }
49 |
50 | return function(url) {
51 | return new NGStomp(url);
52 | }
53 | });
54 |
--------------------------------------------------------------------------------