Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何使用SpringWebSockets向特定用户发送消息?_Spring Mvc_Stomp_Spring Websocket_Sockjs - Fatal编程技术网

Spring mvc 如何使用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

我无法使用SpringWebSockets将消息发送给特定用户。这是我的代码。在这里,当我过去发送消息时,我得到的是
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)