!function(a){"object"==typeof module&&"object"==typeof module.exports?module.exports=a(jQuery):a(jQuery)}(function(a){var b=function(b){if(this._opt=null,!this._isNotEmpty(b,"url"))throw new Error("Missing argument, example usage: $.simpleWebSocket({ url: 'ws://127.0.0.1:3000' }); ");this._opt=b,this._ws=null,this._reConnectTries=60,this._reConnectDeferred=null,this._dataType=this._prop(this._opt,"dataType","json"),this._listeners=[];var c=this;return this._api=function(){return{connect:function(){return a.extend(c._api,c._reConnect.apply(c,[]))},isConnected:function(a){return a?(a.apply(this,[c._isConnected.apply(c,[])]),c._api):c._isConnected.apply(c,[])},send:function(b){return a.extend(c._api,c._send.apply(c,[b]))},listen:function(b){return a.extend(c._api,c._listenReconnect.apply(c,[b]))},remove:function(a){return c._remove.apply(c,[a]),c._api},removeAll:function(){return c._removeAll.apply(c,[]),c._api},close:function(){return c._close.apply(c,[]),c._reset.apply(c,[]),c._api}}}(),this._api};return b.prototype={_createWebSocket:function(a){var b;if(!(b=a.protocols?void 0!==window.MozWebSocket?new MozWebSocket(a.url,a.protocols):window.WebSocket?new WebSocket(a.url,a.protocols):null:void 0!==window.MozWebSocket?new MozWebSocket(a.url):window.WebSocket?new WebSocket(a.url):null))throw new Error("Error, websocket could not be initialized.");return b},_bindSocketEvents:function(b,c){var d=this;a(b).bind("open",c.open).bind("close",c.close).bind("message",function(a){try{if(d._dataType&&"json"===d._dataType.toLowerCase()){var b=JSON.parse(a.originalEvent.data);c[a.type].call(this,b)}else if(d._dataType&&"xml"===d._dataType.toLowerCase()){var e=new DOMParser,f=e.parseFromString(a.originalEvent.data,"text/xml");c[a.type].call(this,f)}else c[a.type]&&c[a.type].call(this,a.originalEvent.data)}catch(b){c[a.type]&&c[a.type].call(this,a.originalEvent.data)}}).bind("error",function(a){c.error&&c.error.call(this,a)})},_webSocket:function(a){var b=this._createWebSocket(a);return this._bindSocketEvents(b,a),b},_getSocketEventHandler:function(a){var b=this;return{open:function(b){var c=this;a&&a.resolve(c)},close:function(b){a&&a.rejectWith(b)},message:function(a){for(var c=0,d=b._listeners.length;c0?window.setTimeout(function(){a._reConnect.apply(a,[])},a._prop.apply(a,[a._opt,"timeout",1e4])):a._reConnectDeferred.rejectWith.apply(a,[b])})},_reConnect:function(){var a=this;return this._reConnectDeferred&&"pending"===this._reConnectDeferred.state()||this._reset(),this._ws&&1===this._ws.readyState?this._reConnectDeferred.resolve(this._ws):this._reConnectTry(),a._reConnectDeferred.promise.apply(a,[])},_preparePayload:function(a){return this._opt.dataType&&"text"===this._opt.dataType.toLowerCase()?a:this._opt.dataType&&"xml"===this._opt.dataType.toLowerCase()?a:(this._opt.dataType&&this._opt.dataType.toLowerCase(),JSON.stringify(a))},_send:function(b){var c=this,d=a.Deferred();return function(a){c._reConnect.apply(c,[]).done(function(b){b.send(a),d.resolve.apply(c,[c._api])}).fail(function(a){d.rejectWith.apply(c,[a])})}(this._preparePayload(b)),d.promise()},_indexOfListener:function(a){for(var b=0,c=this._listeners.length;b