This section contains a detailed code example for generating a token and establishing a websocket with a connector, then a complete subscription followed by a complete unsubscription example. The arbitrary chosen connector is the “TIA_Connector”
Token Generation Example
var token = null;
$.ajax("http://localhost:8080/genToken/client/1.0", {
method: 'GET',
headers: {'Authorization': 'Basic YWRtaW46YWRtaW4=' // admin : admin},
success: function(data) {
if (!data.result) {
alert("Error retrieving token: " + data.message);
return;
}
token = data.result;
}});
Attention Using the admin as user might be a security risk. In productive systems the admin should be changed into an authorized user with the respective permissions. |
Websocket Connection Example
var webSocket = null
;
webSocket = new WebSocket("ws://localhost:8080/client/1.0/connectorAsync/" + "TIA_Connector" + "?SimplifierToken=" + token)
Complete Subscription Example
var requestSubscribeData = {
"operation": "MONITORING_SUBSCRIBE",
"nodes": ["myNode","myOtherNode"],
"namespaceIndices": [2,2],
"publishingInterval":1000.0,
"clientHandlingID": 1,
"samplingInterval": 1000.0,
"queueSize": 2,
"discardOldestItem": true,
"monitoringMode": "Reporting",
"returnedTimestamps": "Both"
}
var requestSubscribe = null;
request = {
"frameType": "unsubscribe",
"subscriptionKey":"bb827118-f1b0-2170-9937-f8c7e1620107"
};
var requestSubscribe = null;
requestSubscribe = {
"frameType": "subscribe",
"subscriptionKey":"bb827118-f1b0-2170-9937-f8c7e1620107",
"json": requestData
};
webSocket.send(requestSubscribe);
Complete Subscription Example
var requestUnsubscribe = null;
requestUnsubscribe = {
"frameType": "unsubscribe",
"subscriptionKey":"bb827118-f1b0-2170-9937-f8c7e1620107"
};
webSocket.send(requestUnsubscribe);