dimanche 30 septembre 2018

Spring websocket with sockJS only working in local machine

In my application, WebSocket is working fine in local. But when I deployed to an external server, I am getting the error,

WebSocket connection to 'ws://192.*..**/ws/279/asm4kjqd/websocket?token=Bearer%205jb20iLCJleHAiOjE1NDU5MTc2NTl9.ttG-utbulJyTjnAIYHunEsC03efxzJ7fePAvItBgdlE5jb20iLCJleHAiOjE1NDU5MTc2NTl9.ttG-utbulJyTjnAIYHunEsC03efxzJ7fePAvItBgdlE' failed: Error during WebSocket handshake: Unexpected response code: 200

My Config classes are,

@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);
    }

And from client side, I am connecting as follow,

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));
    }

The websocket connection is getting established when I run the programm in local machine (localhost). But when deployed to the server I am getting above mentioned error. How to fix this?

Thanks




Aucun commentaire:

Enregistrer un commentaire