Skip to content

Commit 5743baf

Browse files
committed
Settable retry time in MS. Fixes rwaldron#5
1 parent 87d6e7b commit 5743baf

File tree

4 files changed

+145
-76
lines changed

4 files changed

+145
-76
lines changed

jquery.eventsource.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
stream: {},
2727
lastEventId: 0,
2828
isHostApi: false,
29+
retry: 500,
2930
history: {},
3031
options: {}
3132
},
@@ -71,99 +72,111 @@
7172
var label = options.label;
7273

7374
stream.cache[ label ].stream.addEventListener("open", function(event) {
74-
if ( stream.cache[label] ) {
75+
if ( stream.cache[ label ] ) {
7576

7677
this.label = label;
7778

78-
stream.cache[label].options.open.call(this, event);
79+
stream.cache[ label ].options.open.call(this, event);
7980
}
8081
}, false);
8182

8283
stream.cache[label].stream.addEventListener("message", function(event) {
8384

8485
var streamData = [];
8586

86-
if ( stream.cache[label] ) {
87+
if ( stream.cache[ label ] ) {
8788

8889
streamData[ streamData.length ] = jQuery.parseJSON( event.data );
8990

9091
this.label = label;
9192

92-
stream.cache[label].lastEventId = +event.lastEventId;
93-
stream.cache[label].history[stream.cache[label].lastEventId] = streamData;
94-
stream.cache[label].options.message.call(this, streamData[0] ? streamData[0] : null, {
93+
stream.cache[ label ].lastEventId = +event.lastEventId;
94+
stream.cache[ label ].history[stream.cache[ label ].lastEventId] = streamData;
95+
stream.cache[ label ].options.message.call(this, streamData[0] ? streamData[0] : null, {
9596
data: streamData,
96-
lastEventId: stream.cache[label].lastEventId
97+
lastEventId: stream.cache[ label ].lastEventId
9798
}, event);
9899

99100
// TODO: Add custom event triggering
100101
}
101102
}, false);
102103

103-
return stream.cache[label].stream;
104+
return stream.cache[ label ].stream;
104105
},
105106
// open fallback event source
106107
openPollingSource: function( options ) {
107-
var label = options.label,
108+
var label = options.label,
108109
source;
109110

110-
if ( stream.cache[label] ) {
111+
if ( stream.cache[ label ] ) {
111112

112113
source = jQuery.ajax({
113114
type: "GET",
114115
url: options.url,
115116
data: options.data,
116117
beforeSend: function() {
117-
if ( stream.cache[label] ) {
118+
if ( stream.cache[ label ] ) {
118119
this.label = label;
119-
stream.cache[label].options.open.call( this );
120+
stream.cache[ label ].options.open.call( this );
120121
}
121122
},
122123
success: function( data ) {
123124

124125
var tempdata,
125126
label = options.label,
126127
parsedData = [],
127-
streamData = jQuery.map( data.split("\n"), function(sdata, i) {
128+
streamData = jQuery.map( data.split("\n\n"), function(sdata, i) {
128129
return !!sdata && sdata;
129-
}),
130-
idx = 0, length = streamData.length;
130+
}),
131+
idx = 0, length = streamData.length,
132+
rretryprefix = /retry/,
133+
retries;
131134

132-
if ( jQuery.isArray(streamData) ) {
135+
if ( jQuery.isArray( streamData ) ) {
133136

134137
for ( ; idx < length; idx++ ) {
135138

136-
if ( streamData[idx] ) {
137-
tempdata = streamData[idx].split("data: ")[ 1 ];
139+
if ( streamData[ idx ] ) {
138140

139-
// Convert `dataType` here
140-
if ( options.dataType === "json" ) {
141-
tempdata = jQuery.parseJSON( tempdata );
142-
}
141+
if ( rretryprefix.test( streamData[ idx ] ) &&
142+
(retries = streamData[ idx ].split("retry: ")).length ) {
143+
144+
if ( retries.length === 2 && !retries[ 0 ] ) {
145+
146+
stream.cache[ label ].retry = stream.cache[ label ].options.retry = +retries[ 1 ];
147+
}
148+
149+
} else {
150+
tempdata = streamData[ idx ].split("data: ")[ 1 ];
143151

144-
parsedData[ parsedData.length ] = tempdata;
152+
// Convert `dataType` here
153+
if ( options.dataType === "json" ) {
154+
tempdata = jQuery.parseJSON( tempdata );
155+
}
156+
157+
parsedData[ parsedData.length ] = tempdata;
158+
}
145159
}
146160
}
147161
}
148162

149-
if ( stream.cache[label] ) {
163+
if ( stream.cache[ label ] ) {
150164

151165
this.label = label;
152166

153-
stream.cache[label].lastEventId++;
154-
stream.cache[label].history[stream.cache[label].lastEventId] = parsedData;
155-
stream.cache[label].options.message.call(this, parsedData[0] ? parsedData[0] : null, {
167+
stream.cache[ label ].lastEventId++;
168+
stream.cache[ label ].history[ stream.cache[ label ].lastEventId ] = parsedData;
169+
stream.cache[ label ].options.message.call(this, parsedData[0] ? parsedData[0] : null, {
156170
data: parsedData,
157-
lastEventId: stream.cache[label].lastEventId
171+
lastEventId: stream.cache[ label ].lastEventId
158172
});
159173

160-
161174
setTimeout(
162175
function() {
163176
pluginFns._private.openPollingSource.call( this, options );
164177
},
165-
// matches speed of host api EventSource
166-
500
178+
// Use server sent retry time if exists or default retry time if not
179+
( stream.cache[ label ] && stream.cache[ label ].retry ) || 500
167180
);
168181
}
169182
},
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
<?php
2-
header("Content-Type: text/event-stream\n\n");
3-
4-
// despite not having the while(true){}
5-
// this seems to repeat pushing messages to the client
6-
echo 'data: ' . json_encode(
7-
array(
8-
0 => array(
9-
'time' => time(),
10-
'message' => 'Some kind of foo'
11-
),
12-
1 => array(
13-
'time' => time(),
14-
'message' => 'Some kind of quux'
15-
)
16-
)
17-
) . "\n";
18-
19-
?>
1+
<?php
2+
header("Content-Type: text/event-stream\n\n");
3+
4+
// despite not having the while(true){}
5+
// this seems to repeat pushing messages to the client
6+
echo 'data: ' . json_encode(
7+
array(
8+
0 => array(
9+
'time' => time(),
10+
'message' => 'Some kind of foo'
11+
),
12+
1 => array(
13+
'time' => time(),
14+
'message' => 'Some kind of quux'
15+
)
16+
)
17+
) . "\n";
18+
19+
?>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
header("Content-Type: text/event-stream\n\n");
3+
4+
echo 'retry: 1000' . "\n\n";
5+
6+
echo 'data: ' . json_encode(
7+
array(
8+
0 => array(
9+
'time' => time(),
10+
'message' => 'Some kind of foo'
11+
),
12+
1 => array(
13+
'time' => time(),
14+
'message' => 'Some kind of quux'
15+
)
16+
)
17+
) . "\n";
18+
19+
20+
?>

0 commit comments

Comments
 (0)