spec/javascripts/generated/assets/websocket_connection.js in websocket-rails-0.6.2 vs spec/javascripts/generated/assets/websocket_connection.js in websocket-rails-0.7.0

- old
+ new

@@ -1,74 +1,59 @@ + /* WebSocket Interface for the WebSocketRails client. -*/ + */ - (function() { - var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; - WebSocketRails.WebSocketConnection = (function() { + WebSocketRails.WebSocketConnection = (function(_super) { + __extends(WebSocketConnection, _super); + + WebSocketConnection.prototype.connection_type = 'websocket'; + function WebSocketConnection(url, dispatcher) { this.url = url; this.dispatcher = dispatcher; - this.flush_queue = __bind(this.flush_queue, this); - this.on_error = __bind(this.on_error, this); - this.on_close = __bind(this.on_close, this); - this.on_message = __bind(this.on_message, this); - this.trigger = __bind(this.trigger, this); + WebSocketConnection.__super__.constructor.apply(this, arguments); if (this.url.match(/^wss?:\/\//)) { console.log("WARNING: Using connection urls with protocol specified is depricated"); - } else if (window.location.protocol === 'http:') { - this.url = "ws://" + this.url; - } else { + } else if (window.location.protocol === 'https:') { this.url = "wss://" + this.url; + } else { + this.url = "ws://" + this.url; } - this.message_queue = []; this._conn = new WebSocket(this.url); - this._conn.onmessage = this.on_message; - this._conn.onclose = this.on_close; - this._conn.onerror = this.on_error; + this._conn.onmessage = (function(_this) { + return function(event) { + var event_data; + event_data = JSON.parse(event.data); + return _this.on_message(event_data); + }; + })(this); + this._conn.onclose = (function(_this) { + return function(event) { + return _this.on_close(event); + }; + })(this); + this._conn.onerror = (function(_this) { + return function(event) { + return _this.on_error(event); + }; + })(this); } - WebSocketConnection.prototype.trigger = function(event) { - if (this.dispatcher.state !== 'connected') { - return this.message_queue.push(event); - } else { - return this._conn.send(event.serialize()); - } + WebSocketConnection.prototype.close = function() { + return this._conn.close(); }; - WebSocketConnection.prototype.on_message = function(event) { - var data; - data = JSON.parse(event.data); - return this.dispatcher.new_message(data); + WebSocketConnection.prototype.send_event = function(event) { + WebSocketConnection.__super__.send_event.apply(this, arguments); + return this._conn.send(event.serialize()); }; - WebSocketConnection.prototype.on_close = function(event) { - var close_event; - close_event = new WebSocketRails.Event(['connection_closed', event]); - this.dispatcher.state = 'disconnected'; - return this.dispatcher.dispatch(close_event); - }; - - WebSocketConnection.prototype.on_error = function(event) { - var error_event; - error_event = new WebSocketRails.Event(['connection_error', event]); - this.dispatcher.state = 'disconnected'; - return this.dispatcher.dispatch(error_event); - }; - - WebSocketConnection.prototype.flush_queue = function() { - var event, _i, _len, _ref; - _ref = this.message_queue; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - event = _ref[_i]; - this._conn.send(event.serialize()); - } - return this.message_queue = []; - }; - return WebSocketConnection; - })(); + })(WebSocketRails.AbstractConnection); }).call(this);