Commit 39e009c9 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

use line-delimited json streams instead of RFC 7464

(because it is more common)
parent e1c154f9
Pipeline #135938 failed with stages
in 1 second
......@@ -167,7 +167,8 @@ class RateLimiter:
class JsonSeqStreamResponse(StreamResponse):
"""aiohttp response class for streaming json objects
The objects are streamed in the application/json-seq format (RFC 7464).
The stream is line-delimited, i.e: a newline <LF> character is inserted between each JSON
object (and the objects are guaranteed not to contain any newline)
usage:
resp = JsonSeqStreamResponse()
......@@ -180,14 +181,11 @@ class JsonSeqStreamResponse(StreamResponse):
"""
def __init__(self, *k, **kw):
super().__init__(*k, **kw)
self.content_type = "application/json-seq"
self.content_type = "application/json"
self.charset = "utf-8"
async def prepare(self, request):
prepared = self.prepared
await super().prepare(request)
if not prepared:
self.write(b"\x1e")
# send periodic nop messages (empty dict) to keep the connection alive
#
......@@ -200,7 +198,7 @@ class JsonSeqStreamResponse(StreamResponse):
loop = asyncio.get_event_loop()
def keepalive_cb():
try:
self.write(b"{}\n\x1e")
self.write(b"{}\n")
except StopIteration:
# it seems that self.write raises StopIteration when the
# connection is closed
......@@ -209,7 +207,7 @@ class JsonSeqStreamResponse(StreamResponse):
loop.call_later(KEEPALIVE_INTERVAL, keepalive_cb)
def send(self, json_object):
self.write(json.dumps(json_object).encode() + b"\n\x1e")
self.write(json.dumps(json_object).encode() + b"\n")
class DoneException(Exception):
pass
......
// Start an asynchronous long-polling HTTP GET request for streaming a sequence
// of JSON objects (RFC 7464)
// of line-delimited JSON objects
//
// url: string http url
// on_event: function(event) event callback
......@@ -54,7 +54,7 @@ function json_seq_event_listener(url, on_event=null, on_error=null)
// streaming
while(true) {
var end = req.responseText.indexOf("\x1e", req.offset);
var end = req.responseText.indexOf("\n", req.offset);
if (end < 0) {
break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment