如何正确使用ChunkedStream [英] How to use ChunkedStream properly

查看:151
本文介绍了如何正确使用ChunkedStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的用例...我有一个上游服务,通过网络发送我的Netty应用程序数据,并且该数据需要发布到连接到Netty的多个客户端。推送到客户端的数据必须是HTTPTransfer-Encoding:chunked。

Here's my use case... I have an upstream service that sends my Netty app data over the network, and that data needs to be published to multiple clients connected to Netty. The data pushed to the clients must be HTTP "Transfer-Encoding: chunked."

我发现 ChunkedStream 虽然也许我可以创建一个 PipedInputStream 和一个 PipedOutputStream (连接到 PipedInputStream )并将 ChunkedStream 写入频道。然后,当我从上游服务收到数据时,我可以将数据写入频道的 PipedOutputStream ,然后将其发送给客户:

I found ChunkedStream and though that maybe I could create a PipedInputStream and a PipedOutputStream (connected to the PipedInputStream) and write the ChunkedStream to the channel. Then when data is received from my upstream service I could write the data into the PipedOutputStream of the channels and it'd be sent to the clients:

在channelConnected中

PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
ctx.getChannel().write( new PersistentChunkedStream(in) );

单独的线程将数据发布到连接的渠道

ChannelBuffer buff = ChannelBuffers.copiedBuffer("FOO",CharsetUtil.UTF_8);
out.write( buff.array() );
channel.get(ChunkedWriteHandler.class).resumeTransfer();

我必须延长 ChunkedStream 才能返回 null 来自 nextChunk 如果有0个字节可用(在没有线程挂起的情况下暂停写入),所以我打电话写入关联频道的 PipedOutputStream 后, resumeTransfer 。当我调试并逐步执行代码时,我可以看到 flush ChunkedWriteHandler 被调用,它会调用:

I had to extend ChunkedStream to return null from nextChunk if there are 0 bytes available (to "suspend" the write without the thread hanging), so I call resumeTransfer after I write to the PipedOutputStream of the associated channel. When I debug and step through the code, I can see flush of ChunkedWriteHandler being called, which does call:

Channels.write(ctx, writeFuture, chunk, currentEvent.getRemoteAddress());

我写入 PipedOutputStream的字节,但客户从未收到过。

with the bytes I wrote into the PipedOutputStream, but it's never received by the client.

HTTP curl

~ $ curl -vN http://localhost:8080/stream
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /stream HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< 
### NOTE: NO "FOO" TRANSMIT BACK ###

有什么想法吗?也许有更好的方法来实现这个目标?

Any thoughts? Maybe there's a better way to accomplish this?

推荐答案

我想知道为什么你甚至想要使用PipedInputStream / PipedOutputStream。我认为在没有您的数据的情况下直接调用Channel.write(..)会更干净/更容易。请注意尽可能多地在Channel.write(..)中提交数据,因为这是一项昂贵的操作。

I wonder why you even want to use the PipedInputStream / PipedOutputStream. I think it would be away cleaner / easier to just call Channel.write(..) directly without your data. Just be aware to submit as much data as you can in Channel.write(..), as its an expensive operation.

您可以调用Channel.write(.. )来自您想要的任何线程,因为它是线程安全的。

You can call Channel.write(..) from any thread that you want, as its thread-safe.

这篇关于如何正确使用ChunkedStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆