1
我正在嘗試使用spring-websocket RestTemplateXhrTransport進行集成測試,但無法使其工作。spring-websocket的RestTemplateXhrTransport工作嗎?
服務器是安裝有彈簧的WebSocket(4.1-RELEASE)與由這本書的配置:
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket").withSockJS().setClientLibraryUrl("../common/lib/sockjs.js");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic/", "/user/");
registry.setApplicationDestinationPrefixes("/app", "/topic");
}
}
我本來期望下面的代碼工作,但它只是超時等待爲序言。如果我看的網絡流量,我可以看到,序言是由服務器發送,但是客戶端只是掛在讀完頭後等待數據:
List<Transport> transports = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate();
RestTemplateXhrTransport xhrTransport = new RestTemplateXhrTransport(restTemplate);
// This makes it work, but that is xhr-polling, and I want xhr-streaming.
//xhrTransport.setXhrStreamingDisabled(true);
transports.add(xhrTransport);
SockJsClient sockJsClient = new SockJsClient(transports);
AbstractWebSocketHandler webSocketHandler = new AbstractWebSocketHandler() {
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
System.out.println("connected");
}
};
sockJsClient.doHandshake(webSocketHandler, new WebSocketHttpHeaders(), new URI("http://localhost:8080/socket"))
.get(3000, TimeUnit.SECONDS);
我使用的是Tomcat應用服務器。它無法兼容7.0.55和8.0.12。
我也試着從spring-websockets-portfolio示例中看一下IntegrationPortfolioTests。如果我在將WebSocketTransport添加爲主要傳輸並將使用RestTemplateXhrTransport的行留在其中的設置方法中註釋掉該行,則此測試用例也不起作用。