本文介绍了Websockets 未连接在代理后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很常见的问题,但我找不到针对我的具体情况的解决方案.我使用的是 Glassfish 4.1.1,我的应用程序实现了 Websockets.

在客户端,我只是通过以下方式连接到 WS-server:

var serviceLocation = "ws://" + window.location.host + window.location.pathname + "dialog/";var wsocket = new WebSocket(serviceLocation + token_var);

在服务器端,websockets 是通过@ServerEndpoint 功能实现的,看起来很常见:

@ServerEndpoint(value = "/dialog/{token}",decoders = DialogMessageDecoder.class)公共类 DialogWebsoketEndpoint {@OnOpenpublic void open(final Session session, @PathParam("token") final String token) { ... }等等.}

一切正常,直到客户尝试通过代理连接.使用此测试:

在我的电脑上,除了一件事外,它看起来很相似:HTTP 代理:否.

解决方案

正如对问题的评论所述,代理似乎不能正确支持 Websocket.

这是一个常见问题(一些手机公司的代理会中断 websocket 连接),解决方案是使用 TLS/SSL 连接.

问题的出现主要是因为某些代理正确"(读取:损坏)Websocket 请求标头.

但是,在使用 TLS/SSL 时,代理无法读取标头数据(已加密),导致大多数代理上的数据直通".

这意味着标头将安全地到达另一端,代理将(大部分)忽略连接......这可能仍会导致连接超时相关的问题,但通常可以解决问题.

编辑

请注意,浏览器将保护客户端免于将非加密内容与加密内容混合.确保脚本在使用 TLS/SSL 连接时使用 wss 变体启动 ws 连接.

This is quite common problem, but I cannot find a solution to my specific case. I'm using Glassfish 4.1.1 and my application implements Websockets.

On a client side I'm connecting to WS-server simply by:

var serviceLocation = "ws://" + window.location.host + window.location.pathname + "dialog/";
var wsocket = new WebSocket(serviceLocation + token_var);

On a server side websockets are implemented via @ServerEndpoint functionality and looks very common:

@ServerEndpoint(value = "/dialog/{token}", decoders = DialogMessageDecoder.class)
public class DialogWebsoketEndpoint {

    @OnOpen
    public void open(final Session session, @PathParam("token") final String token) { ... }
etc.
}

Everything works fine up to the moment when customer tries to connect behind proxy.Using this test: http://websocketstest.com/ I've found that computer of the customer works behind http-proxy 1.1.He cannot connect to websockets, onopen simply do not fire at all. wsoscket.readyState never become 1.

How can I tune my ServerEndpoint to make this code work even when customer is connecting behind proxy?

Thank you in advance!

UPDATE: I would provide a screenshot with websocketstest at that computer:

On my computer it seems similarly except one thing:HTTP Proxy: NO.

解决方案

Much as the comments to the questions state, it seems the Proxy doesn't support Websockets properly.

This is a common issue (some cell-phone companies have proxies that disrupt websocket connections) and the solution is to use TLS/SSL connections.

The issue comes up mainly because some proxies "correct" (read: corrupt) the Websocket request headers.

However, when using TLS/SSL, the proxies can't read the header data (which is encrypted), causing data "pass-through" on most proxies.

This means the headers will arrive safely at the other end and the proxy will (mostly) ignore the connection... this might still cause an issue where connection timeouts are concerned, but it usually resolves the issue.

EDIT

Notice that the browsers will protect the client from mixing non-encrypted content with encrypted content. Make sure the script initiates the ws connections using the wss variant when TLS/SSL connections are used.

这篇关于Websockets 未连接在代理后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:46