WebSocket握手期间出错:在Openshift中使用Spring Websocket,Stomp,SockJS时出现意外响应代码:400

时间:2016-12-15 03:39:48

标签: spring websocket openshift stomp sockjs

据我所知,到目前为止,在通过互联网搜索时,openshift仅提供端口8000,用于通过HTTP打开Web套接字连接。

我无法弄清楚的是如何使用Stomp和SockJs实现Spring WebSocket实现。这是我的代码段,

Websocket客户端实施:

function connect() {
        var socket = new SockJS('http://jbossews-nabac.rhcloud.com:8000/portal/ws');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/queue/reply/${pageContext.session.id}', function(greeting) {
                showGreeting(greeting.body);
            });
        });
    }

WebSocket ServerSide Configration:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketDefaultConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic/", "/queue/");
        config.setApplicationDestinationPrefixes("/portal");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
        .withSockJS()
        .setInterceptors(httpSessionIdHandshakeInterceptor());
    }

    @Bean
    public HttpSessionIdHandshakeInterceptor httpSessionIdHandshakeInterceptor() {
        return new HttpSessionIdHandshakeInterceptor();
    }

}

Chrome Console

如果我这样试试,

var socket = new SockJS('/portal/ws');

我得到的错误是,

Chrome Console

请帮我解决此错误。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,最后,经过很长一段时间我设法在Openshift V3中使用Spring WebSocket,但我认为这个解决方案也适用于较旧的Openshift V2。

唯一的妥协是不使用SockJS,而是使用标准的WebSocket API。

在你的情况下你必须在方法registerStompEndpoints中删除.withSockJS(),编辑添加后缀" / websocket"的端点路径。并将允许的起源设置为" *" (可能后者不是强制性的。)

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketDefaultConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic/", "/queue/");
        config.setApplicationDestinationPrefixes("/portal");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws/websocket")
        .setAllowedOrigins(‌​"*")
        .setInterceptors(httpSessionIdHandshakeInterceptor());
    }

    @Bean
    public HttpSessionIdHandshakeInterceptor httpSessionIdHandshakeInterceptor() {
        return new HttpSessionIdHandshakeInterceptor();
    }
}

在客户端中,如上所述,您必须使用WebSocket API而不是SockJS,并使用新路径连接到端点。

function connect() {
    var socket = new WebSocket('ws://your_service_exposed_route:80/portal/ws/websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/queue/reply/${pageContext.session.id}', function(greeting) {
            showGreeting(greeting.body);
        });
    });
}

如您所见,我使用了端口80:OpenShift V3支持在同一端口上运行WebSockets。当服务通过路由公开时,对于包括WebSockets在内的HTTP流量,这将是外部端口80。

这就是全部!