0
我無法使用Spring web sockets將消息發送給特定用戶,這裏是我的代碼。在這裏,當我用來發送消息,然後我越來越org.springframework.messaging.simp.annotation.support.MissingSessionUserException: No "user" header in message
如何使用Spring Web Sockets向特定用戶發送消息?
請告訴我如何避免這種情況。提前致謝。
Controller.java
@MessageMapping("/sendMessage")
public void getMessage(String message,Principal principal){
this.template.convertAndSendToUser(principal.getName(),"/topic/messages",message);
}
的index.jsp
function connect() {
var socket = new SockJS('/SpringWebSocket/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/user/queue/messages', function(messageOutput) {
alert("respose");
});
});
}
function sendMessage() {
var from = document.getElementById('from').value;
var text = document.getElementById('text').value;
stompClient.send("/app/sendMessage",
{}, JSON.stringify({
'from' : from,
'text' : text,
}));
}
爲spring-servlet.xml:
<context:component-scan base-package="com.test"/>
<mvc:annotation-driven></mvc:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<websocket:message-broker application-destination-prefix="/app" >
<websocket:stomp-endpoint path="/chat">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /queue"/>
</websocket:message-broker>