Spring mvc 如何使用SpringWebSockets向特定用户发送消息?
我无法使用SpringWebSockets将消息发送给特定用户。这是我的代码。在这里,当我过去发送消息时,我得到的是Spring mvc 如何使用SpringWebSockets向特定用户发送消息?,spring-mvc,stomp,spring-websocket,sockjs,Spring Mvc,Stomp,Spring Websocket,Sockjs,我无法使用SpringWebSockets将消息发送给特定用户。这是我的代码。在这里,当我过去发送消息时,我得到的是org.springframework.messaging.simp.annotation.support.MissingSessionUserException:消息中没有“user”标题 请告诉我如何避免这种情况。提前谢谢 Controller.java @MessageMapping("/sendMessage") public void getMessage(String
org.springframework.messaging.simp.annotation.support.MissingSessionUserException:消息中没有“user”标题
请告诉我如何避免这种情况。提前谢谢
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>
而不是
this.template.convertAndSendToUser(principal.getName(),“/topic/messages”,message)代码>
使用以下this.template.convertAndSendToUser(principal.getName(),“/queue/messages”,message)代码>