123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- /*
- * EventSource polyfill version 0.9.6
- * Supported by sc AmvTek srl
- * :email: devel@amvtek.com
- */
- ;(function (global) {
- if (global.EventSource && !global._eventSourceImportPrefix){
- return;
- }
- var evsImportName = (global._eventSourceImportPrefix||'')+"EventSource";
- var EventSource = function (url, options) {
- if (!url || typeof url != 'string') {
- throw new SyntaxError('Not enough arguments');
- }
- this.URL = url;
- this.setOptions(options);
- var evs = this;
- setTimeout(function(){evs.poll()}, 0);
- };
- EventSource.prototype = {
- CONNECTING: 0,
- OPEN: 1,
- CLOSED: 2,
- defaultOptions: {
- loggingEnabled: false,
- loggingPrefix: "eventsource",
- interval: 500, // milliseconds
- bufferSizeLimit: 256*1024, // bytes
- silentTimeout: 300000, // milliseconds
- getArgs:{
- 'evs_buffer_size_limit': 256*1024
- },
- xhrHeaders:{
- 'Accept': 'text/event-stream',
- 'Cache-Control': 'no-cache',
- 'X-Requested-With': 'XMLHttpRequest'
- }
- },
- setOptions: function(options){
- var defaults = this.defaultOptions;
- var option;
- // set all default options...
- for (option in defaults){
- if ( defaults.hasOwnProperty(option) ){
- this[option] = defaults[option];
- }
- }
- // override with what is in options
- for (option in options){
- if (option in defaults && options.hasOwnProperty(option)){
- this[option] = options[option];
- }
- }
- // if getArgs option is enabled
- // ensure evs_buffer_size_limit corresponds to bufferSizeLimit
- if (this.getArgs && this.bufferSizeLimit) {
- this.getArgs['evs_buffer_size_limit'] = this.bufferSizeLimit;
- }
- // if console is not available, force loggingEnabled to false
- if (typeof console === "undefined" || typeof console.log === "undefined") {
- this.loggingEnabled = false;
- }
- },
- log: function(message) {
- if (this.loggingEnabled) {
- console.log("[" + this.loggingPrefix +"]:" + message)
- }
- },
- poll: function() {
- try {
- if (this.readyState == this.CLOSED) {
- return;
- }
- this.cleanup();
- this.readyState = this.CONNECTING;
- this.cursor = 0;
- this.cache = '';
- this._xhr = new this.XHR(this);
- this.resetNoActivityTimer();
- }
- catch (e) {
- // in an attempt to silence the errors
- this.log('There were errors inside the pool try-catch');
- this.dispatchEvent('error', { type: 'error', data: e.message });
- }
- },
- pollAgain: function (interval) {
- // schedule poll to be called after interval milliseconds
- var evs = this;
- evs.readyState = evs.CONNECTING;
- evs.dispatchEvent('error', {
- type: 'error',
- data: "Reconnecting "
- });
- this._pollTimer = setTimeout(function(){evs.poll()}, interval||0);
- },
- cleanup: function() {
- this.log('evs cleaning up')
- if (this._pollTimer){
- clearInterval(this._pollTimer);
- this._pollTimer = null;
- }
- if (this._noActivityTimer){
- clearInterval(this._noActivityTimer);
- this._noActivityTimer = null;
- }
- if (this._xhr){
- this._xhr.abort();
- this._xhr = null;
- }
- },
- resetNoActivityTimer: function(){
- if (this.silentTimeout){
- if (this._noActivityTimer){
- clearInterval(this._noActivityTimer);
- }
- var evs = this;
- this._noActivityTimer = setTimeout(
- function(){ evs.log('Timeout! silentTImeout:'+evs.silentTimeout); evs.pollAgain(); },
- this.silentTimeout
- );
- }
- },
- close: function () {
- this.readyState = this.CLOSED;
- this.log('Closing connection. readyState: '+this.readyState);
- this.cleanup();
- },
- ondata: function() {
- var request = this._xhr;
- if (request.isReady() && !request.hasError() ) {
- // reset the timer, as we have activity
- this.resetNoActivityTimer();
- // move this EventSource to OPEN state...
- if (this.readyState == this.CONNECTING) {
- this.readyState = this.OPEN;
- this.dispatchEvent('open', { type: 'open' });
- }
- var buffer = request.getBuffer();
- if (buffer.length > this.bufferSizeLimit) {
- this.log('buffer.length > this.bufferSizeLimit');
- this.pollAgain();
- }
- if (this.cursor == 0 && buffer.length > 0){
- // skip byte order mark \uFEFF character if it starts the stream
- if (buffer.substring(0,1) == '\uFEFF'){
- this.cursor = 1;
- }
- }
- var lastMessageIndex = this.lastMessageIndex(buffer);
- if (lastMessageIndex[0] >= this.cursor){
- var newcursor = lastMessageIndex[1];
- var toparse = buffer.substring(this.cursor, newcursor);
- this.parseStream(toparse);
- this.cursor = newcursor;
- }
- // if request is finished, reopen the connection
- if (request.isDone()) {
- this.log('request.isDone(). reopening the connection');
- this.pollAgain(this.interval);
- }
- }
- else if (this.readyState !== this.CLOSED) {
- this.log('this.readyState !== this.CLOSED');
- this.pollAgain(this.interval);
- //MV: Unsure why an error was previously dispatched
- }
- },
- parseStream: function(chunk) {
- // normalize line separators (\r\n,\r,\n) to \n
- // remove white spaces that may precede \n
- chunk = this.cache + this.normalizeToLF(chunk);
- var events = chunk.split('\n\n');
- var i, j, eventType, datas, line, retry;
- for (i=0; i < (events.length - 1); i++) {
- eventType = 'message';
- datas = [];
- parts = events[i].split('\n');
- for (j=0; j < parts.length; j++) {
- line = this.trimWhiteSpace(parts[j]);
- if (line.indexOf('event') == 0) {
- eventType = line.replace(/event:?\s*/, '');
- }
- else if (line.indexOf('retry') == 0) {
- retry = parseInt(line.replace(/retry:?\s*/, ''));
- if(!isNaN(retry)) {
- this.interval = retry;
- }
- }
- else if (line.indexOf('data') == 0) {
- datas.push(line.replace(/data:?\s*/, ''));
- }
- else if (line.indexOf('id:') == 0) {
- this.lastEventId = line.replace(/id:?\s*/, '');
- }
- else if (line.indexOf('id') == 0) { // this resets the id
- this.lastEventId = null;
- }
- }
- if (datas.length) {
- // dispatch a new event
- var event = new MessageEvent(eventType, datas.join('\n'), window.location.origin, this.lastEventId);
- this.dispatchEvent(eventType, event);
- }
- }
- this.cache = events[events.length - 1];
- },
- dispatchEvent: function (type, event) {
- var handlers = this['_' + type + 'Handlers'];
- if (handlers) {
- for (var i = 0; i < handlers.length; i++) {
- handlers[i].call(this, event);
- }
- }
- if (this['on' + type]) {
- this['on' + type].call(this, event);
- }
- },
- addEventListener: function (type, handler) {
- if (!this['_' + type + 'Handlers']) {
- this['_' + type + 'Handlers'] = [];
- }
- this['_' + type + 'Handlers'].push(handler);
- },
- removeEventListener: function (type, handler) {
- var handlers = this['_' + type + 'Handlers'];
- if (!handlers) {
- return;
- }
- for (var i = handlers.length - 1; i >= 0; --i) {
- if (handlers[i] === handler) {
- handlers.splice(i, 1);
- break;
- }
- }
- },
- _pollTimer: null,
- _noactivityTimer: null,
- _xhr: null,
- lastEventId: null,
- cache: '',
- cursor: 0,
- onerror: null,
- onmessage: null,
- onopen: null,
- readyState: 0,
- // ===================================================================
- // helpers functions
- // those are attached to prototype to ease reuse and testing...
- urlWithParams: function (baseURL, params) {
- var encodedArgs = [];
- if (params){
- var key, urlarg;
- var urlize = encodeURIComponent;
- for (key in params){
- if (params.hasOwnProperty(key)) {
- urlarg = urlize(key)+'='+urlize(params[key]);
- encodedArgs.push(urlarg);
- }
- }
- }
- if (encodedArgs.length > 0){
- if (baseURL.indexOf('?') == -1)
- return baseURL + '?' + encodedArgs.join('&');
- return baseURL + '&' + encodedArgs.join('&');
- }
- return baseURL;
- },
- lastMessageIndex: function(text) {
- var ln2 =text.lastIndexOf('\n\n');
- var lr2 = text.lastIndexOf('\r\r');
- var lrln2 = text.lastIndexOf('\r\n\r\n');
- if (lrln2 > Math.max(ln2, lr2)) {
- return [lrln2, lrln2+4];
- }
- return [Math.max(ln2, lr2), Math.max(ln2, lr2) + 2]
- },
- trimWhiteSpace: function(str) {
- // to remove whitespaces left and right of string
- var reTrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
- return str.replace(reTrim, '');
- },
- normalizeToLF: function(str) {
- // replace \r and \r\n with \n
- return str.replace(/\r\n|\r/g, '\n');
- }
- };
- if (!isOldIE()){
- EventSource.isPolyfill = "XHR";
- // EventSource will send request using XMLHttpRequest
- EventSource.prototype.XHR = function(evs) {
- request = new XMLHttpRequest();
- this._request = request;
- evs._xhr = this;
- // set handlers
- request.onreadystatechange = function(){
- if (request.readyState > 1 && evs.readyState != evs.CLOSED) {
- if (request.status == 200 || (request.status>=300 && request.status<400)){
- evs.ondata();
- }
- else {
- request._failed = true;
- evs.readyState = evs.CLOSED;
- evs.dispatchEvent('error', {
- type: 'error',
- data: "The server responded with "+request.status
- });
- evs.close();
- }
- }
- };
- request.onprogress = function () {
- };
- request.open('GET', evs.urlWithParams(evs.URL, evs.getArgs), true);
- var headers = evs.xhrHeaders; // maybe null
- for (var header in headers) {
- if (headers.hasOwnProperty(header)){
- request.setRequestHeader(header, headers[header]);
- }
- }
- if (evs.lastEventId) {
- request.setRequestHeader('Last-Event-Id', evs.lastEventId);
- }
- request.send();
- };
- EventSource.prototype.XHR.prototype = {
- useXDomainRequest: false,
- _request: null,
- _failed: false, // true if we have had errors...
- isReady: function() {
- return this._request.readyState >= 2;
- },
- isDone: function() {
- return (this._request.readyState == 4);
- },
- hasError: function() {
- return (this._failed || (this._request.status >= 400));
- },
- getBuffer: function() {
- var rv = '';
- try {
- rv = this._request.responseText || '';
- }
- catch (e){}
- return rv;
- },
- abort: function() {
- if ( this._request ) {
- this._request.abort();
- }
- }
- };
- }
- else {
- EventSource.isPolyfill = "IE_8-9";
- // patch EventSource defaultOptions
- var defaults = EventSource.prototype.defaultOptions;
- defaults.xhrHeaders = null; // no headers will be sent
- defaults.getArgs['evs_preamble'] = 2048 + 8;
- // EventSource will send request using Internet Explorer XDomainRequest
- EventSource.prototype.XHR = function(evs) {
- request = new XDomainRequest();
- this._request = request;
- // set handlers
- request.onprogress = function(){
- request._ready = true;
- evs.ondata();
- };
- request.onload = function(){
- this._loaded = true;
- evs.ondata();
- };
- request.onerror = function(){
- this._failed = true;
- evs.readyState = evs.CLOSED;
- evs.dispatchEvent('error', {
- type: 'error',
- data: "XDomainRequest error"
- });
- };
- request.ontimeout = function(){
- this._failed = true;
- evs.readyState = evs.CLOSED;
- evs.dispatchEvent('error', {
- type: 'error',
- data: "XDomainRequest timed out"
- });
- };
- // XDomainRequest does not allow setting custom headers
- // If EventSource has enabled the use of GET arguments
- // we add parameters to URL so that server can adapt the stream...
- var reqGetArgs = {};
- if (evs.getArgs) {
- // copy evs.getArgs in reqGetArgs
- var defaultArgs = evs.getArgs;
- for (var key in defaultArgs) {
- if (defaultArgs.hasOwnProperty(key)){
- reqGetArgs[key] = defaultArgs[key];
- }
- }
- if (evs.lastEventId){
- reqGetArgs['evs_last_event_id'] = evs.lastEventId;
- }
- }
- // send the request
- request.open('GET', evs.urlWithParams(evs.URL,reqGetArgs));
- request.send();
- };
- EventSource.prototype.XHR.prototype = {
- useXDomainRequest: true,
- _request: null,
- _ready: false, // true when progress events are dispatched
- _loaded: false, // true when request has been loaded
- _failed: false, // true if when request is in error
- isReady: function() {
- return this._request._ready;
- },
- isDone: function() {
- return this._request._loaded;
- },
- hasError: function() {
- return this._request._failed;
- },
- getBuffer: function() {
- var rv = '';
- try {
- rv = this._request.responseText || '';
- }
- catch (e){}
- return rv;
- },
- abort: function() {
- if ( this._request){
- this._request.abort();
- }
- }
- };
- }
- function MessageEvent(type, data, origin, lastEventId) {
- this.bubbles = false;
- this.cancelBubble = false;
- this.cancelable = false;
- this.data = data || null;
- this.origin = origin || '';
- this.lastEventId = lastEventId || '';
- this.type = type || 'message';
- }
- function isOldIE () {
- //return true if we are in IE8 or IE9
- return (window.XDomainRequest && (window.XMLHttpRequest && new XMLHttpRequest().responseType === undefined)) ? true : false;
- }
- global[evsImportName] = EventSource;
- })(this);
|