Spring boot Websocket with stomp js: I keep getting Whoops! Lost connection to http://localhost:8080/ws

人走茶凉 提交于 2020-05-29 08:25:27

问题


I want to integrate chatting into an application I made, and after following some tutorials and running the application I keep getting "Whoops! Lost connection to http://localhost:8080/ws" on my console I tried using sockjs path as"/ws" but still got the same error, please can someone explain to me what i am doing wrong ?

here is the snippet of my code:

 @Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {


    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {

        stompEndpointRegistry.addEndpoint("/ws")
                .setHandshakeHandler(new CustomHandshakeHandler())
                .withSockJS();

    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        registry.setApplicationDestinationPrefixes("/app");

        registry.enableSimpleBroker("/message");

    }
}

and this is my client

    var socket = new SockJS('http://localhost:8080/ws');

stompClient = Stomp.over(socket);

stompClient.connect({}, onConnected, onError);

function onConnected() {

    console.log("its working");

}


function onError(error) {

    console.log(error);
}

回答1:


i am not sure about the CustomHandshakeHandler which you are using here. so that might be an issue to look into. also, consider to add .setAllowedOrigins("*") to your stompEndpointRegistry.

apart from that, the code looks ok and should work IMO.



来源:https://stackoverflow.com/questions/50170970/spring-boot-websocket-with-stomp-js-i-keep-getting-whoops-lost-connection-to-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!