gpt4 book ai didi

spring - 为什么 Spring SendToUser 不起作用

转载 作者:行者123 更新时间:2023-12-02 06:12:22 25 4
gpt4 key购买 nike

我只是按照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 :

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

最佳答案

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

关于spring - 为什么 Spring SendToUser 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27013159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com