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.
diff --git a/script/test/changes.js b/script/test/changes.js
index 0fba9f9..b034a0b 100644
--- a/script/test/changes.js
+++ b/script/test/changes.js
@@ -123,7 +123,7 @@
 
     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 @@
       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();