Fat Java 客户端需要通过 http/https 与 Web 服务器进行双向通信通道
我有一种情况,我希望 Java 客户端有一个带有 servlet 的双向数据通道(我可以控制两者),这样任何一个都可以开始数据传输,而不必等待另一个先执行某些操作,但要要穿过防火墙,需要使用 http 或 https 进行隧道传输。
我环顾四周,但我不相信我知道询问谷歌的正确条件。
我最初是在研究 http 隧道模块,但意识到我在另一端有一个 Web 容器,我相信适当的方法是考虑一个需要与家庭通信的胖客户端。我想http 1.1中的持久连接在这里可能非常有用。我可以轻松地进行心跳传输以防止连接空闲。
此时我只需要进行概念验证,因此我主要需要现在可以工作的东西,然后可以对其进行优化甚至替换。
因此,我很感激指向允许我建立连接的项目的指针,其中任何一方都可以随意将信息(如序列化对象或描述性字节流)推送到另一方。如果可能的话,我更喜欢纯 Java。
编辑:感谢您的指点。看来我需要的东西将在 servlet 3.0 规范中提供,我可能最终会长期使用它,具体取决于它何时在各种 Web 容器中得到支持。
现在我正在研究 Cometd 包,它似乎能够完全满足我的原型的需要。
I have a situation where I want a Java client to have a two-way data channel with a servlet (I have control over both), so that either can begin data transferring without having to wait for the other to do something first, but to get through the firewalls this needs to be tunnelled in http or https.
I have looked around, but I do not believe I know the right terms for asking Google.
I was originally looking at http-tunneling modules, but realizing that I have a web container in the other end, I believe that the appropriate way is to think of a fat client needing to communicate home. I was thinking that the persistant connection in http 1.1 might be very useful here. I can easily do heartbeat transfers to keep the connection from ideling.
At this point in time I just need to do a proof of concept so I primarily need something that works now, which can then be optimized or even replaced later.
So, I'd appreciate pointers to projects that allow me to have a connection where either side can at will push information (like a serialized object or a descriptive stream of bytes) to the other side. I'd prefer pure Java, if at all possible.
EDIT: Thanks for the pointers. It appears that what I need, will be available in the servlet 3.0 specification, which I might end up using in the long term depending on when it will be supported in the various web containers.
For now I am investigating the Cometd package, which appears to be able to do exactly what I need for my prototype.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
搜索术语:comet、long-polling
这些主要用于 AJAX 上下文中,但我认为没有理由不能在 Java 项目中使用它们。
Search terms: comet, long-polling
These are mostly used in an AJAX context, but I see no reason why you could not use them in a Java project.
请看一下 Eclipse Net4J,
http://wiki.eclipse.org/Net4j
它支持所有你提到的功能。一个特别好的功能是它支持 HTTP 连接池,因此您可以在客户端和服务器之间拥有大量通道,但只使用少量 HTTP 连接。
唯一的问题是它根本没有文档。你只需要阅读源代码。一旦你弄清楚了,它就非常容易使用。
旧的 Net4J 站点上还有更多图表,
http://net4j.berlios.de/
Please take a look at Eclipse Net4J,
http://wiki.eclipse.org/Net4j
It supports all the features you mentioned. A special nice feature is that it supports HTTP connection pooling so you can have lots of channels between client and server but use only a few HTTP connections.
The only problem is that it doesn't have documentation at all. You just have to read the source code. Once you figure it out, it's very easy to use.
There are a few more diagrams on old Net4J site,
http://net4j.berlios.de/
需要多快?您始终可以在客户端上进行轮询。只需经常查看新消息即可。
How fast does it need to be? You could always just do polling on the client. Just check for new messages every so often.
您可以通过 HTTP 使用 Hessian 协议。它是一种用于序列化数据的快速二进制协议。通常用于 Web 服务风格的 RPC 通信,但没有理由它不能是双向的 - 请参阅 Hessian mux。它也是纯 Java :-)
You can use the Hessian protocol over HTTP. It's a fast binary protocol for serializing data. Typically used for a web-services style RPC communication, but there's no reason it couldn't be 2-way - see Hessian mux. It's pure Java, too :-)
通常,这是通过让服务器不立即响应 http 请求来完成的。在发送响应之前,它会等待一些更新(或超时)。显然,需要注意确保服务器能够在负载下处理此问题。
例如,参见 Comet。
Generally this is done by having the server not respond to an http request immediately. It waits around for some update (or a timeout) before sending a response. Obviously some care needs to be made ensuring that the server will handle this under load.
See, for instance, Comet.