client/app/app.js
changeset 6 c53e9b24f93f
parent 5 90a7c431b979
child 7 813fec862542
--- a/client/app/app.js	Thu Oct 09 14:34:14 2014 +0200
+++ b/client/app/app.js	Thu Oct 09 15:22:50 2014 +0200
@@ -29,7 +29,6 @@
             
             dataModel.data.$promise.then(function(data) {
                 if(typeof data.categories!=='undefined' && data.categories.length>0){
-                    console.log('    JE RENTRE');
                     var cats = [];
                     var nbCat = data.categories.length;
                     for(var i=0;i<nbCat;i++){
@@ -55,10 +54,11 @@
             var ellog = null;
             
             ellog = document.getElementById('log');
-            
             function log(m) {
-                ellog.innerHTML += m + '\n';
-                ellog.scrollTop = ellog.scrollHeight;
+                if(ellog){
+                    ellog.innerHTML += m + '\n';
+                    ellog.scrollTop = ellog.scrollHeight;
+                }
             }
 
             var wsuri;
@@ -74,22 +74,32 @@
             } else if ('MozWebSocket' in window) {
                 sock = new window.MozWebSocket(wsuri);
             } else {
-                log('Browser does not support WebSocket!');
+                if(context.logging===true){
+                    log('Browser does not support WebSocket!');
+                }
                 window.location = 'http://autobahn.ws/unsupportedbrowser';
             }
-            console.log('hi 1', sock);
+            console.log('socket in controller = ', sock);
             if (sock) {
                 sock.onopen = function() {
-                    log('Connected to ' + wsuri);
+                    if(context.logging===true){
+                        log('Connected to ' + wsuri);
+                    }
                 };
 
                 sock.onclose = function(e) {
-                    log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = "' + e.reason + '")');
+                    if(context.logging===true){
+                        log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')');
+                    }
+                    alert('Communication interrompue : la socket vient de se fermer.');
                     sock = null;
                 };
 
                 sock.onmessage = function(e) {
-                    log('Got message: ' + e.data);
+                    if(context.logging===true){
+                        log('Got message: ' + e.data);
+                    }
+                    alert('Annotation bien reçue.');
                 };
             }
 
@@ -106,14 +116,20 @@
                 if (sock) {
                     var new_annot = {
                             categories : eventCode,
-                            user : 'admin'
+                            user : $scope.username
                     };
                     sock.send(JSON.stringify(new_annot));
-                    log('Sent: ' + JSON.stringify(new_annot));
+                    if(context.logging===true){
+                        log('Sent: ' + JSON.stringify(new_annot));
+                    }
                 } else {
-                    log('Not connected.');
+                    if(context.logging===true){
+                        alert('La socket ne fonctionne pas.');
+                        if(context.logging===true){
+                            log('Not connected.');
+                        }
+                    }
                 }
-                //alert('annotation envoyée ! eventCode = ' + eventCode);
             };
             
             // Interface management