我的Websocket客户端是使用STOMP Java客户端(Spring项目)开发的,而服务器是通过Spring启动实现的。
客户端/服务器握手发生时,出现连接升级问题。
Java客户端代码
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);
服务器端
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/health")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
}
与服务器连接时在客户端生成的异常
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)
在tomcat localhost_access_log中,我看到以下请求信息
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
我没有找到有关如何解决此问题以及造成此问题的原因的任何信息。
请帮助我解决此问题。
答案 0 :(得分:1)
为时已晚,无法为您的问题提供有用的解决方案。今天,我面临着您已经遇到的同一问题。不幸的是,Spring Framework示例和官方资源并未完全引用。因此,我想写一下我的制作和运行方式。首先,原始答案不属于我。我在https://code5.cn/so/java/783097找到了它。
据我了解,为了能够从HTTP升级WebSocket协议,我们应该对“ WebSocketConfiguration”部分进行一些扩展,以提供所需的功能。
RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
.withSockJS();
registry.addEndpoint("/hello")
.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
.setAllowedOrigins("*");
进行了上述调整后,您可以了解通过嗅探WireShark在协议之间进行切换的情况。
我希望这对寻求摆脱错误的人有所帮助。