带有sockJS的Spring Websocket仅在本地计算机上工作

时间:2018-10-01 05:50:56

标签: java sockets spring-boot web spring-websocket

在我的应用程序中,WebSocket在本地运行良好。但是当我部署到外部服务器时,出现了错误,

  

WebSocket连接到 'WS:// 192的 * ** / WS / 279 / asm4kjqd / WebSocket的标记=承载%205jb20iLCJleHAiOjE1NDU5MTc2NTl9.ttG-utbulJyTjnAIYHunEsC03efxzJ7fePAvItBgdlE5jb20iLCJleHAiOjE1NDU5MTc2NTl9.ttG-utbulJyTjnAIYHunEsC03efxzJ7fePAvItBgdlE。?' 失败: WebSocket握手期间出错:意外的响应代码:200

我的Config类是

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport
        implements WebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints( StompEndpointRegistry stompEndpointRegistry )
    {
        stompEndpointRegistry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
    }

    @Bean
    @Override
    public WebSocketHandler subProtocolWebSocketHandler()
    {
        return new CustomSubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
    }

    @Override
    public void configureWebSocketTransport( WebSocketTransportRegistration registry )
    {
        super.configureWebSocketTransport(registry);
    }

    @Override
    public boolean configureMessageConverters( List<MessageConverter> messageConverters )
    {
        return super.configureMessageConverters(messageConverters);
    }

    @Override
    public void configureClientInboundChannel( ChannelRegistration registration )
    {
        super.configureClientInboundChannel(registration);
    }

    @Override
    public void configureClientOutboundChannel( ChannelRegistration registration )
    {
        super.configureClientOutboundChannel(registration);
    }

    @Override
    public void addArgumentResolvers( List<HandlerMethodArgumentResolver> argumentResolvers )
    {
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public void addReturnValueHandlers( List<HandlerMethodReturnValueHandler> returnValueHandlers )
    {
        super.addReturnValueHandlers(returnValueHandlers);
    }

然后从客户端连接,如下所示,

let id = localStorage.getItem("userId")
        let url = `/ws?token=${localStorage.getItem("access_token")}`;
        // Web Socket connection starts here.
        let sockJS = new SockJS(url);
        let stompClient = Stomp.over(sockJS);
        stompClient.connect({},(function(){
            stompClient.subscribe(`${wsUrls.notify.url}/${id}`, (function(message){
                let parsedObj = JSON.parse(message.body);
                this.props.dispatch(storeWsNotifications(parsedObj));
            }).bind(this));
        }).bind(this));
    }

在本地计算机(本地主机)中运行程序时,将建立websocket连接。但是当部署到服务器时,我遇到了上面提到的错误。该如何解决?

谢谢

1 个答案:

答案 0 :(得分:0)

您必须在url中指定主机名,例如: 让url = ws://remotehostname:port/ws?token=${localStorage.getItem("access_token")}

不只是:

让url = /ws?token=${localStorage.getItem("access_token")}

在您的JavaScript中。