Skip to content
Extraits de code Groupes Projets
_websocket_js.haml 1,86 Kio
-  if user_signed_in?
  - unless request.user_agent.include? "Safari" ||"Chrome"
    = javascript_include_tag 'FABridge', 'swfobject', 'web_socket'
    :javascript
        WebSocket.__swfLocation = "/javascripts/WebSocketMain.swf";
  :javascript
    $(document).ready(function(){
      function debug(str){ $("#debug").append("<p>" +  str); };

      ws = new WebSocket("ws://#{request.host}:8080/");
      ws.onmessage = function(evt) { 
        var obj = jQuery.parseJSON(evt.data);
        debug("got a " + obj['class']);

        if (obj['class']=="retractions"){
          processRetraction(obj['post_id']);
        
        }else if (obj['class']=="comments"){
          processComment(obj['post_id'], obj['html']) 
        
        }else{
          processPost(obj['class'], obj['html'])
        }
      }; 
      ws.onclose = function() { debug("socket closed"); };
      ws.onopen = function() {
        ws.send(location.pathname);
        debug("connected...");
      };

      function processRetraction(post_id){
        $('#' + post_id ).fadeOut(500, function(){
          $(this).remove;
        });
      }

      function processComment(post_id, html){
        $('#'+ post_id + ' .comment_set li:last' ).before(
          $(html).fadeIn("fast", function(){})
        );
      }

      function processPost(className, html){
        if(onPageForClass(className)){
          $("#stream").prepend(
            $(html).fadeIn("fast", function(){
              $("#stream label:first").inFieldLabels();
            })
          ); 
        }
      }
      
      function onPageForClass(className){
        return ((location.href.indexOf(className) != -1 ) || (location.pathname == '/')) && onPageOne();
      }
      
      function onPageOne() {
          var c = document.location.search.charAt(document.location.search.length-1);
          return ((c =='') || (c== '1'));
      }
    });