2 * This js establishes a websockets connection with the API Server.
5 /* The subscribe method takes a window element id and object id.
6 Any log events for that particular object id are sent to that window element. */
7 function subscribeToEventLog (elementId) {
8 // if websockets are not supported by browser, do not subscribe for events
9 websocketsSupported = ('WebSocket' in window);
10 if (websocketsSupported == false) {
14 // grab websocket connection from window, if one exists
15 event_log_disp = $(window).data("arv-websocket");
16 if (event_log_disp == null) {
17 // create the event log dispatcher
18 websocket_url = $('meta[name=arv-websocket-url]').attr("content");
19 if (websocket_url == null)
22 event_log_disp = new WebSocket(websocket_url);
24 event_log_disp.onopen = onEventLogDispatcherOpen;
25 event_log_disp.onmessage = onEventLogDispatcherMessage;
27 // store websocket in window to allow reuse when multiple divs subscribe for events
28 $(window).data("arv-websocket", event_log_disp);
32 /* send subscribe message to the websockets server */
33 function onEventLogDispatcherOpen(event) {
34 this.send('{"method":"subscribe"}');
37 /* trigger event for all applicable elements waiting for this event */
38 function onEventLogDispatcherMessage(event) {
39 parsedData = JSON.parse(event.data);
40 object_uuid = parsedData.object_uuid;
42 // if there are any listeners for this object uuid or "all", trigger the event
43 matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuid=\"all\"]";
44 $(matches).trigger('arv-log-event', event.data);