2016-03-28 29 views
0

我跟着例子https://spring.io/guides/gs/messaging-stomp-websocket/發送郵件使用@UserDestination

@Configuration 
@EnableWebSocketMessageBroker 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { 

@Override 
public void configureMessageBroker(MessageBrokerRegistry config) { 
    config.enableSimpleBroker("/topic"); 
    config.setApplicationDestinationPrefixes("/app"); 
} 

@Override 
public void registerStompEndpoints(StompEndpointRegistry registry) { 
    registry.addEndpoint("/hello").withSockJS(); 
} 

} 

我嘗試添加動態終結點前到的WebSocket跺腳端點。

想知道如何實現這一點?

到目前爲止,在我看到的例子中,端點是靜態的。

我希望甚至有選擇地分離端點。

回答

0

我讀到@UserDestination

,並改變了我的@MessageMapping方法

@MessageMapping("/getfeeds") 
public void subscribeToFeeds(Principal principal) throws Exception { 
    String reply = "hello " + principal.getName(); 
    System.out.println("sending " + reply); 
    simpMessagingTemplate.convertAndSendToUser(principal.getName(), "/queue/getfeeds", reply); 
} 

在我的javascript, 我做了以下內容:

function connect() { 
     var socket = new SockJS('/getfeeds'); 
     stompClient = Stomp.over(socket); 
     stompClient.connect('', '', function(frame1) { 
      setConnected(true); 
      console.log('Connected: ' + frame1);    
       // connect callback 
       // subscribe to 
       stompClient.subscribe('/user/getfeeds', function(name) { 
        var msg = JSON.parse(name.body); 
        showGreeting(name); 
       }); 
     }); 
    } 

我也呼籲stompClient。發送( 「/應用程序/ getfeeds」);

我不清楚如何在客戶端使用用戶目的地訂閱。任何意見或例子將不勝感激。謝謝

+0

啊。我發現了我的錯誤。這些是更新。 1)convertAndSendToUser(principal.getName,「/ queue/messages」,reply)。 2)stompClient.subscribe('/ user/queues/messages',...) – Kans