我正在使用 Spring Boot 和 Tomcat 7 使用 STOMP 和 sockJS 创建一个带有 Websockets 的 Web 应用程序。以下是我的请求映射的类:
@Controller
public class HelloController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String index() {
return "index";
}
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
return new Greeting("Hello, " + message.getName() + "!");
}
}
不幸的是,我在网页上的控制台中收到以下错误:
Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined
Opening Web Socket...
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/WarTest/hello/info
Whoops! Lost connection to undefined
你好是我的大部分代码包,但我不确定导致错误的“信息”目录是什么。有没有人可以帮助我?
编辑:这是我的 Javascript 客户端代码:
function connect() {
var socket = new SockJS('/WarTest/hello');
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);
});
});
}
function disconnect() {
stompClient.disconnect();
setConnected(false);
console.log("Disconnected");
}
function sendName() {
var name = document.getElementById('name').value;
stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
}
function showGreeting(message) {
var response = document.getElementById('response');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
response.appendChild(p);
还有我的 websocketconfig:
@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("/hello").withSockJS();
}
}
编辑:
WebInitializer.java
public class WebInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
编辑2:
WebsocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.setApplicationDestinationPrefixes("/app").enableSimpleBroker("/queue","/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}
编辑3:
当我使用 IDE 直接启动它时,我得到了这个SockJS('http://localhost:8080/WarTest/hello'):
Opening Web Socket... stomp.js:130
Web Socket Opened... stomp.js:130
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.js:130
<<< CONNECTED
heart-beat:0,0
version:1.1
stomp.js:130
connected to server undefined stomp.js:130
Connected: CONNECTED
version:1.1
heart-beat:0,0
(index):23
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings
但是,当我作为 WAR 部署到 Tomcat 时,我收到了找不到 URL/信息的错误。