WebSocket - 无效的状态行 javascript

3

我在Weblogic 12.1.3服务器上运行Spring 3.1.2后端。为了接受WebSocket连接,我的配置如下:

public class SpringConfigurator extends Configurator {

    private static final Logger LOGGER = LoggerFactory.make();

    private static final Map<String, Map<Class<?>, String>> cache = new ConcurrentHashMap<String, Map<Class<?>, String>>();

    private static final String MAGIC_STR = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

    private static final String NO_VALUE = ObjectUtils.identityToString(new Object());

    @SuppressWarnings("unchecked")
    @Override
    public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {

        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        if (wac == null) {
            String message = "Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?";
            LOGGER.error(message);
            throw new IllegalStateException(message);
        }

        String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
        if (wac.containsBean(beanName)) {
            T endpoint = wac.getBean(beanName, endpointClass);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Using @ServerEndpoint singleton " + endpoint);
            }
            return endpoint;
        }

        Component annot = AnnotationUtils.findAnnotation(endpointClass, Component.class);
        if ((annot != null) && wac.containsBean(annot.value())) {
            T endpoint = wac.getBean(annot.value(), endpointClass);
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Using @ServerEndpoint singleton " + endpoint);
            }
            return endpoint;
        }

        beanName = getBeanNameByType(wac, endpointClass);
        if (beanName != null) {
            return (T) wac.getBean(beanName);
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
        }
        return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
    }

    // modifyHandshake() is called before getEndpointInstance()
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        super.modifyHandshake(sec, request, response);

    }

    private String getBeanNameByType(WebApplicationContext wac, Class<?> endpointClass) {

        String wacId = wac.getId();

        Map<Class<?>, String> beanNamesByType = cache.get(wacId);
        if (beanNamesByType == null) {
            beanNamesByType = new ConcurrentHashMap<Class<?>, String>();
            cache.put(wacId, beanNamesByType);
        }

        if (!beanNamesByType.containsKey(endpointClass)) {
            String[] names = wac.getBeanNamesForType(endpointClass);
            if (names.length == 1) {
                beanNamesByType.put(endpointClass, names[0]);
            } else {
                beanNamesByType.put(endpointClass, NO_VALUE);
                if (names.length > 1) {
                    String message = "Found multiple @ServerEndpoint's of type " + endpointClass + ", names=" + names;
                    LOGGER.error(message);
                    throw new IllegalStateException(message);
                }
            }
        }

        String beanName = beanNamesByType.get(endpointClass);
        return NO_VALUE.equals(beanName) ? null : beanName;
    }

}

问题在于当我尝试通过 JavaScript 客户端打开 WebSocket 连接时,它会正确生成响应头。我已经在此位置进行了调试。
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
    super.modifyHandshake(sec, request, response);
}

但是在客户端上,它会出现以下错误:
WebSocket连接到'ws://localhost:7001/websocket'失败:WebSocket握手期间出错:无效的状态行
在Chrome开发工具中,响应如下所示:
HTTP/0.9 200 OK
我认为某种方式的http请求没有升级到websocket连接。
我真的很感激对这个问题的任何帮助。

我有相同的问题,但我正在使用PHP Ratchet作为服务器,你解决了这个问题吗? - Rafael Moni
2
你好,是的,我在我的服务器配置中发现了这个问题;_过滤器链_不知何故破坏了websocket握手响应。我实现了一个websocket过滤器,将请求直接分派到端点以绕过过滤器链。request.getRequestDispatcher("/websocket").forward(httpServletRequest, httpServletResponse); 这不是最好的解决方案,但它暂时解决了问题。 - iozgen
1个回答

1
今天我在测试WebLogic 12.2.1上的http://showcase.omnifaces.org/push/socket时遇到了这个问题。
当尝试连接websocket时,在第一次webapp测试时,WebLogic抛出以下异常:
java.lang.IllegalStateException: The async-support is disabled on this request: weblogic.servlet.internal.ServletRequest
Impl@6682044b[GET /omnifaces.push/counter?e6845a3a-26ed-4520-9824-63ffd85b24eb HTTP/1.1]
    at weblogic.servlet.internal.ServletRequestImpl.startAsync(ServletRequestImpl.java:1949)
    at weblogic.servlet.internal.ServletRequestImpl.startAsync(ServletRequestImpl.java:1925)
    at javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:432)
    at weblogic.websocket.tyrus.TyrusServletFilter.doFilter(TyrusServletFilter.java:234)
    ...

事实证明,与我测试过的所有其他服务器相反,WebLogic自己的TyrusServletFilter负责处理websocket握手请求,它在部署的webapp的web.xml提供的过滤器之后内部安装。webapp附带了一个字符编码过滤器和一个映射到/*的GZIP过滤器,因此它们在websocket过滤器之前被调用。这一开始看起来很奇怪,但我认为就是这样,所以我只是添加了<async-supported>true</async-supported>到那些webapp提供的过滤器,以便TyrusServletFilter可以发挥作用。
然而,在建立websocket连接时,当推送消息正在发送时,客户端会发生JavaScript错误,正是你所面临的错误:

WebSocket连接到'ws://localhost:7001/omnifaces.push/counter?e6845a3a-26ed-4520-9824-63ffd85b24eb'失败:WebSocket握手期间出错:无效状态行

原来WebSockets无法处理GZIP响应。禁用GZIP过滤器后,一切都继续完美运行。
然而,根本问题在于WebLogic应该在所有由Web应用程序提供的过滤器之前安装它的TyrusServletFilter。我测试过的所有其他Java EE服务器都能正确地执行此操作。您在问题评论中提到的立即分派和转发所有websocket握手请求到其目标URL模式的解决方法是一个好方法。另一种选择是重新配置web.xml提供的过滤器,不再匹配websocket握手请求,例如使用更具体的URL模式或映射到特定的servlet。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接