为什么Spring的SendToUser无法工作

3

我只是按照Spring的指示,向特定用户发送响应,而不是广播消息。但最终,无法发送任何响应消息。

这是我的js代码:

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('reqsample');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('user/queue/resp', function(resp) {
            var body = JSON.parse(resp.body);
            showResp('cmid:' + body.cmid + ',reqName:' + body.reqName
                    + ',custNo:' + body.custNo + ',qty:' + body.quantity);
        });
        stompClient.subscribe('user/queue/errors', function(resp) {
            var body = JSON.parse(resp.body);
            showResp(body);
        });
    });
}
function disconnect() {
    stompClient.disconnect();
    setConnected(false);
    console.log("Disconnected");
}
function sendName() {
    var name = document.getElementById('name').value;
    stompClient.send("/app/req", {}, JSON.stringify({
        'name' : name
    }));
}

这是控制器:


@Controller
public class MessageController {
    @MessageMapping("/req")
    @SendToUser("/queue/resp")
    public RespMessage greeting(ReqMessage message, Principal pc)
        throws Exception {
    System.out.println("---- received message: " + message == null ? "NULL"
            : message.getName());
    System.out.println("---- received user info: " + pc.getName());
    RespMessage rm = new RespMessage();
    rm.setCmid("CM_01");
    rm.setCustNo("Cust_02");
    rm.setQuantity("1000");
    rm.setReqName(message.getName());
    return rm;

    }

    @MessageExceptionHandler
    @SendToUser("/queues/errors")
    public String handleException(Exception ex) {
        System.out.println(ex);
        return ex.getMessage();
    }

    }

这是Spring配置:

<context:component-scan base-package="wx.poc7" />
<mvc:annotation-driven />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/js/**" location="/js/" />
<websocket:message-broker
    application-destination-prefix="/app" user-destination-prefix="/user">
    <websocket:stomp-endpoint path="reqsample">
        <websocket:sockjs />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/queue, /topic" />
 </websocket:message-broker>

请帮忙,谢谢。

我尝试使用 @SendToUser、@SendToUser("/queue/resp") 和 SimpMessagingTemplate,但是完全无法向浏览器响应消息。

3个回答

8
用户目的地前缀为/user,但你的订阅目的地似乎缺少/。将user/queue/resp更改为/user/queue/resp以在客户端上接收消息。

哦,我的天啊!它起作用了,只需在前缀用户之前添加 / ,然后 /user 就可以工作了。非常感谢,节省了我很多时间。 - andy wang

0

当你在JavaScript中订阅频道时:

stompClient.subscribe('user/queue/errors', function(resp) {
            var body = JSON.parse(resp.body);
            showResp(body);
        });

正确的路径是'user/' +用户名+ 'queue/errors'

也可以查看这个主题: {{link1:在Spring Websocket上向特定用户发送消息}}


0

你的WebSocket配置看起来不错,但我认为问题在于正确的connect URL:

new SockJS('reqsample')

请确保此URL有效。我看到您在stomp-endpoint path="reqsample"中使用相同的URL。但请不要忘记在web.xml中进行了DispatcherServlet映射。

尝试使用包括主机和端口的SockJS构造函数中的full URL。像这样:

http://localhost:8080/reqsample

如果您的DispatcherServlet映射到/
当您找出正确的STOMP端点URL时,可以从该JSP的相对路径进行测试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接