其他分享
首页 > 其他分享> > SockJS无法创建/连接到Spring WebSocket

SockJS无法创建/连接到Spring WebSocket

作者:互联网

我正在学习Spring WebSocket.我已经成功运行了这个Spring WebSocket教程.现在我试图将它原样合并到我现有的Spring MVC应用程序中.当我从Chrome浏览器运行它时,我在其开发控制台中看到以下错误.

Chrome控制台

Opening Web Socket...
GET http://localhost:8080/MyAppName/api/gs-guide-websocket/info?t=1497735312528 500 (Internal Server Error) -- abstract-xhr.js:132
Whoops! Lost connection to http://localhost:8080/MyAppName/api/gs-guide-websocket -- stomp.min.js:8 

服务器端错误

javax.servlet.ServletException: Could not resolve view with name '/MyAppName/api/gs-guide-websocket/info' in servlet with name 'MyAppName'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1262)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)

客户端

function connect() {
    var socket = new SockJS('/MyAppName/api/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings', function (greeting) {
            showGreeting(JSON.parse(greeting.body).content);
        });
    });
}

服务器端

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/MyAppName/api/gs-guide-websocket").withSockJS();
    }

}

我现在试着解决这个问题几个小时.我该如何解决这个问题?

TY

解决方法:

如果您的应用程序上下文是MyAppName,那么您不需要在addEndpoint方法中指定它 – 此路径与您的应用程序上下文相关.

可能是registry.addEndpoint(“/ MyAppName / api / gs-guide-websocket”)使用path / MyAppName / MyAppName / api / gs-guide-websocket注册端点

标签:spring-mvc,spring,spring-websocket,stomp,sockjs
来源: https://codeday.me/bug/20190710/1427641.html