-
Notifications
You must be signed in to change notification settings - Fork 0
/
messaging.js
111 lines (99 loc) · 4.06 KB
/
messaging.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
import appConfig from "./config.js"
// Initialize factory with the most recent API defaults
var factoryProps = new solace.SolclientFactoryProperties();
factoryProps.profile = solace.SolclientFactoryProfiles.version10;
solace.SolclientFactory.init(factoryProps);
// enable logging to JavaScript console at WARN level
// NOTICE: works only with "solclientjs-debug.js"
solace.SolclientFactory.setLogLevel(appConfig.solace.LogLevel);
let session // message session
let onMessage // callback
var msgController = {
clientName: "",
connect: function (onConnected, _onMessage) {
onMessage = _onMessage
log.info('Connecting to Solace PubSub+ Event Broker using url: ' + appConfig.solace.SessionProperties.url);
log.info('Client username: ' + appConfig.solace.SessionProperties.userName);
log.info('Solace PubSub+ Event Broker VPN name: ' + appConfig.solace.SessionProperties.vpnName);
// create session
try {
session = solace.SolclientFactory.createSession(appConfig.solace.SessionProperties);
} catch (err) {
log.error(err.toString());
}
// define session event listeners
const that = this
session.on(solace.SessionEventCode.UP_NOTICE, function (sessionEvent) {
log.info('=== Successfully connected and ready to subscribe. ===');
msgController.clientName = session.getSessionProperties().clientName
setTimeout(() => { onConnected() }, 0);
});
session.on(solace.SessionEventCode.CONNECT_FAILED_ERROR, function (sessionEvent) {
log.error('Connection failed to the message router: ' + sessionEvent.infoStr +
' - check correct parameter values and connectivity!');
});
session.on(solace.SessionEventCode.DISCONNECTED, function (sessionEvent) {
log.info('Disconnected.');
if (session !== null) {
session.dispose();
session = null;
}
});
session.on(solace.SessionEventCode.SUBSCRIPTION_ERROR, function (sessionEvent) {
log.warn('Cannot subscribe to topic: ' + sessionEvent.correlationKey);
});
// define message event listener
session.on(solace.SessionEventCode.MESSAGE, function (message) {
let msg = {}
msg.topic = message.getDestination().name
msg.payload = JSON.parse(msgController.getTextPayload(message))
onMessage(msg)
});
// actually connect to the broker
session.connect()
},
subscribeTo: function (topicName) {
try {
log.info('Subscribe to topic: ' + topicName);
session.subscribe(
solace.SolclientFactory.createTopicDestination(topicName),
true, // generate confirmation when subscription is added successfully
topicName, // use topic name as correlation key
10000 // 10 seconds timeout for this operation
);
} catch (error) {
log.error(error.toString());
}
},
unSubscribe: function (topicName) {
try {
log.info('Un-Subscribe topic: ' + topicName);
session.unsubscribe(
solace.SolclientFactory.createTopicDestination(topicName),
true, // generate confirmation when subscription is added successfully
topicName, // use topic name as correlation key
10000 // 10 seconds timeout for this operation
);
} catch (error) {
log.error(error.toString());
}
},
getTextPayload: function (message) {
if (message.getType() == solace.MessageType.TEXT) {
return message.getSdtContainer().getValue();
} else {
return message.getBinaryAttachment(); // binary attachment, all text
}
},
sendRequest(topic, payload, onFilteringMessage) {
let message = solace.SolclientFactory.createMessage();
message.setDestination(solace.SolclientFactory.createTopicDestination(topic));
message.setBinaryAttachment(payload);
message.setDeliveryMode(solace.MessageDeliveryModeType.DIRECT);
session.sendRequest(message, 10 * 1000, (session, replyMsg, userObject) => {
const replyPayload = this.getTextPayload(replyMsg)
onFilteringMessage(replyPayload)
}, (session, error, userObject) => { log.error(`sendRequest error: ${error}`) })
}
}
export { msgController as default }