我正在尝试实现这个websocket示例,但目前请求没有被路由到我的Spring控制器。
http://spring.io/guides/gs/messaging-stomp-websocket/下面是我的WebSocketConfig类:package com.kinnect.mobile.gateway.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@ComponentScan(basePackages = {"com.kinnect.mobile.gateway.controller"})
@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();
}
}
这是我的控制器:
package com.kinnect.mobile.gateway.controller;
import hello.Greeting;
import hello.HelloMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
System.out.println("Test...");
return new Greeting("Hello, " + message.getName() + "!");
}
}
下面是我用来调用控制器的JavaScript:
var stompClient = null;
function setConnected(connected) {
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
document.getElementById('response').innerHTML = '';
}
function connect() {
var socket = new SockJS('http://localhost:8080/KinnectMobileGateway/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('http://localhost:8080/KinnectMobileGateway/topic/greetings', function(greeting){
console.log("Greeting received!");
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("http://localhost:8080/KinnectMobileGateway/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);
}
我能够建立套接字连接,当我尝试建立套接字连接时,我可以在服务器的控制台中看到这一点,但是System.out.println("Test")没有被触发:
16:13:47.044 [http-bio-8080-exec-2] DEBUG o.s.m.simp.stomp.StompDecoder - Decoded [Payload byte[14]][Headers={stompCommand=SEND, nativeHeaders={content-length=[14], destination=[http://localhost:8080/KinnectMobileGateway/app/hello]}, simpMessageType=MESSAGE, simpDestination=http://localhost:8080/KinnectMobileGateway/app/hello, id=b49ca021-a789-9fdc-66cd-413d7549fb90, timestamp=1400573627044}]
我认为配置是错误的,我的控制器类在启动时没有注册,但我不确定如何。请帮助!
试试这个:
stompClient.subscribe('/topic/greetings'
...
stompClient.send("/app/hello"
您应该使用relative paths
作为您的逻辑。StackTrace显示你更正了info:
simpDestination=http://localhost:8080/KinnectMobileGateway/app/hello
但是你的目的地是/app/hello