From dd9565c709dab8c63689b946bd33acafac919562 Mon Sep 17 00:00:00 2001 From: Haso Keric Date: Fri, 22 Oct 2010 03:00:10 -0400 Subject: [PATCH 1/2] added: minified version --- jquery.eventsource.min.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 jquery.eventsource.min.js diff --git a/jquery.eventsource.min.js b/jquery.eventsource.min.js new file mode 100644 index 0000000..e353fd6 --- /dev/null +++ b/jquery.eventsource.min.js @@ -0,0 +1,12 @@ +;(function($){$.extend($.ajaxSettings.accepts,{stream:'text/event-stream'}) +var stream={defaults:{label:null,url:null,open:$.noop,message:$.noop},setup:{stream:{},lastEventId:0,isNative:false,history:{},options:{}},cache:{}},pluginFns={public:{close:function(label){var cache={};if(label!=='*'){for(var prop in stream.cache){if(label!==prop){cache[prop]=stream.cache[prop];}}} +stream.cache=cache;return stream.cache;},streams:function(label){if(label==='*'){return stream.cache;} +return stream.cache[label]||{};}},_private:{isJson:function(arg){if(arg===null){return false;} +return(new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')).test($.isPlainObject(arg)?JSON.stringify(arg):arg);},openEventSource:function(options){stream.cache[options.label].stream.addEventListener('open',function(event){if(stream.cache[options.label]){this['label']=options.label;stream.cache[options.label].options.open.call(this,event);}},false);stream.cache[options.label].stream.addEventListener('message',function(event){if(stream.cache[options.label]){var streamData=[];streamData[streamData.length]=pluginFns._private.isJson(event.data)?JSON.parse(event.data):event.data +this['label']=options.label;stream.cache[options.label].lastEventId=+event.lastEventId;stream.cache[options.label].history[stream.cache[options.label].lastEventId]=streamData;stream.cache[options.label].options.message.call(this,streamData[0]?streamData[0]:null,{data:streamData,lastEventId:stream.cache[options.label].lastEventId},event);}},false);return stream.cache[options.label].stream;},openPollingSource:function(options){if(stream.cache[options.label]){var source=$.ajax({type:'GET',url:options.url,data:options.data,beforeSend:function(){if(stream.cache[options.label]){this['label']=options.label;stream.cache[options.label].options.open.call(this);}},success:function(data){var tempdata,parsedData=[],streamData=$.map(data.split("\n"),function(sdata,i){if(sdata){return sdata;}});if($.isArray(streamData)){for(var i=0;i Date: Fri, 22 Oct 2010 03:26:49 -0400 Subject: [PATCH 2/2] added: new customization functions for more control and updated minified version --- README.markdown | 20 ++++++++++++++++++++ example.js | 25 +++++++++++++++++++++++++ jquery.eventsource.js | 7 ++++--- jquery.eventsource.min.js | 6 ++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index efa481e..a55abb9 100644 --- a/README.markdown +++ b/README.markdown @@ -45,6 +45,26 @@ and falls back to Ajax polling logic when it's not. dataType: 'json', + // Set wether you want to do a GET or POST request + // Default if none specified is GET + + requestType: 'GET', + + // Set the request interval + // Default if none specified 500 + + requestInterval: 500, + + // Set when the request should timeout + // Default: 50000 if none specified + + timeout: 50000, + + // Set wether to only fire the success event if content has been modified + // Default is false + + ifModified: false, + // Set a callback to fire when the event source is opened // `onopen` open: function (data) { diff --git a/example.js b/example.js index 51c4bad..6f311f5 100644 --- a/example.js +++ b/example.js @@ -68,6 +68,31 @@ $(function () { $.eventsource('close', 'json-event-source'); } }); + + + // JSON EXAMPLE - POST AND SLOWER REQUEST INTERVAL + $.eventsource({ + label: 'json-event-source', + url: 'test-event-sources/event-source-2.php', + dataType: 'json', + requestType: 'POST', + requestInterval: 15000, + open: function () { + console.group('$.eventsource() - Example 3 : JSON open callback'); + console.log( 'opened' ); + + console.groupEnd('$.eventsource() - Example 3 : JSON open callback'); + }, + message: function (data) { + console.group('$.eventsource() - Example 3 : JSON message callback'); + console.log( 'message received' ); + console.log(data); + console.groupEnd('$.eventsource() - Example 3 : JSON message callback'); + + + $.eventsource('close', 'json-event-source'); + } + }); diff --git a/jquery.eventsource.js b/jquery.eventsource.js index 1ceb7d4..812f139 100644 --- a/jquery.eventsource.js +++ b/jquery.eventsource.js @@ -117,9 +117,10 @@ if ( stream.cache[options.label] ) { var source = $.ajax({ - type: 'GET', + type: options.requestType ? options.requestType : "GET", url: options.url, data: options.data, + ifModified: options.ifModified ? true : false, beforeSend: function () { if ( stream.cache[options.label] ) { @@ -170,12 +171,12 @@ function () { pluginFns._private.openPollingSource.call(this, options); }, - 500// matches speed of native EventSource + options.requestInterval ? options.requestInterval : 500 // matches speed of native EventSource ); } }, cache: false, - timeout: 50000 + timeout: options.timeout ? options.timeout : 50000 }); } return source; diff --git a/jquery.eventsource.min.js b/jquery.eventsource.min.js index e353fd6..a4625a7 100644 --- a/jquery.eventsource.min.js +++ b/jquery.eventsource.min.js @@ -1,3 +1,9 @@ +/*! + * jQuery Event Source + * + * Copyright (c) 2010 Rick Waldron + * Dual licensed under the MIT and GPL licenses. + */ ;(function($){$.extend($.ajaxSettings.accepts,{stream:'text/event-stream'}) var stream={defaults:{label:null,url:null,open:$.noop,message:$.noop},setup:{stream:{},lastEventId:0,isNative:false,history:{},options:{}},cache:{}},pluginFns={public:{close:function(label){var cache={};if(label!=='*'){for(var prop in stream.cache){if(label!==prop){cache[prop]=stream.cache[prop];}}} stream.cache=cache;return stream.cache;},streams:function(label){if(label==='*'){return stream.cache;}