2015-04-22 20 views
3

我想在我的項目中使用RabbitMQ作爲代理,我想在客戶端打開套接字時分配目標隊列。 事情是這樣的:http://i.imgur.com/ldB2M0m.pngRabbitMQ的自定義目的地

我設法與SimpleBroker做到這一點,但是當我嘗試用StompBrokerRelay我不能assing上RabbitMQ的隊列中來做到這一點,我停止客戶端(http://i.imgur.com/gNaRHCQ.png)上接收消息。

這是我正在做它:

控制器:

@RestController 
public class FeedController { 

@Autowired 
private SimpMessageSendingOperations template; 

    @RequestMapping(value = "/feed", method = RequestMethod.POST, consumes = "application/json") 
    public Reference getLeankrReference(@RequestBody Reference ref) 
    {  
     this.template.convertAndSendToUser(ref.getChannelId(), "/topic/feed", ref); 
     return ref; 
    } 
} 

WebSocket的配置:

@Configuration 
@EnableWebSocketMessageBroker 
@EnableScheduling 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { 

    @Override 
    public void configureMessageBroker(MessageBrokerRegistry config) 
    { 
     config.enableStompBrokerRelay("/topic/") 
      .setAutoStartup(true); 

     //config.enableSimpleBroker("/user/"); 
     config.setApplicationDestinationPrefixes("/app"); 
    } 

    public void registerStompEndpoints(StompEndpointRegistry registry) { 
     registry.addEndpoint("/vision").withSockJS(); 
    } 
} 

客戶:

 function connect() { 
     var socket = new SockJS('/ws/vision'); 
     var channel = document.getElementById('name').value; 
     stompClient = Stomp.over(socket); 

     stompClient.connect({}, function(frame) { 
      setConnected(true); 
      console.log('Connected: ' + frame); 
      stompClient.subscribe('/user/' + channel + '/feed', function(message) { 
       showContent(JSON.parse(message.body)); 
      }); 
     }); 
    } 

我知道我錯過了一些東西。也許一些經紀人配置?

預先感謝您!

+1

你有沒有錯過配置'/隊列/'旁邊'/ topic /'? –

+0

@ArtemBilan我也試過'/ queue /'。它幫助我訂閱RabbitMQ上的隊列。但是,當我嘗試在客戶端上發佈某些內容時,它不會收到任何消息。 –

回答

0

最後,我想清楚我錯過了什麼!

WebSocket的配置:

我只是分配話題隊列。在這種情況下,我還需要隊列隊列,一旦我想將其分配給特定的用戶/頻道。

config.enableStompBrokerRelay("/queue/", "/topic/"); 

客戶:

我不是指我想用隊列的類型。

stompClient.subscribe('/user/queue/feed', function(content) {

但這是不夠的。它缺少正確的安全配置。

這樣的事情,

安全配置:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .csrf().disable() 
     .headers().addHeaderWriter(
      new XFrameOptionsHeaderWriter(
        XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN)).and() 
     .formLogin() 
      .defaultSuccessUrl("/index.html") 
      .loginPage("/login.html") 
      .failureUrl("/login.html?error") 
      .permitAll() 
      .and() 
     .logout() 
      .logoutSuccessUrl("/login.html?logout") 
      .logoutUrl("/logout.html") 
      .permitAll() 
      .and() 
     .authorizeRequests() 
      .antMatchers("/**").permitAll() 
      .anyRequest().authenticated() 
      .and(); 
} 

@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .inMemoryAuthentication() 
      .withUser("channel1").password("password").roles("USER"); 
} 

隨着我增加了一個登錄頁面。這是沒有必要的。您只需確保使用密碼參數進行身份驗證。

現在,兔子知道用戶/頻道,它可以發送隊列到特定的目的地。