2016-04-06 16 views
4

こんにちはでメッセージを送信していないが、私のクラスのWebSocket用SimpMessagingTemplate私はストンプエンドポイントにメッセージを送信しようとしていますが、私はany.Iを得ていないすべては、以下のストンプで春ブーツを使用しています春ブーツ

@Controller 
public class GreetingController { 

    @MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public Greeting greeting(HelloMessage message) throws Exception { 
    System.out.println(message.getName()); 
    Thread.sleep(13000); // simulated delay 
    return new Greeting("Hello, " + message.getName() + "!"); 
    } 

} 

@Controller             
public class Testcont { 

    @Autowired 
    private SimpMessagingTemplate messageSender; 

    @RequestMapping(value="/Users/get",method=RequestMethod.POST) 
    @ResponseBody 
    public String getUser(@RequestParam(value = "userId") String userId, @RequestParam(value = "password") String password, @RequestParam(value="port") String port, HttpServletRequest request) { 
    HelloMessage mess=new HelloMessage(); 
    mess.setName(userId); 
    messageSender.convertAndSend("/app/hello",mess); 
    return "Success"; 

} 

と私の設定です

@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(); 
    } 

} 

上記のコードは、Webブラウザでうまく機能しています。

+0

どのようにしてこのZahidを達成しましたか?私は同じ問題で立ち往生して答えを理解できなかった –

答えて

4

SimpMessagingTemplate Beanはまさにブローカー部分(AbstractMessageBrokerConfiguration)のためである:

@Bean 
public SimpMessagingTemplate brokerMessagingTemplate() { 
    SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel()); 
    String prefix = getBrokerRegistry().getUserDestinationPrefix(); 
    if (prefix != null) { 
     template.setUserDestinationPrefix(prefix); 
    } 

あなたがいないブローカー先(あなたのケースで/app/)にメッセージを送信するので、そのようなメッセージは、ちょうどAbstractBrokerMessageHandler.checkDestinationPrefix(destination)によって無視されます。

あなたが同じ@MessageMappingすることにより、その内部のメッセージを処理したい場合は、あなたがSimpAnnotationMethodMessageHandlerによって供給されている直接clientInboundChannel、使用する必要があります:私はあなたがあなた自身のSimpMessagingTemplateインスタンスを作成することができますね

@Bean 
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() { 
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler(); 
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes()); 

clientInboundChannel、これはbrokerMessagingTemplate beanに似ています。そしてあなたは大丈夫でしょう。

+0

私は今どこで間違いをしているのか分かりました。 –

+0

アルテム私はあなたの答えを理解していない、私を導くことができますか? –

+0

どこから来たのか分かりませんが、リファレンスマニュアルから実際に始める方がよいと思います。http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html 。残りはすべて別のSOスレッドで処理する必要があります。 –

関連する問題