Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit 60a9c17

Browse files
committed
Progress: #13
1 parent 8e54309 commit 60a9c17

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

docs/samples/javascript/index.html

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
headers["Authorization"] = "Basic " + btoa("tango-cs:tango");
2626
});
2727
</script>
28+
<!-- Below you will find two scripts that demonstrates Tango REST subscriptions API usage-->
29+
2830
<!-- In this example client creates a subscription and opens an event-stream -->
2931
<script type="application/javascript">
3032
/**
@@ -115,6 +117,132 @@
115117
});
116118

117119
</script>
120+
<!-- More complex sample with tow dynamically added tango events-->
121+
<script type="application/javascript">
122+
/**
123+
* Tango REST entry point
124+
*
125+
* @type {string}
126+
* @const
127+
*/
128+
const kRestURL = "http://ec2-35-156-104-8.eu-central-1.compute.amazonaws.com:8080/tango/rest/rc4";
129+
130+
/**
131+
* Create a subscription using POST request
132+
*
133+
* @type {Promise}
134+
*/
135+
var subscriptionPromise = webix.ajax().post(kRestURL + "/subscriptions")
136+
/**
137+
* webix returns xhr wrapper that is used to extract JSON data
138+
*/
139+
.then(value => value.json())
140+
/**
141+
* now open subscription's event-stream
142+
*/
143+
.then(subscription => {
144+
/**
145+
* Create new EventSource. Due to CORS we need set withCredentials to true
146+
*
147+
* @type {EventSource}
148+
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource
149+
*/
150+
var source = new EventSource(kRestURL + "/subscriptions/" + subscription.id + "/event-stream", {
151+
withCredentials: true
152+
});
153+
/**
154+
* EventSource.onerror handler. This one is invoked when event stream receives event with type=error
155+
* or server is down
156+
*
157+
* @param event
158+
*/
159+
source.onerror = function (event) {
160+
this.close();
161+
};
162+
163+
return webix.extend(subscription, {
164+
source : source
165+
})
166+
})
167+
/**
168+
* POST request failure handler
169+
*/
170+
.fail(() => {
171+
document.getElementById("error").innerHTML = "Server does not respond";
172+
});
173+
/**
174+
* PUT a Tango event into newly created subscription
175+
*/
176+
subscriptionPromise.then(subscription => {
177+
/**
178+
* define request content type
179+
*/
180+
return webix.ajax().headers({
181+
"Content-Type": "application/json"
182+
})
183+
/**
184+
* subscribe to tango://ip-172-31-30-179:10000/sys/tg_test/1/double_scalar/change event
185+
*/
186+
.put(kRestURL + "/subscriptions/" + subscription.id, JSON.stringify([
187+
{
188+
host: "ip-172-31-30-179:10000",
189+
device: "sys/tg_test/1",
190+
attribute: "double_scalar",
191+
type: "change"
192+
}
193+
]))
194+
/**
195+
* extract JSON preserving source reference
196+
*/
197+
.then(value => webix.extend(subscription, value.json(), true))
198+
.then(subscription => {
199+
/**
200+
* add event listener for each event in our subscription. We have only one buy now.
201+
*/
202+
subscription.events.forEach(event => {
203+
subscription.source.addEventListener(event.id, function (event) {
204+
/**
205+
* event.type -- same as subscription.events[0].id field
206+
* event.data -- data sent by upstream Tango server or error cause e.g. error: ...
207+
* event.lastEventId -- event's timestamp
208+
*/
209+
document.getElementById(event.type).innerHTML = event.data + "@" + event.lastEventId;
210+
});
211+
});
212+
})
213+
});
214+
215+
/**
216+
* Subscribe to another event
217+
*/
218+
subscriptionPromise.then(subscription => {
219+
return webix.ajax().headers({
220+
"Content-Type": "application/json"
221+
})
222+
/**
223+
* subscribe to tango://ip-172-31-30-179:10000/sys/tg_test/1/long_scalar/change event
224+
*/
225+
.put(kRestURL + "/subscriptions/" + subscription.id, JSON.stringify([
226+
{
227+
host: "ip-172-31-30-179:10000",
228+
device: "sys/tg_test/1",
229+
attribute: "long_scalar",
230+
type: "change"
231+
}
232+
]))
233+
/**
234+
* extract JSON preserving source reference
235+
*/
236+
.then(value => webix.extend(subscription, value.json(), true))
237+
})
238+
.then(subscription => {
239+
let eventId = subscription.events.filter(event => event.attribute === 'long_scalar')[0].id;
240+
241+
subscription.source.addEventListener(eventId, function(event){
242+
document.getElementById(event.type).innerHTML = event.data + "@" + event.lastEventId;
243+
});
244+
})
245+
</script>
118246
</body>
119247

120248
</html>

0 commit comments

Comments
 (0)