okio - Streaming okhttp response body -
i'm implementing server-sent events library using okhttp. server sent events works keeping open http connection server on 'events' can streamed client. connection close on errors, or if client explicitly disconnects.
what's best way achieve streaming behaviour using okhttp? i've attempted like:
response.body().source().readall(new sink() { @override public void write(buffer source, long bytecount) throws ioexception { log.d(tag, "write(): bytecount = "+bytecount); } @override public void flush() throws ioexception { log.d(tag, "flush()"); } @override public timeout timeout() { return timeout.none; } @override public void close() throws ioexception { log.d(tag, "close()"); } });
with approach see log message in write()
, can take quite while (minutes). makes me think there might buffering going on under hood , don't data until buffer flushed.
i've used curl
verify server behaving correctly. data is being sent on time, i'm not getting callback when arrives.
my experience okhttp
, okio
limited, it's possible i've messed up, or have forgotten set option. appreciated! :)
when call readall()
okio prefers net throughput on latency, , messages buffered chunks. instead, write loop repeatedly reads buffer
. that'll messages arrive.
buffer buffer = new buffer(); while (!source.exhausted()) { long count = response.body().source().read(buffer, 8192); // handle data in buffer. }
Comments
Post a Comment