Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.

JS Monitor

In order to get this example working, insert the javascript into the script task into the template.html

Which are the events I want to see?

Multiple topic / events pairs can be sent; events holds 1..n events. Possible events (for instance 1) can be found at «http://cpee.org:9298/1/notifications/topics». 
 
var sub = 'topic'  + '=' + 'running' + '&' +
          'events' + '=' + 'activity_calling,activity_manipulating,activity_failed,activity_done';

Which instance do I want to monitor?

var url = "http://cpee.org:9298/20/";

Monitor!

First we tell the instance which events we want. 
 
$.ajax({
    type: "POST",
    url: url + "/notifications/subscriptions/",
    data: sub,
    success: function(res){
 
Upon success we extract the subscription id … 
 
     var subscription;
      res = res.unserialize();
      $.each(res,function(a,b){
        if (b[0] == 'key') {
          subscription = b[1];
        }  
      });
 
… and use it to create an url to a websocket. 
 
     var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
      var ws = new Socket(url.replace(/http/,'ws') + "/notifications/subscriptions/" + subscription + "/ws/");
      ws.onopen = function() {
        mylog("monitoring", "opened", subscription);
      };
      ws.onclose = function() {
        mylog("monitoring", "closed", "server down i assume.");
      };
 
This websocket constantly streams the events to us, we just have to interpret them. 
 
     ws.onmessage = function(e) {
        data = e.data.parseXML();
        if ($('event > topic',data).length > 0) {
          var notification = $('event > notification',data).text();
          mylog("event", $('event > topic',data).text() + "/" + $('event > event',data).text(), notification);
        }
        if ($('vote > topic',data).length > 0) {
          var notification = $('vote > notification',data).text();
          mylog("vote", $('vote > topic',data).text() + "/" + $('vote > vote',data).text(), notification);
        }  
      };
      
    }
});
 
Neat thing: this doesn't need to be on a server, put it into the templace and it will even work when opened locally. 
Letzte Änderung: 10.09.2014, 04:19 | 291 Worte