fin*_*tic 5 java http-1.1 chunked-encoding okhttp
我们正在构建一个应用程序,它可以实时记录用户的语音,并通过 HTTP 请求将记录的数据发送到服务器。当服务器实时处理数据时,它也会以分块的形式发回响应。简单来说,应用程序正在向服务器一块一块地发送数据,同时,它也从服务器一块一块地接收响应。
请不要告诉我这是不可能的,因为我在 iOS 中有一个工作示例,它使用 withURLSession使用uploadTask流对将数据实时发送到服务器,然后从此回调中逐块接收响应urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)。
下面是我的 Java 代码。我的发送工作正常,但仅在发送完成后才收到响应。
RequestBody body = new RequestBody()
{
@Override
public MediaType contentType()
{
return MediaType.get("application/octet-stream");
}
@Override
public void writeTo(BufferedSink sink) throws IOException
{
String filename = "/path/spoken.pcm";
try
{
InputStream inputStream = new DataInputStream(new FileInputStream(new File(filename)));
byte[] cacheBytes = new byte[320];
int length;
while((length = inputStream.read(cacheBytes, 0, cacheBytes.length)) != -1)
{
System.out.println("write thread name: " + Thread.currentThread());
sink.write(cacheBytes, 0, length);
Thread.sleep(10);
}
inputStream.close();
}
catch(IOException | InterruptedException e)
{
e.printStackTrace();
}
}
};
Request request = new Request.Builder()
.url("www.server.com")
.post(body)
.build();
Interceptor interceptor = new Interceptor()
{
@Override
public Response intercept(Chain chain) throws IOException
{
System.out.println("intercept!!!");
CountDownLatch latch = new CountDownLatch(1);
Response response = chain.proceed(chain.request());
BufferedSource source = response.body().source();
System.out.println("got response body !!!!");
new Thread(new Runnable()
{
@Override
public void run()
{
byte[] cachedBytes = new byte[512];
try
{
while(!source.exhausted())
{
int length = source.read(cachedBytes);
byte[] partialBytes = new byte[length];
System.arraycopy(cachedBytes, 0, partialBytes, 0, length);
System.out.println("partial response received: " + getHexString(partialBytes));
}
}
catch (IOException e)
{
e.printStackTrace();
}
latch.countDown();
}
}).start();
try
{
latch.await();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return response;
}
};
httpClient = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
httpClient.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException
{
try(ResponseBody responseBody = response.body())
{
if(!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println("all responses received!");
}
}
});
Run Code Online (Sandbox Code Playgroud)
此日志:System.out.println("got response body !!!!");仅当我将所有数据发送到服务器后才出现。这意味着,当writeTo(BufferedSink sink)返回时,我会在拦截器回调中以块的形式获得响应,然后onResponse(Call call, Response response)调用回调。
我需要的是,当我发送数据时,我希望能够同时获得分块响应。
好吧,到处查了一下,这个实现并不是像其他人所说的HTTP标准方式。尽管 HTTP 并不限制您在单个请求中同时发送和接收,但我已经使用 iOS 应用程序作为客户端并使用 Java HTTP Servlet 作为服务器实现了这一点。问题是,大多数库不支持这种行为。您可以在客户端为此编写自己的 SDK,但如果您计划在服务器上使用负载均衡器和反向代理,您还应该考虑将来的风险,并且它们可能也不支持此行为。
因此,达到相同效果的最快解决方案是使用 HTTP 发送请求,但响应将通过 MQTT 发送到客户端。
| 归档时间: |
|
| 查看次数: |
1848 次 |
| 最近记录: |