JSR-356:如何在握手期间中止websocket连接? [英] JSR-356: How to abort a websocket connection during the handshake?

查看:188
本文介绍了JSR-356:如何在握手期间中止websocket连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够在握手期间中止websocket连接,以防HTTP请求不符合某些条件。根据我的理解,正确的地方是在配置器的.websocket.server.ServerEndpointConfig,%20javax.websocket.server.HandshakeRequest,%20javax.websocket.HandshakeResponse%29> ServerEndpointConfig.Configurator.modifyHandshake()方法实施。我只是无法弄清楚如何中止连接。有一个 HandshakeResponse 参数,它允许在响应中添加标题,但我不能找到任何完成工作的标题。

I need to be able to abort a websocket connection during the handshake in case the HTTP request does not meet certain criteria. From what I understand, the proper place to do that is inside the ServerEndpointConfig.Configurator.modifyHandshake() method of my own Configurator implementation. I just can't figure out what to do to abort the connection. There's a HandshakeResponse parameter which allows adding headers to the response but I couldn't find any header that does the job.

那么如何在握手期间中止websocket连接?这甚至可能吗?

So how can I abort a websocket connection during the handshake? Is that even possible?

推荐答案

你是对的,使用'modifyHandShake()'来更新响应标题,你需要完全正确的要删除或设置标头 Sec-WebSocket-Accept 的值,请从规范

you're right , use ´modifyHandShake()´ to update the response headers , you need exactly to remove or set the value of the header Sec-WebSocket-Accept, check this from the spec


| Sec-WebSocket-Accept |标头字段指示服务器是否愿意接受连接的
。如果存在,这个
头字段必须包含在
中发送的客户端nonce的哈希值| Sec-WebSocket-Key |以及预定义的GUID。任何其他值
不得解释为
服务器接受连接。

The |Sec-WebSocket-Accept| header field indicates whether the server is willing to accept the connection. If present, this header field must include a hash of the client's nonce sent in |Sec-WebSocket-Key| along with a predefined GUID. Any other value must not be interpreted as an acceptance of the connection by the server.



    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=




这些字段由WebSocket客户端检查脚本页面。
如果是| Sec-WebSocket-Accept | value与预期的
值不匹配,如果缺少头字段,或者如果HTTP状态代码是
不是101,则不会建立连接,并且WebSocket帧
将不会发送。

These fields are checked by the WebSocket client for scripted pages. If the |Sec-WebSocket-Accept| value does not match the expected value, if the header field is missing, or if the HTTP status code is not 101, the connection will not be established, and WebSocket frames will not be sent.

您的代码如下所示:

 @Override
public void modifyHandshake(ServerEndpointConfig sec,
    HandshakeRequest request, HandshakeResponse response) {
    super.modifyHandshake(sec, request, response);
    response.getHeaders().put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, new ArrayList<String>());
}

浏览器会将此解释为服务器未接受连接。例如在chrome中我收到消息

The browser will interpret this like server did not accepted the connection. for example in chrome I get the message


Websocket握手期间出错

Error during Websocket handshake

这篇关于JSR-356:如何在握手期间中止websocket连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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