2 * This js establishes a websockets connection with the API Server.
5 /* Subscribe to websockets event log. Do nothing if already connected. */
6 function subscribeToEventLog () {
7 // if websockets are not supported by browser, do not subscribe for events
8 websocketsSupported = ('WebSocket' in window);
9 if (websocketsSupported == false) {
13 // check if websocket connection is already stored on the window
14 event_log_disp = $(window).data("arv-websocket");
15 if (event_log_disp == null) {
16 // need to create new websocket and event log dispatcher
17 websocket_url = $('meta[name=arv-websocket-url]').attr("content");
18 if (websocket_url == null)
21 event_log_disp = new WebSocket(websocket_url);
23 event_log_disp.onopen = onEventLogDispatcherOpen;
24 event_log_disp.onmessage = onEventLogDispatcherMessage;
26 // store websocket in window to allow reuse when multiple divs subscribe for events
27 $(window).data("arv-websocket", event_log_disp);
31 /* Send subscribe message to the websockets server. Without any filters
32 arguments, this subscribes to all events */
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;
46 // if there are any listeners for this object uuid or "all", trigger the event
47 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\"],.arv-log-event-listener[data-object-kind=\"" + parsedData.object_kind + "\"]";
48 $(matches).trigger('arv-log-event', parsedData);
51 /* Automatically connect if there are any elements on the page that want to
52 receive event log events. */
53 $(document).on('ajax:complete ready', function() {
54 var a = $('.arv-log-event-listener');
56 subscribeToEventLog();