由 Servlet 容器提供服务的 WebSocket [英] WebSockets served by a Servlet Container

查看:43
本文介绍了由 Servlet 容器提供服务的 WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我正在研究 WebSockets,并就如何使用 Java Servlet API 实现服务器端提出了一些想法.我没有花太多时间,但是在使用 Tomcat 的一些测试中遇到了以下问题,如果不修补容器或至少对 HttpServletResponse 实现进行容器特定的修改,这些问题似乎无法解决:

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:

  • WebSocket 规范要求在 101 HTTP 响应中定义一条消息.HttpServletResponse.setStatus(int code, String message) 已弃用,但未提及可用的替代品.更改默认的 Tomcat 配置后,我让 Tomcat 接受我的消息字符串,但由于该方法已被弃用,我不确定这是否适用于其他 servlet 容器.

  • 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.

WebSocket 规范要求对连接升级请求的 HTTP 响应中的前几个标头的指定顺序.servlet API 不提供指定响应标头顺序的方法,Tomcat 将自己的标头添加到响应中,将其中一些标头放在任何标头之前,这些标头由 servlet 实现添加.

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.

由于在提交标头时不知道响应的内容长度,因此 Tomcat 会自动切换到响应的分块传输编码,这与 WebSocket 规范不兼容.

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.

我是否遗漏了一些明显的东西,或者真的不可能在基于 servlet 的 Web 应用程序中集成 WebSocket 服务器端点?

Am I missing something obvious, or is it really not possible to integrate WebSocket server endpoints in a servlet based web app?

推荐答案

Jetty 中有一个实现.我们可以希望 tomcat 和 jetty 找到兼容的 API.

There is an implementation in Jetty. We can hope that tomcat and jetty find a compatible API.

这篇关于由 Servlet 容器提供服务的 WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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