Sha256: f1bb297bb4b4ecba6e229c5a13d04a6c55c73dc7b588804fbcefc159e0cdceb4

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

var RocketIO = function(){
  new EventEmitter().apply(this);
  this.type = null; // "comet", "websocket"
  this.session = null;
  this.io = null;
  var self = this;
  var ws_close_timer = null;

  this.connect = function(){
    self.io = function(){
      if(self.type === "comet") return;
      if(typeof WebSocketIO === "undefined") return;
      var io = new WebSocketIO();
      io.session = self.session;
      return io.connect();
    }() || function(){
      if(typeof CometIO === "undefined") return;
      var io = new CometIO();
      io.session = self.session;
      return io.connect();
    }();
    if(typeof self.io === "undefined"){
      setTimeout(function(){
        self.emit("error", "WebSocketIO and CometIO are not available");
      }, 100);
      return self;
    };
    if(self.io.url.match(/^ws:\/\/.+/)) self.type = "websocket";
    else if(self.io.url.match(/cometio/)) self.type = "comet";
    else self.type = "unknown";
    self.io.on("*", function(event_name, args){
      self.emit(event_name, args);
    });
    self.io.on("connect", function(session_id){
      self.session = session_id;
    });
    ws_close_timer = setTimeout(function(){
      self.close();
      self.type = "comet";
      self.connect();
    }, 3000);
    self.once("connect", function(){
      if(ws_close_timer) clearTimeout(ws_close_timer);
      ws_close_timer = null;
    });
    return self;
  };

  this.close = function(){
    self.io.close();
  };

  this.push = function(type, data){
    self.io.push(type, data);
  };
};

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinatra-rocketio-0.1.2 lib/js/rocketio.js
sinatra-rocketio-0.1.1 lib/js/rocketio.js