-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstompie.js
131 lines (120 loc) · 3.7 KB
/
stompie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Stompie 0.0.5
*
* Angular module for managing connection and subscribing to STOMP queues.
*
* @author mrolafsson
*/
angular.module('stompie', [])
.factory('$stompie', ['$rootScope', '$timeout', function ($rootScope, $timeout) {
'use strict';
var _stompie = {},
_endpoint = null,
_init_callbacks = [],
_socket = {};
/**
* Creates a connection to the server.
*
* @private
*/
var _init = function () {
if (_endpoint !== null) {
_socket.client = new SockJS(_endpoint);
_socket.stomp = Stomp.over(_socket.client);
_socket.stomp.connect({}, _ready, _reconnect);
}
};
/**
* Periodically attempts reconnect if the connection is closed.
*
* @private
*/
var _reconnect = function () {
$timeout(function () {
_init();
}, 3000);
};
/**
* Invoke all initialisation callbacks provided once the server connects.
*
* @private
*/
var _ready = function () {
$rootScope.$apply(function () {
for (var i = 0; i < _init_callbacks.length; i++) {
_init_callbacks[i]();
}
});
};
/**
* Initiate a new connection to a STOMP server. You should be using the using() method (pun intended).
*
* @param endpoint
* @param callback
* @private
*/
_stompie._connect = function (endpoint, callback) {
_endpoint = endpoint;
_init();
_init_callbacks.push(callback);
};
/**
* Use and create a connection if one is not already present.
* TODO At present it will only allow connections to a single endpoint.
*
* @param endpoint
* @param callback
*/
_stompie.using = function (endpoint, callback) {
if (_endpoint === null || endpoint != _endpoint) {
_stompie._connect(endpoint, callback);
} else {
_init_callbacks.push(callback);
}
};
/**
* Disconnect the socket, obviously terminating all subscriptions.
*
* @param callback
*/
_stompie.disconnect = function (callback) {
_socket.stomp.disconnect(callback);
};
/**
* Subscribe to a given channel with the callback provided.
*
* @param channel
* @param callback
* @returns subscription with which you can unsubscribe.
*/
_stompie.subscribe = function (channel, callback) {
return _socket.stomp.subscribe(channel, function (data) {
var payload = null;
try {
payload = JSON.parse(data.body);
} finally {
$rootScope.$digest(callback(payload));
}
});
};
/**
* If application prefixes are set on the STOMP server you need to specify that in the queue parameter.
*
* @param queue
* @param obj
* @param priority
* @returns {_stompie}
*/
_stompie.send = function (queue, obj, priority) {
try {
var json = JSON.stringify(obj);
_socket.stomp.send(queue, {
priority: (priority !== undefined ? priority : 9)
}, json);
} catch (e) {
throw e;
}
return this;
};
return _stompie;
}]);