Sha256: d9a506617b0f6e821ce41d6dae001d22c3486227d78237d8ba328e206d81981a

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 KB

Contents

var CometIO = function(){
  new EventEmitter().apply(this);
  this.url = "<%= cometio_url %>";
  this.session = null;
  var running = false;
  var self = this;

  this.push = function(type, data){
    $.ajax(
      {
        url : self.url,
        data : {type : type, data : data, session : self.session},
        success : function(data){
        },
        error : function(req, stat, e){
          self.emit("error", "CometIO push error");
        },
        complete : function(e){
        },
        type : "POST",
        dataType : "json",
        timeout : 10000
      }
    );
  };

  this.connect = function(){
    if(running) return self;
    self.on("__session_id", function(session){
      self.session = session;
      self.emit("connect", self.session);
    });
    running = true;
    self.get();
    return self;
  };

  this.close = function(){
    running = false;
    self.removeListener("__session_id");
  };

  this.get = function(){
    if(!running) return;
    $.ajax(
      {
        url : self.url,
        data : {session : self.session},
        success : function(data){
          if(data){
            self.emit(data.type, data.data);
          }
          self.get();
        },
        error : function(req, stat, e){
          self.emit("error", "CometIO get error");
          setTimeout(self.get, 10000);
        },
        complete : function(e){
        },
        type : "GET",
        dataType : "json",
        timeout : <%= (CometIO.options[:timeout]+10)*1000 %>
      }
    );
  };
};

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sinatra-cometio-0.1.9 lib/js/cometio.js
sinatra-cometio-0.1.8 lib/js/cometio.js
sinatra-cometio-0.1.7 lib/js/cometio.js