Spring Boot と Tomcat 7 を使用して、STOMP と sockJS を使用して Websocket で 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() + "!");
}
}
残念ながら、Web ページのコンソールに次のエラーが表示されます。
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
hello はほとんどのコードを含む私のパッケージですが、エラーの原因となっている「info」ディレクトリが何であるかわかりません。私を助けてくれる人はいますか?
編集:これが私の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);
}
}
EDIT2:
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();
}
}
EDIT3:
を使用して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/情報が見つからないというエラーが発生します。