-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstomp-websocket-multisubscriber.js
171 lines (148 loc) · 4.3 KB
/
stomp-websocket-multisubscriber.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* stomp-websocket-multi-subscriber
* https://github.com/euledge/stomp-websocket-multi-subscriber
*
* author Hitoshi Kuroyanagi (@euledge)
* copyright (c) 2016 Hitoshi Kuroyanagi
*
* require
* - https://github.com/sockjs/sockjs-client
* - https://github.com/jmesnil/stomp-websocket
*
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
function StompWebsocket() {
this.stompClient
this.connectHandler
this.disconnectHandler
this.closeHandler
this.errorHandler
this.debug = false
this.subscribers = [];
}
// register function on connect
StompWebsocket.prototype.setConnectHandler = function(func) {
this.connectHandler = func
}
// unregister function on connect
StompWebsocket.prototype.removeConnectHandler = function() {
delete this.connectHandler
}
// register function on disconnect
StompWebsocket.prototype.setDisconnectHandler = function(func) {
this.disconnectHandler = func
}
//register function on close
StompWebsocket.prototype.setCloseHandler = function(func) {
this.closeHandler = func
}
//unregister function on close
StompWebsocket.prototype.removeCloseHandler = function() {
delete this.closeHandler
}
//register function on error
StompWebsocket.prototype.setErrorHandler = function(func) {
this.errorHandler = func
}
//unregister function on error
StompWebsocket.prototype.removeErrorHandler = function() {
delete this.errorHandler
}
// unregister function on disconnect
StompWebsocket.prototype.removeDisconnectHandler = function() {
delete this.disconnectHandler
}
// Disable debug message
StompWebsocket.prototype.setDebugOn = function() {
this.debug = true
}
StompWebsocket.prototype.connect = function(endpoint, topic) {
const socket = new SockJS(endpoint);
this.stompClient = Stomp.over(socket);
if (!this.debug) {
this.stompClient.debug = function(func) {}
}
this.stompClient.connect({}, (function(frame) {
if (this.connectHandler) {
this.connectHandler()
}
this.stompClient.subscribe(topic, this.trigger.bind(this));
}).bind(this));
socket.onerror = function(e) {
if (stomp.errorHandler) {
stomp.errorHandler(e)
}
console.error(e);
}
const stomp = this;
socket.onclose = function() {
if (stomp.closeHandler) {
stomp.closeHandler()
}
}
}
StompWebsocket.prototype.disconnect = function() {
if (this.stompClient != null) {
this.stompClient.disconnect();
}
if (this.disconnectHandler) {
this.disconnectHandler()
}
}
StompWebsocket.prototype.send = function(endpoint, data) {
this.stompClient.send(endpoint, {}, data);
}
// name use key on removeSubscriber, sleepSubscriber, activeSubscriber
StompWebsocket.prototype.addSubscriber = function(name, func) {
var obj = {
name: name,
active: true,
func: func
}
this.subscribers.push(obj)
}
StompWebsocket.prototype.removeSubscriber = function(name) {
for (var i = 0; i < this.subscribers.length; i++) {
var subscriber = this.subscribers[i];
if (subscriber['name'] === name) {
if (this.debug) {
console.log("removeSubscriber " + name);
}
this.subscribers.splice(i, 1);
}
}
}
StompWebsocket.prototype.sleepSubscriber = function(name) {
var l = this.subscribers.length;
for (var i = 0; i < l; i++) {
var subscriber = this.subscribers[i];
if (subscriber['name'] === name) {
if (this.debug) {
console.log("sleepSubscriber " + name);
}
subscriber['active'] = false;
}
}
}
StompWebsocket.prototype.activeSubscriber = function(name) {
var l = this.subscribers.length;
for (var i = 0; i < l; i++) {
var subscriber = this.subscribers[i];
if (subscriber['name'] === name) {
if (this.debug) {
console.log("activeSubscriber " + name);
}
subscriber['active'] = true;
}
}
}
// execute subscriber with active state
StompWebsocket.prototype.trigger = function(event) {
for (var i = 0; i < this.subscribers.length; i++) {
if (this.subscribers[i]['active'] && event.headers.destination === this.subscribers[i]['name']) {
var subscriber = this.subscribers[i]['func'];
subscriber(event.body);
}
}
}