请问javax.servlet.Filter拦截Websocket消息? (Java Servlet API) [英] Will javax.servlet.Filter intercept Websocket messages? (Java Servlet API)

查看:350
本文介绍了请问javax.servlet.Filter拦截Websocket消息? (Java Servlet API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个websocket连接时,请说/ ws

When I have a websocket connection, say at "/ws"

/ ws路径上的常规javax.servlet.Filter是否会拦截每条消息一个常规的http请求?它是否只拦截第一个请求(作为握手的一部分?)

Will a regular javax.servlet.Filter on the "/ws" path intercept each message as a regular http request? Will it intercept only the first request (as part of the handshake?)

如果不是,那么,我如何为websocket消息制作这样的过滤器?

If no, then, how can I make such a filter for websocket messages?

我在规范中没有找到任何具体细节。

I don't find any specifics about that in the spec.

推荐答案

否,Servlet Filter不会捕获WebSocket消息。

No, Servlet Filter won't catch WebSocket messages.

Servlet根本不支持WebSocket协议。您唯一能做的就是注册 HttpUpgradeHandler (参见 HttpServletRequest#升级)然后在其上实现WebSocket协议。

Servlet does not support WebSocket protocol at all. Only thing you can do is to register HttpUpgradeHandler (see HttpServletRequest#upgrade)and then implement WebSocket protocol on top of that.

或..如果你想节省一些时间,看看JSR 356和实现,他们正在这个API的基础上工作。我个人致力于 Tyrus ,这是JSR 356的参考实现。(它是Java EE 7的一部分,但是你只需要Servlet 3.1 API)。

Or.. if you want to save some time, take a look at JSR 356 and the implementations, they are working on top of this API. I personally work on Tyrus, which is the reference implementation of JSR 356. (It is part of Java EE 7, but you only need Servlet 3.1 API).

这篇关于请问javax.servlet.Filter拦截Websocket消息? (Java Servlet API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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