diff options
author | Damjan Georgievski <gdamjan@gmail.com> | 2014-05-02 17:04:41 +0200 |
---|---|---|
committer | Robert Newson <rnewson@apache.org> | 2014-05-21 17:08:11 +0100 |
commit | 10dcfc5de8b39a420bfe0f2562be8a3946d30d67 (patch) | |
tree | 4c53ba97386f089ead698b839c78311cea8e88b0 | |
parent | 7439833ea51ac1436f341c7aab6e8de1fe4a2a18 (diff) | |
download | couchdb-10dcfc5de8b39a420bfe0f2562be8a3946d30d67.tar.gz |
Send a real EventSource event for heartbeat
The EventSource connection can get stuck (in TCP half-open state*) and there's no way
for the client to detect that. This commit changes the way heartbeat is sent, instead of
sending a newline character, it sends an empty event of type heartbeat:
event: heartbeat
data:
This event doesn't have an id: field, so the client will retain its latest Last-Event-ID state.
This doesn't change the expectations of clients that used EventSource till now, because they
subscribe to the 'message' event type. To get the 'heartbeat' events a client will need to
explicitly subscribe to it:
source.addEventListener('heartbeat', function () { /* cancel a timer that would otherwise reconnect the source */ });
* this can happen when you suspend your laptop, on flaky internet connection, ADSL reconnect,
bad wifi signals, bad routers etc. Pretty often in a typical internet usage nowadays.
-rw-r--r-- | share/www/script/test/changes.js | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/share/www/script/test/changes.js b/share/www/script/test/changes.js index 00944f73c..f35ce39d6 100644 --- a/share/www/script/test/changes.js +++ b/share/www/script/test/changes.js @@ -123,7 +123,7 @@ couchTests.changes = function(debug) { xhr = CouchDB.newXhr(); - //verify the hearbeat newlines are sent + //verify the heartbeat newlines are sent xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=continuous&heartbeat=10&timeout=500"), true); xhr.send(""); @@ -171,6 +171,25 @@ couchTests.changes = function(debug) { T(results[1].changes[0].rev == docBar._rev); } + // test that we receive EventSource heartbeat events + if (!!window.EventSource) { + var source = new EventSource( + "/test_suite_db/_changes?feed=eventsource&heartbeat=10"); + + var count_heartbeats = 0; + source.addEventListener('heartbeat', function () { count_heartbeats = count_heartbeats + 1; } , false); + + waitForSuccess(function() { + if (count_heartbeats < 3) { + throw "keep waiting"; + } + return true; + }, "eventsource-heartbeat"); + + T(count_heartbeats >= 3); + source.close(); + } + // test longpolling xhr = CouchDB.newXhr(); |