2016-04-06 282 views
4

大家好,我试图将消息发送到端点踩不发送消息,但any.I现在用弹簧启动与STOMP以下我没有得到是我班SimpMessagingTemplate春季启动

@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"; 

} 

和我的WebSocket的配置

@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

你是怎么实现这个扎希德的?米卡住在同一问题上,我不明白答案 –

回答

4

SimpMessagingTemplate豆正是对于券商的部分(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来处理内部邮件,你应该使用clientInboundChannel直接,这是由SimpAnnotationMethodMessageHandler提供:

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

我想你可以创建自己的SimpMessagingTemplate实例clientInboundChannel,类似于brokerMessagingTemplate bean。你会没事的。

+0

谢谢,我想我现在知道我在哪里犯错。 –

+0

阿尔乔姆我没明白你的答案,你能指导我吗? –

+0

从您指导的位置不确定,但我认为从参考手册开始确实会更好:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html 。一切休息都应该在单独的SO线程中解决。 –