Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue: reconnection only happends for 1 time after connection drops #136

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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