/
jquery.signalR.min.js
9 lines (9 loc) · 12.5 KB
/
jquery.signalR.min.js
1
2
3
4
5
6
7
8
9
/*!
* SignalR JavaScript Library v0.5
* http://signalr.net/
*
* Copyright David Fowler and Damian Edwards 2012
* Licensed under the MIT.
* https://github.com/SignalR/SignalR/blob/master/LICENSE.md
*/
(function(n,t){"use strict";function s(i){var r;return(i=n.trim(i),i.indexOf("http")!==0)?!1:(r=t.document.createElement("a"),r.href=i,r.protocol+r.host!==t.location.protocol+t.location.host)}var f;if(typeof n!="function")throw"SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file.";if(!t.JSON)throw"SignalR: No JSON parser found. Please ensure json2.js is referenced before the SignalR.js file if you need to support clients without native JSON parsing support, e.g. IE<8.";var u,o,r={onStart:"onStart",onStarting:"onStarting",onSending:"onSending",onReceived:"onReceived",onError:"onError",onReconnect:"onReconnect",onStateChanged:"onStateChanged",onDisconnect:"onDisconnect"},i=function(n,i){if(i!==!1){var r;typeof t.console!="undefined"&&(r="["+(new Date).toTimeString()+"] SignalR: "+n,t.console.debug?t.console.debug(r):t.console.log&&t.console.log(r))}},e=function(t,i){i!==t.state&&(n(t).trigger(r.onStateChanged,[i]),t.state=i)};u=function(n,t,i){return new u.fn.init(n,t,i)},u.connectionState={connecting:0,connected:1,reconnecting:2,disconnecting:3,disconnected:4},u.fn=u.prototype={init:function(n,t,i){this.url=n,this.qs=t,typeof i=="boolean"&&(this.logging=i)},ajaxDataType:"json",logging:!1,state:u.connectionState.disconnected,reconnectDelay:2e3,start:function(f,o){var h=this,c={transport:"auto",jsonp:!1},v,l=n.Deferred(),a=document.createElement("a");return h.state===u.connectionState.connecting||h.state===u.connectionState.connected?(l.resolve(h),l.promise()):(e(h,u.connectionState.connecting),n.type(f)==="function"?o=f:n.type(f)==="object"&&(n.extend(c,f),n.type(c.callback)==="function"&&(o=c.callback)),a.href=h.url,h.baseUrl=a.protocol===":"?t.document.location.protocol+"//"+t.document.location.host:a.protocol+"//"+a.host,s(h.url)&&(i("Auto detected cross domain url."),c.transport==="auto"&&(c.jsonp||(c.jsonp=!n.support.cors,c.jsonp&&i("Using jsonp because this browser doesn't support cors")),c.transport=c.jsonp===!0?"longPolling":["webSockets","longPolling"])),h.ajaxDataType=c.jsonp?"jsonp":"json",n(h).bind(r.onStart,function(){n.type(o)==="function"&&o.call(h),l.resolve(h)}),v=function(i,f){if(f=f||0,f>=i.length){h.transport||l.reject("SignalR: No transport could be initialized successfully. Try specifying a different transport or none at all for auto initialization.");return}var o=i[f],s=n.type(o)==="object"?o:u.transports[o];s.start(h,function(){h.transport=s,n(h).trigger(r.onStart),e(h,u.connectionState.connected),n(t).unload(function(){h.stop(!1)})},function(){v(i,f+1)})},t.setTimeout(function(){var t=h.url+"/negotiate";i("Negotiating with '"+t+"'."),n.ajax({url:t,global:!1,cache:!1,type:"GET",data:{},dataType:h.ajaxDataType,error:function(t){n(h).trigger(r.onError,[t.responseText]),l.reject("SignalR: Error during negotiation request: "+t),h.stop()},success:function(t){if(h.appRelativeUrl=t.Url,h.id=t.ConnectionId,h.webSocketServerUrl=t.WebSocketServerUrl,!t.ProtocolVersion||t.ProtocolVersion!=="1.0"){n(h).trigger(r.onError,"SignalR: Incompatible protocol version."),l.reject("SignalR: Incompatible protocol version.");return}n(h).trigger(r.onStarting);var f=[],i=[];n.each(u.transports,function(n){if(n==="webSockets"&&!t.TryWebSockets)return!0;i.push(n)}),n.isArray(c.transport)?n.each(c.transport,function(){var t=this;(n.type(t)==="object"||n.type(t)==="string"&&n.inArray(""+t,i)>=0)&&f.push(n.type(t)==="string"?""+t:t)}):n.type(c.transport)==="object"||n.inArray(c.transport,i)>=0?f.push(c.transport):f=i,v(f)}})},0),l.promise())},starting:function(t){var i=this,u=n(i);return u.bind(r.onStarting,function(){t.call(i),u.unbind(r.onStarting)}),i},send:function(n){var t=this;if(!t.transport)throw"SignalR: Connection must be started before data can be sent. Call .start() before .send()";return t.transport.send(t,n),t},sending:function(t){var i=this;return n(i).bind(r.onSending,function(){t.call(i)}),i},received:function(t){var i=this;return n(i).bind(r.onReceived,function(n,r){t.call(i,r)}),i},stateChanged:function(t){var i=this;return n(i).bind(r.onStateChanged,function(n,r){t.call(i,r)}),i},error:function(t){var i=this;return n(i).bind(r.onError,function(n,r){t.call(i,r)}),i},disconnected:function(t){var i=this;return n(i).bind(r.onDisconnect,function(){t.call(i)}),i},reconnected:function(t){var i=this;return n(i).bind(r.onReconnect,function(){t.call(i)}),i},stop:function(t){var i=this;if(i.state!==u.connectionState.disconnecting&&i.state!==u.connectionState.disconnected){try{e(i,u.connectionState.disconnecting),i.transport&&(i.transport.abort(i,t),i.transport.stop(i),i.transport=null),n(i).trigger(r.onDisconnect),delete i.messageId,delete i.groups}finally{e(i,u.connectionState.disconnected)}return i}},log:function(n){i(n,this.logging)}},u.fn.init.prototype=u.fn,f={addQs:function(i,r){return r.qs?typeof r.qs=="object"?i+"&"+n.param(r.qs):typeof r.qs=="string"?i+"&"+r.qs:i+"&"+t.escape(r.qs.toString()):i},getUrl:function(n,i,r,u){var o=i==="webSockets"?"":n.baseUrl,f=o+n.appRelativeUrl,e="transport="+i+"&connectionId="+t.escape(n.id);return n.data&&(e+="&connectionData="+t.escape(n.data)),r?(u&&(f=f+"/reconnect"),n.messageId&&(e+="&messageId="+n.messageId),n.groups&&(e+="&groups="+t.escape(JSON.stringify(n.groups)))):f=f+"/connect",f+="?"+e,f=this.addQs(f,n)},ajaxSend:function(i,u){var f=i.url+"/send?transport="+i.transport.name+"&connectionId="+t.escape(i.id);f=this.addQs(f,i),n.ajax({url:f,global:!1,type:"POST",dataType:i.ajaxDataType,data:{data:u},success:function(t){t&&n(i).trigger(r.onReceived,[t])},error:function(t,u){u!=="abort"&&(u!=="parsererror"||i.ajaxDataType!=="jsonp")&&n(i).trigger(r.onError,[t])}})},ajaxAbort:function(r,u){if(typeof r.transport!="undefined"){u=typeof u=="undefined"?!0:u;var f=r.url+"/abort?transport="+r.transport.name+"&connectionId="+t.escape(r.id);f=this.addQs(f,r),n.ajax({url:f,async:u,timeout:1e3,global:!1,type:"POST",dataType:r.ajaxDataType,data:{}}),i("Fired ajax abort async = "+u)}},processMessages:function(t,u){var f=n(t);if(u){if(u.Disconnect){i("Disconnect command received from server"),t.stop();return}u.Messages&&n.each(u.Messages,function(){try{f.trigger(r.onReceived,[this])}catch(u){i("Error raising received "+u),n(t).trigger(r.onError,[u])}}),u.MessageId&&(t.messageId=u.MessageId),u.TransportData&&(t.groups=u.TransportData.Groups)}},foreverFrame:{count:0,connections:{}}},u.transports={webSockets:{name:"webSockets",send:function(n,t){n.socket.send(t)},start:function(o,s,h){var l,v=!1,y=this,p=!s,a,c;if(t.MozWebSocket&&(t.WebSocket=t.MozWebSocket),!t.WebSocket){h();return}o.socket||(o.webSocketServerUrl?l=o.webSocketServerUrl:(c=document.location,c.protocol!=="http:"&&c.protocol!=="https:"&&(c=t.document.createElement("a"),c.href=o.url),a=c.protocol==="https:"?"wss://":"ws://",l=a+c.host),n(o).trigger(r.onSending),l+=f.getUrl(o,this.name,p),i("Connecting to websocket endpoint '"+l+"'"),o.socket=new t.WebSocket(l),o.socket.onopen=function(){v=!0,i("Websocket opened"),s?s():e(o,u.connectionState.connected)},o.socket.onclose=function(t){v?typeof t.wasClean!="undefined"&&t.wasClean===!1?(n(o).trigger(r.onError,[t.reason]),i("Unclean disconnect from websocket."+t.reason)):i("Websocket closed"):h&&h(),e(o,u.connectionState.reconnecting),y.stop(o),y.start(o)},o.socket.onmessage=function(i){var u=t.JSON.parse(i.data),e;u&&(e=n(o),u.Messages?f.processMessages(o,u):e.trigger(r.onReceived,[u]))})},stop:function(n){n.socket!==null&&(n.socket.close(),n.socket=null)},abort:function(){}},serverSentEvents:{name:"serverSentEvents",timeOut:3e3,start:function(o,s,h){var c=this,v=!1,a=n(o),l=!s,p,y;if(o.eventSource&&(i("The connection already has an event source. Stopping it."),o.stop()),!t.EventSource){h&&(i("This browser doesn't support SSE."),h());return}a.trigger(r.onSending),p=f.getUrl(o,this.name,l);try{i("Attempting to connect to SSE endpoint '"+p+"'"),o.eventSource=new t.EventSource(p)}catch(w){i("EventSource failed trying to connect with error "+w.Message),h?h():(a.trigger(r.onError,[w]),l&&(i("EventSource reconnecting"),c.reconnect(o)));return}y=t.setTimeout(function(){v===!1&&(i("EventSource timed out trying to connect"),h&&h(),l?(i("EventSource reconnecting"),c.reconnect(o)):c.stop(o))},c.timeOut),o.eventSource.addEventListener("open",function(){i("EventSource connected"),y&&t.clearTimeout(y),v===!1&&(v=!0,s&&s(),l&&(a.trigger(r.onReconnect),e(o,u.connectionState.connected)))},!1),o.eventSource.addEventListener("message",function(n){n.data!=="initialized"&&f.processMessages(o,t.JSON.parse(n.data))},!1),o.eventSource.addEventListener("error",function(n){if(!v){h&&h();return}i("EventSource readyState: "+o.eventSource.readyState),n.eventPhase===t.EventSource.CLOSED?o.eventSource.readyState===t.EventSource.CONNECTING?(i("EventSource reconnecting due to the server connection ending"),e(o,u.connectionState.reconnecting),c.reconnect(o)):(i("EventSource closed"),c.stop(o)):(i("EventSource error"),a.trigger(r.onError))},!1)},reconnect:function(n){var i=this;t.setTimeout(function(){i.stop(n),i.start(n)},n.reconnectDelay)},send:function(n,t){f.ajaxSend(n,t)},stop:function(n){n&&n.eventSource&&(n.eventSource.close(),n.eventSource=null,delete n.eventSource)},abort:function(n,t){f.ajaxAbort(n,t)}},foreverFrame:{name:"foreverFrame",timeOut:3e3,start:function(o,s,h){var l=this,a=f.foreverFrame.count+=1,v,y,c=n("<iframe data-signalr-connection-id='"+o.id+"' style='position:absolute;top:0;left:0;width:0;height:0;visibility:hidden;'></iframe>");if(t.EventSource){h&&(i("This brower supports SSE, skipping Forever Frame."),h());return}n(o).trigger(r.onSending),v=f.getUrl(o,this.name),v+="&frameId="+a,c.prop("src",v),f.foreverFrame.connections[a]=o,i("Binding to iframe's readystatechange event."),c.bind("readystatechange",function(){n.inArray(this.readyState,["loaded","complete"])>=0&&(i("Forever frame iframe readyState changed to "+this.readyState+", reconnecting"),e(o,u.connectionState.reconnecting),l.reconnect(o))}),o.frame=c[0],o.frameId=a,s&&(o.onSuccess=s),n("body").append(c),y=t.setTimeout(function(){o.onSuccess&&(i("Failed to connect using forever frame source, it timed out after "+l.timeOut+"ms."),l.stop(o),h&&h())},l.timeOut)},reconnect:function(n){var r=this;t.setTimeout(function(){var u=n.frame,t=f.getUrl(n,r.name,!0)+"&frameId="+n.frameId;i("Upating iframe src to '"+t+"'."),u.src=t},n.reconnectDelay)},send:function(n,t){f.ajaxSend(n,t)},receive:f.processMessages,stop:function(t){t.frame&&(t.frame.stop?t.frame.stop():t.frame.document&&t.frame.document.execCommand&&t.frame.document.execCommand("Stop"),n(t.frame).remove(),delete f.foreverFrame.connections[t.frameId],t.frame=null,t.frameId=null,delete t.frame,delete t.frameId,i("Stopping forever frame"))},abort:function(n,t){f.ajaxAbort(n,t)},getConnection:function(n){return f.foreverFrame.connections[n]},started:function(t){t.onSuccess?(t.onSuccess(),t.onSuccess=null,delete t.onSuccess):(n(t).trigger(r.onReconnect),e(t,u.connectionState.connected))}},longPolling:{name:"longPolling",reconnectDelay:3e3,start:function(o,s){var l=this,c=!1;o.pollXhr&&(i("Polling xhr requests already exists, aborting."),o.stop()),o.messageId=null,t.setTimeout(function(){(function h(a,v){n(a).trigger(r.onSending);var d=a.messageId,k=d===null,b=!k,w=f.getUrl(a,l.name,b,v),p=null,y=!1;b===!0&&v===!0&&e(o,u.connectionState.reconnecting),i("Attempting to connect to '"+w+"' using longPolling."),a.pollXhr=n.ajax({url:w,global:!1,type:"GET",dataType:o.ajaxDataType,success:function(l){var w=0,p=!1;(c==!1&&(s(),c=!0),v===!0&&y===!1&&(i("Raising the reconnect event"),e(o,u.connectionState.connected),n(a).trigger(r.onReconnect),y=!0),f.processMessages(a,l),l&&l.TransportData&&n.type(l.TransportData.LongPollDelay)==="number"&&(w=l.TransportData.LongPollDelay),l&&l.TimedOut&&(p=l.TimedOut),l&&l.Disconnect)||(w>0?t.setTimeout(function(){h(a,p)},w):h(a,p))},error:function(u,f){if(f==="abort"){i("Aborted xhr requst.");return}i("An error occurred using longPolling. Status = "+f+". "+u.responseText),p&&clearTimeout(p),n(a).trigger(r.onError,[u.responseText]),t.setTimeout(function(){h(a,!0)},o.reconnectDelay)}}),v===!0&&(p=t.setTimeout(function(){y===!1&&(e(o,u.connectionState.connected),n(a).trigger(r.onReconnect),y=!0)},l.reconnectDelay))})(o),t.setTimeout(function(){c===!1&&(s(),c=!0)},150)},250)},send:function(n,t){f.ajaxSend(n,t)},stop:function(n){n.pollXhr&&(n.pollXhr.abort(),n.pollXhr=null,delete n.pollXhr)},abort:function(n,t){f.ajaxAbort(n,t)}}},u.noConflict=function(){return n.connection===u&&(n.connection=o),u},n.connection&&(o=n.connection),n.connection=n.signalR=u})(window.jQuery,window)