java 由 Servlet 容器提供服务的 WebSocket
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2190500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebSockets served by a Servlet Container
提问by jarnbjo
I was taking a look at WebSockets last week and made a few thoughts on how to implement the server side with the Java Servlet API. I didn't spend too much time, but ran into the following problems during a few tests with Tomcat, which seem impossible to solve without patching the container or at least making container specific modifications to the HttpServletResponse implementation:
上周我正在研究 WebSockets,并对如何使用 Java Servlet API 实现服务器端提出了一些想法。我没有花太多时间,但是在使用 Tomcat 的一些测试中遇到了以下问题,如果不修补容器或至少对 HttpServletResponse 实现进行容器特定的修改,这些问题似乎无法解决:
The WebSocket specification mandate a defined message in the 101 HTTP response. HttpServletResponse.setStatus(int code, String message) is deprecated without mentioning a usable replacement. After changing the default Tomcat configuration, I made Tomcat honor my message string, but since the method is deprecated, I'm not sure if this will work with other servlet containers.
The WebSocket specification require a specified order of the first few headers in the HTTP response to the connection upgrade request. The servlet API does not offer a method to specify the order of the response headers and Tomcat adds its own headers to the response, placing a few of them before any headers, which are added by the servlet implementation.
Since the content length of the response is not known when committing the header, Tomcat automatically switches to chunked transfer encoding for the response, which is incompatible with the WebSocket specification.
WebSocket 规范要求在 101 HTTP 响应中定义一条消息。HttpServletResponse.setStatus(int code, String message) 已弃用,但未提及可用的替代品。更改默认的 Tomcat 配置后,我让 Tomcat 接受我的消息字符串,但由于该方法已被弃用,我不确定这是否适用于其他 servlet 容器。
WebSocket 规范要求对连接升级请求的 HTTP 响应中的前几个标头的指定顺序。servlet API 不提供指定响应标头顺序的方法,Tomcat 将自己的标头添加到响应中,将其中一些标头放在任何标头之前,这些标头由 servlet 实现添加。
由于提交标头时响应的内容长度是未知的,因此Tomcat自动切换到响应的分块传输编码,这与WebSocket规范不兼容。
Am I missing something obvious, or is it really not possible to integrate WebSocket server endpoints in a servlet based web app?
我是否遗漏了一些明显的东西,或者真的不可能在基于 servlet 的 Web 应用程序中集成 WebSocket 服务器端点?
回答by Horcrux7
回答by dlaidlaw
The Glassfish Atmosphereproject will do what you want. There is a servlet you can define to do all the work.
该Glassfish的气氛项目会做你想要什么。您可以定义一个 servlet 来完成所有工作。
回答by Oliver
jWebSocketclaims to run as a Tomcat application. Unfortunately some files are missing in the binary distribution of jWebSocket. Some people are trying to recompile jWebSocketand get the necessary files, since the source code is available. All in all, jWebSocketdoes not seem as a reliable product.
jWebSocket声称作为 Tomcat 应用程序运行。不幸的是,在二进制分发版中缺少一些文件jWebSocket。有些人试图重新编译jWebSocket并获取必要的文件,因为源代码是可用的。总而言之,jWebSocket似乎不是一个可靠的产品。
回答by Predrag Stojadinovi?
Yes there is a very good one (open source and completely free): http://www.jWebSocket.org
是的,有一个非常好的(开源且完全免费):http: //www.jWebSocket.org

