WebSocketStompClient 不会连接到 SockJS 端点
2022-09-03 17:58:31
我正在使用新的(从4.2版开始)java STOMP客户端支持。我的起点是入门指南(使用WebSocket构建交互式Web应用程序)。
示例中提供了以下终结点:
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
我可以使用浏览器客户端成功连接。尝试使用以下方法使用 java 踩踏客户端连接到此端点时:
WebSocketClient transport = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(transport);
stompClient.setMessageConverter(new StringMessageConverter());
String url = "ws://127.0.0.1:8080/hello";
ListenableFuture<StompSession> future = stompClient.connect(url,myHandler);
我遇到以下异常:
08:56:30.629 [SimpleAsyncTaskExecutor-1] DEBUG o.s.m.simp.stomp.DefaultStompSession - Failed to connect session id=4e6737da-8f68-691d-375f-9e46f48bc149 javax.websocket.DeploymentException: The HTTP response from the server [HTTP/1.1 200 OK ] did not permit the HTTP upgrade to WebSocket at org.apache.tomcat.websocket.WsWebSocketContainer.parseStatus(WsWebSocketContainer.java:637) ~[tomcat-embed-websocket-8.0.20.jar:8.0.20] at org.apache.tomcat.websocket.WsWebSocketContainer.processResponse(WsWebSocketContainer.java:621) ~[tomcat-embed-websocket-8.0.20.jar:8.0.20] at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:309) ~[tomcat-embed-websocket-8.0.20.jar:8.0.20] at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:152) ~[spring-websocket-4.2.0.BUILD-SNAPSHOT.jar:4.2.0.BUILD-SNAPSHOT] at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:149) ~[spring-websocket-4.2.0.BUILD-SNAPSHOT.jar:4.2.0.BUILD-SNAPSHOT] at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0] at java.lang.Thread.run(Unknown Source) [na:1.8.0]
经过进一步调查,我更改了基于TomcatWebSocketTestServer的服务器端点配置.java在StompWebSocketIntegrationTests中使用.java如下所示:
RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
.setAllowedOrigins("*");
我现在可以连接,但浏览器客户端无法连接。
GET http://localhost:8080/hello/info 404 (Not Found)
如果我将 .withSockJs() 添加到终结点配置中:
RequestUpgradeStrategy upgradeStrategy = new TomcatRequestUpgradeStrategy();
registry.addEndpoint("/hello")
.setHandshakeHandler(new DefaultHandshakeHandler(upgradeStrategy))
.setAllowedOrigins("*")
.withSockJs();
浏览器客户端可以再次连接,但 java 客户端返回到原始错误。
在这种情况下,我应该能够在两个客户端之间共享同一个终结点,还是需要配置两个单独的终结点?