braid-http 1.3.63 → 1.3.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/braid-http-server.js +12 -3
- package/package.json +1 -1
package/braid-http-server.js
CHANGED
|
@@ -561,7 +561,12 @@ function braidify (req, res, next) {
|
|
|
561
561
|
res.on('finish', x => disconnected('finish'))
|
|
562
562
|
req.on('abort', x => disconnected('abort'))
|
|
563
563
|
|
|
564
|
-
//
|
|
564
|
+
// Start sending heartbeats to the client every N seconds if
|
|
565
|
+
// they've been requested. Heartbeats help a client know if a
|
|
566
|
+
// connection is still alive, and can also signal to
|
|
567
|
+
// intermediaries to keep a connection open, because sometimes
|
|
568
|
+
// intermediaries will time-out a connection after a period of no
|
|
569
|
+
// activity.
|
|
565
570
|
if (req.headers['heartbeats']) {
|
|
566
571
|
let heartbeats = parseFloat(req.headers['heartbeats'])
|
|
567
572
|
if (isFinite(heartbeats)) {
|
|
@@ -570,8 +575,12 @@ function braidify (req, res, next) {
|
|
|
570
575
|
res.on('close', () => closed = true)
|
|
571
576
|
loop()
|
|
572
577
|
function loop() {
|
|
573
|
-
|
|
574
|
-
|
|
578
|
+
// We only send heartbeats:
|
|
579
|
+
// - After the headers have been sent
|
|
580
|
+
// - Before the stream has closed
|
|
581
|
+
if (res.headersSent && !res.writableEnded && !closed)
|
|
582
|
+
res.write("\r\n")
|
|
583
|
+
|
|
575
584
|
setTimeout(loop, 1000 * heartbeats)
|
|
576
585
|
}
|
|
577
586
|
}
|