Sha256: ece9f42c28961d587431b39329efba53a11801399cee1c614aa37a6fe83cd2d6
Contents?: true
Size: 1.67 KB
Versions: 1
Compression:
Stored size: 1.67 KB
Contents
var WebSocketIO = function(url, opts){ new EventEmitter().apply(this); if(typeof opts === "undefined" || opts === null) opts = {}; this.url = url || "<%= websocketio_url %>"; this.session = opts.session || null; this.websocket = null; this.connecting = false; var reconnect_timer_id = null; var running = false; var self = this; self.on("__session_id", function(session_id){ self.session = session_id; self.emit("connect", self.session); }); this.connect = function(){ if(typeof WebSocket === "undefined"){ self.emit("error", "websocket not exists in this browser"); return null; } self.running = true; var url = self.session ? self.url+"/session="+self.session : self.url; self.websocket = new WebSocket(url); self.websocket.onmessage = function(e){ try{ var data_ = JSON.parse(e.data); self.emit(data_.type, data_.data); } catch(e){ self.emit("error", "WebSocketIO data parse error"); } }; self.websocket.onclose = function(){ if(self.connecting){ self.connecting = false; self.emit("disconnect"); } if(self.running){ reconnect_timer_id = setTimeout(self.connect, 10000); } }; self.websocket.onopen = function(){ self.connecting = true; }; return self; }; this.close = function(){ clearTimeout(reconnect_timer_id); self.running = false; self.websocket.close(); }; this.push = function(type, data){ if(!self.connecting){ self.emit("error", "websocket not connected"); return; } self.websocket.send(JSON.stringify({type: type, data: data, session: self.session})); }; };
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
sinatra-websocketio-0.3.1 | lib/js/websocketio.js |