来自服务器 [200] 的 HTTP 响应不允许 HTTP 升级到 WebSocket [英] The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket

查看:183
本文介绍了来自服务器 [200] 的 HTTP 响应不允许 HTTP 升级到 WebSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 websocket 客户端在 STOMP Java 客户端(Spring 项目)中开发,服务器在 Spring 启动中实现.

当客户端/服务器握手发生时,我遇到了连接升级问题.

Java 客户端代码

List运输 = 新的 ArrayList<>(1);transports.add(new WebSocketTransport(new StandardWebSocketClient()));SockJsClient sockjsClient = new SockJsClient(transsports);WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);stompClient.setMessageConverter(new MappingJackson2MessageConverter());stompClient.setTaskScheduler(new ConcurrentTaskScheduler());StompSessionHandler sessionHandler = new SessionHandler();stompClient.connect("ws://localhost:9090/health", sessionHandler);

服务器端

@Override公共无效 registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {stompEndpointRegistry.addEndpoint("/health").setAllowedOrigins("*").withSockJS();}@覆盖public void configureMessageBroker(MessageBrokerRegistry 注册表){registry.enableSimpleBroker("/topic");registry.setApplicationDestinationPrefixes("/app");}

连接到服务器时客户端产生的异常

<前>16:18:50.955/3771 [SimpleAsyncTaskExecutor-1] 错误 o.s.w.s.s.c.DefaultTransportRequest - 在 TransportRequest[url=ws://localhost:9090/health/29/344d627baac949f5bab5f5106] 之后不再有后备传输javax.websocket.DeploymentException:来自服务器 [200] 的 HTTP 响应不允许 HTTP 升级到 WebSocket在 org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:434)在 org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:392)在 org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:194)在 org.springframework.web.socket.client.standard.StandardWebSocketClient.lambda$doHandshakeInternal$0(StandardWebSocketClient.java:150)在 java.util.concurrent.FutureTask.run(FutureTask.java:266)在 java.lang.Thread.run(Thread.java:745)

在 tomcat localhost_access_log 中,我看到以下请求信息

<前>127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET/health/info HTTP/1.1" 302 - 0127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET/login.jsp HTTP/1.1" 200 7649 16127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET/health/191/6828a1fdefee40cf8dc74e825d8d2b0c/websocket HTTP/1.1" 302 - 0127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET/login.jsp HTTP/1.1" 200 7649 0

我没有找到有关如何解决此问题及其原因的任何信息.

请帮我解决这个问题.

解决方案

为您的问题提供有用的解决方案为时已晚.今天我面临着你已经经历过的同样问题.不幸的是,Spring Framework 示例和官方资源并未完全引用.因此,我想写下我是如何创建和运行它的.首先,原始答案不属于我.我在

我希望它对正在寻找摆脱错误的方法的人有所帮助.

I have my websocket client developed in STOMP Java client (Spring project) and server implemented in Spring boot.

When the client/server handshake happens, I am getting a connection upgrade issue.

Java Client Code

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));      

SockJsClient sockjsClient = new SockJsClient(transports);   

WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);

stompClient.setMessageConverter(new MappingJackson2MessageConverter());

stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

StompSessionHandler sessionHandler = new SessionHandler();
stompClient.connect("ws://localhost:9090/health", sessionHandler);

Server side

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {       
    stompEndpointRegistry.addEndpoint("/health")
            .setAllowedOrigins("*")
            .withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic");
    registry.setApplicationDestinationPrefixes("/app");
}

Exception generated at client side while connection to server

16:18:50.955/3771 [SimpleAsyncTaskExecutor-1] ERROR o.s.w.s.s.c.DefaultTransportRequest - No more fallback transports after TransportRequest[url=ws://localhost:9090/health/29/344d627baac949f5bab5506f05f1a7eb/websocket]
javax.websocket.DeploymentException: The HTTP response from the server [200] did not permit the HTTP upgrade to WebSocket
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:434)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:392)
               at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:194)
               at org.springframework.web.socket.client.standard.StandardWebSocketClient.lambda$doHandshakeInternal$0(StandardWebSocketClient.java:150)
               at java.util.concurrent.FutureTask.run(FutureTask.java:266)
               at java.lang.Thread.run(Thread.java:745)

In the tomcat localhost_access_log I see the below request info

127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/info HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 16
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /health/191/6828a1fdefee40cf8dc74e825d8d2b0c/websocket HTTP/1.1" 302 - 0
127.0.0.1 - - [01/Nov/2018:22:09:41 +0530] "GET /login.jsp HTTP/1.1" 200 7649 0

I didn't find any info on how to fix this issue and what is causing it.

Please help me resolve this issue.

解决方案

Too late to be able to present a helpful resolution for your problem. Today I'm facing the same issue that you have already experienced. Unfortunately, Spring Framework samples and official resources are not referencing entirely. Therefore, I would like to write about how I made it up and running. Firstly, the original answer doesn't belong to me. I found it at https://code5.cn/so/java/783097.

As far as I can understand, to be able to upgrade the WebSocket protocol from HTTP, we should extend the "WebSocketConfiguration" part a little bit with desired capabilities.

RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
        .withSockJS();

registry.addEndpoint("/hello")
        .setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
        .setAllowedOrigins("*");

After making the above adjustments, you could comprehend regarding switching between protocols through sniffing WireShark.

I hope it would be helpful for someone who is looking for a way to get rid of errors.

这篇关于来自服务器 [200] 的 HTTP 响应不允许 HTTP 升级到 WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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