-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicSubscriber.js
More file actions
188 lines (175 loc) · 7.83 KB
/
TopicSubscriber.js
File metadata and controls
188 lines (175 loc) · 7.83 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* Solace Systems Web Messaging API for JavaScript
* PublishSubscribe tutorial - Topic Subscriber
* Demonstrates subscribing to a topic for direct messages and receiving messages
*/
/*jslint es6 browser devel:true*/
/*global solace*/
var TopicSubscriber = function (topicName) {
"use strict";
var subscriber = {};
subscriber.session = null;
subscriber.topicName = topicName;
subscriber.subscribed = false;
// Logger
subscriber.log = function (line) {
var now = new Date();
var time = [('0' + now.getHours()).slice(-2), ('0' + now.getMinutes()).slice(-2), ('0' + now.getSeconds()).slice(-2)];
var timestamp = '[' + time.join(':') + '] ';
console.log(timestamp + line);
var logTextArea = document.getElementById('log');
logTextArea.value += timestamp + line + '\n';
logTextArea.scrollTop = logTextArea.scrollHeight;
};
subscriber.log('\n*** Subscriber to topic "' + subscriber.topicName + '" is ready to connect ***');
// Callback for message events
subscriber.messageEventCb = function (session, message) {
subscriber.log('Received message: "' + message.getBinaryAttachment() + '"');
};
// Callback for session events
subscriber.sessionEventCb = function (session, event) {
subscriber.log(event.toString());
if (event.sessionEventCode === solace.SessionEventCode.UP_NOTICE) {
subscriber.log('=== Successfully connected and ready to subscribe. ===');
} else if (event.sessionEventCode === solace.SessionEventCode.CONNECTING) {
subscriber.log('Connecting...');
subscriber.subscribed = false;
} else if (event.sessionEventCode === solace.SessionEventCode.DISCONNECTED) {
subscriber.log('Disconnected.');
subscriber.subscribed = false;
if (subscriber.session !== null) {
subscriber.session.dispose();
subscriber.session = null;
}
} else if (event.sessionEventCode === solace.SessionEventCode.SUBSCRIPTION_ERROR) {
subscriber.log('Cannot subscribe to topic: ' + event.correlationKey);
} else if (event.sessionEventCode === solace.SessionEventCode.SUBSCRIPTION_OK) {
if (subscriber.subscribed) {
subscriber.subscribed = false;
subscriber.log('Successfully unsubscribed from topic: ' + event.correlationKey);
} else {
subscriber.subscribed = true;
subscriber.log('Successfully subscribed to topic: ' + event.correlationKey);
subscriber.log('=== Ready to receive messages. ===');
}
}
};
// Establishes connection to Solace router
subscriber.connect = function () {
if (subscriber.session !== null) {
subscriber.log('Already connected and ready to subscribe.');
} else {
var host = document.getElementById('host').value;
if (host) {
subscriber.connectToSolace(host);
} else {
subscriber.log('Cannot connect: please specify the Solace router web transport URL.');
}
}
};
subscriber.connectToSolace = function (host) {
subscriber.log('Connecting to Solace router web transport URL ' + host + '.');
var sessionProperties = new solace.SessionProperties();
sessionProperties.url = 'ws://' + host;
// NOTICE: the Solace router VPN name
sessionProperties.vpnName = 'hackathon';
subscriber.log('Solace router VPN name: ' + sessionProperties.vpnName);
// NOTICE: the client username
sessionProperties.userName = 'hackathon';
subscriber.log('Client username: ' + sessionProperties.userName);
subscriber.session = solace.SolclientFactory.createSession(
sessionProperties,
new solace.MessageRxCBInfo(function (session, message) {
// calling callback for message events
subscriber.messageEventCb(session, message);
}, subscriber),
new solace.SessionEventCBInfo(function (session, event) {
// calling callback for session events
subscriber.sessionEventCb(session, event);
}, subscriber)
);
try {
subscriber.session.connect();
} catch (error) {
subscriber.log(error.toString());
}
};
// Gracefully disconnects from Solace router
subscriber.disconnect = function () {
subscriber.log('Disconnecting from Solace router...');
if (subscriber.session !== null) {
try {
subscriber.session.disconnect();
subscriber.session.dispose();
subscriber.session = null;
} catch (error) {
subscriber.log(error.toString());
}
} else {
subscriber.log('Not connected to Solace router.');
}
};
// Subscribes to topic on Solace Router
subscriber.subscribe = function () {
if (subscriber.session !== null) {
if (subscriber.subscribed) {
subscriber.log('Already subscribed to "' + subscriber.topicName + '" and ready to receive messages.');
} else {
subscriber.log('Subscribing to topic: ' + subscriber.topicName);
try {
subscriber.session.subscribe(
solace.SolclientFactory.createTopic(subscriber.topicName),
true, // generate confirmation when subscription is added successfully
subscriber.topicName, // use topic name as correlation key
10000 // 10 seconds timeout for this operation
);
} catch (error) {
subscriber.log(error.toString());
}
}
} else {
subscriber.log('Cannot subscribe because not connected to Solace router.');
}
};
// Unsubscribes from topic on Solace Router
subscriber.unsubscribe = function () {
if (subscriber.session !== null) {
if (subscriber.subscribed) {
subscriber.log('Unsubscribing from topic: ' + subscriber.topicName);
try {
subscriber.session.unsubscribe(
solace.SolclientFactory.createTopic(subscriber.topicName),
true, // generate confirmation when subscription is removed successfully
subscriber.topicName, // use topic name as correlation key
10000 // 10 seconds timeout for this operation
);
} catch (error) {
subscriber.log(error.toString());
}
} else {
subscriber.log('Cannot unsubscribe because not subscribed to the topic "' + subscriber.topicName + '"');
}
} else {
subscriber.log('Cannot unsubscribe because not connected to Solace router.');
}
};
return subscriber;
};