如何使用Sping Websocket在特定的userName上订阅+从方法中获取通知,分别来自BOTH @RequestMapping和@MessageMapping?
我正在实施通知系统。 并希望在用户登录时初始化Socket连接,并向他显示他的通知,以及是否发生某些事件。
以下是我项目的代码片段
web_socket.js
var stompClient = null; function connect( temp, context_path ) { var socket = new SockJS( "/ContextPath/websock?nsec="+temp ); stompClient = Stomp.over(socket); console.log( stompClient ); stompClient.connect({userName: "123456"}, function( frame ){ console.log( "Connected :- "+frame ); stompClient.subscribe("/user/queue/notifications", function( notifications ) { notifications_display( notifications.body ); }); stompClient.subscribe("/user/queue/errors", function( error ) { console.log( "Error :- "+error ); }); getNotifications(); }, function( error ) { console.log( "STOMP Error :-"+error ); }); } function getNotifications() { // alert("getNotifications()"); console.log( stompClient ); stompClient.send("/app/hello", {}, ""); }
WebSocketConfig.java
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint("/websock").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry config) { // TODO Auto-generated method stub config.enableSimpleBroker("/topic","/queue"); config.setApplicationDestinationPrefixes("/app"); } }
WebSocketController.java
@Controller public class WebSocketController { @MessageMapping(value="/hello") @SendToUser("/queue/notifications") public List hello( @Headers Map map ) { System.out.println("WebSocketController.hello()"); for( Map.Entry entry : map.entrySet() ) System.out.println(entry.getKey()+" : "+entry.getValue()); List notifies = new ArrayList(); Notify notify = new Notify(); notify.setMessage("Hello World !!!"); notifies.add( notify ); notify = new Notify(); notify.setMessage("How are you !!!"); notifies.add( notify ); return notifies; } @MessageExceptionHandler @SendToUser("/queue/errors") public String handleException( Throwable exception ) { return exception.getMessage(); } }
当用户登录用户收到通知时,上面的代码工作正常。
现在我想做的是,从我的另一个控制器中的其他地方发送用@RequsetMapping
注释注释的方法的用户特定通知。
为此,我可以使用的变体
SimpMessagingTemplate.convertAndSendToUser(String user, String destination, Object payload)
例如我的Annotated方法
@RequestMapping(value="/saveOrUpdateAssignments") public String saveOrUpdateAssignments ( @ModelAttribute Assignment assignments, RedirectAttributes redirectAttributes ) { boolean isSuccess = this.masterService.saveOrUpdateAssignments( allCode ); if( isSuccess ) simpMessagingTemplate.convertAndSendToUser(String user, String destination, Object payload) return "redirect:/master/loadAssignmentForm"; }
在这个方法中我知道user
。
现在的问题是
如何使用特定userName(userId)上的Sping Websocket集成进行订阅?
我参考了https://stackoverflow.com/a/22367824/3425489 。
如果我改变了
stompClient.subscribe("/user/queue/notifications", function( notifications ) { notifications_display( notifications.body ); });
至
stompClient.subscribe("/user/"+userName+"/queue/notifications", function( notifications ) { notifications_display( notifications.body ); });
我用@MessageMapping(value="/hello")
和** @SendToUser("/queue/notifications")
注释的方法不起作用……
谢谢