Home » Java » HTTP flush is not received with OkHTTP

HTTP flush is not received with OkHTTP

Posted by: admin July 23, 2018 Leave a comment

Questions:

I have a Java client using OkHTTP(3.11.0), which is streaming audio samples to my GO server. The server is recognizing the audio and progressively sending intermediate results via http.Flusher interface. However, the Java client is not able to receive them until it completes uploading the audio stream.

The GO client i wrote to test my GO server is able to receive them even while i am uploading from client.

Server Code

func Recognize(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
    if ok {
        ...//i have logic to get intermediate results
        w.Write([]byte("intermediate results of ASR"))
        flusher.Flush()
    }
}

Client code

    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder(BOUNDARY)
                    .setType(MultipartBody.FORM)
                    .addPart(jsonPart)
                    .addPart(audioPart);

        Request request = requestBuilder.post(multipartBuilder.build())
                .build();

        Log.v(TAG, "BEFORE MAKING REQUEST");
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                Log.v(TAG, "ON RESPONSE");

        });

I am seeing “ON RESPONSE” log only after i upload all my audio samples to server.

The http.Flusher Go doc says as Follows

Note that even for ResponseWriters that support Flush, if the client is connected through an HTTP proxy, the buffered data may not reach the client until the response completes.

How can i solve this issue? Somebody please help me.

Answers: