我尝试进行直播竞价。因此,当用户中标时,他是唯一会收到类似“您中标了。已下订单。”之类消息的人
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic","/user","/queue");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
以上是我的MessageBroker的配置。
messagingTemplate.convertAndSendToUser(String.valueOf(winnerId), "/queue/notifications",
new BidMessage("AUCTION_WON", "You won the auction.", productId));
我还设置了一个使用 userId 作为标识符的委托人:
public class CustomHandshakeHandler extends DefaultHandshakeHandler {
@Autowired
private UserClientService userClientService;
@Override
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
// Extract JWT token from request
String jwtToken = extractJwtToken(request);
System.out.println("determining user"+jwtToken);
String userId = userClientService.getUserIdFromToken(jwtToken);
// userId will become the identifier to send personal message
return () -> userId;
}
我的前端监听:/user/queue/notifications:
stompClient.subscribe('/user/queue/notifications', (message) => {
console.log("inside")
const notification = JSON.parse(message.body);
console.log("notification",notification)
if (notification.type === "AUCTION_WON") {
// Handle the notification for the winner
alert(notification.message); // or update a state to show in the UI
}
});
看来使用了userId作为标识符。正如我从检查->网络->WS 中看到的。它显示用户名:3。因此,如果获胜者是 3,则获胜者应该收到通知。但我什至无法打印出“内部”。对我缺少的地方有什么建议吗? :
我终于明白了。我刚刚删除了“/user”。
config.enableSimpleBroker("/topic","/queue")
/用户通道已由 Spring STOMP 管理