【问题标题】:Why Spring SendToUser does not work为什么 Spring SendToUser 不起作用
【发布时间】:2015-01-16 18:05:19
【问题描述】:

我只是按照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();
    }

    }

这是弹簧配置:

<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,完全无法向浏览器响应消息。

【问题讨论】:

    标签: spring websocket stomp sockjs


    【解决方案1】:

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

    new SockJS('reqsample')
    

    确保此 URL 有效。我看到您对stomp-endpoint path="reqsample" 使用相同的方法。但不要忘记web.xml 中有一个DispatcherServlet 映射。

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

    http://localhost:8080/reqsample
    

    如果您的DispatcherServlet 映射到/

    当您确定 STOMP 端点的正确 URL 后,您可以使用该 JSP 的相对路径。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 天啊!它只需将 / 添加到前缀用户,然后 /user 就可以了。非常感谢,为我节省了很多时间
      【解决方案3】:

      当你在 Javascript 中订阅频道时:

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

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

      也看看这个话题: Sending message to specific user on Spring Websocket

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-27
        • 2015-06-22
        • 2021-07-07
        • 2016-01-01
        • 2019-07-03
        • 2015-11-15
        • 2020-02-29
        相关资源
        最近更新 更多