Skip to content

Commit

Permalink
Merge pull request #136 from icy-fish/master
Browse files Browse the repository at this point in the history
Fix issue: re-connection only happens for 1 time after connection drops
  • Loading branch information
joeybaker committed Jul 7, 2020
2 parents 61e1b19 + 9a4190f commit 46fe04e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/eventsource.js
Expand Up @@ -242,6 +242,7 @@ function EventSource (url, eventSourceInitDict) {
})

req.on('error', function (err) {
self.connectionInProgress = false
onConnectionClosed(err.message)
})

Expand Down
28 changes: 28 additions & 0 deletions test/eventsource_test.js
Expand Up @@ -710,6 +710,34 @@ describe('Reconnection', function () {
}
})

it('continuing attempts when server is down', function (done) {
// Seems set reconnectInterval=0 not work here, this makes total time spent for current case more than 3S
this.timeout(4000)

var es = new EventSource('http://localhost:' + _port++)
es.reconnectInterval = 0
var reconnectCount = 0

es.onerror = function () {
reconnectCount++
// make sure client is keeping reconnecting
if (reconnectCount > 2) {
es.onerror = null
var port = u.parse(es.url).port
configureServer(http.createServer(), 'http', port, function (err, server) {
if (err) return done(err)

server.on('request', writeEvents(['data: hello\n\n']))

es.onmessage = function (m) {
assert.equal('hello', m.data)
server.close(done)
}
})
}
}
})

it('is attempted when server goes down after connection', function (done) {
createServer(function (err, server) {
if (err) return done(err)
Expand Down

0 comments on commit 46fe04e

Please sign in to comment.