gpt4 book ai didi

spring - SockJS 无法创建/连接到 Spring WebSocket

转载 作者:行者123 更新时间:2023-12-02 11:21:49 24 4
gpt4 key购买 nike

我正在学习 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();
}

}

我已经尝试解决这个问题几个小时了。我该如何解决?

最佳答案

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

大概 registry.addEndpoint("/MyAppName/api/gs-guide-websocket")使用路径 /MyAppName/MyAppName/api/gs-guide-websocket 注册端点

关于spring - SockJS 无法创建/连接到 Spring WebSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com