2016-06-29 4 views
0

내 서비스 클래스 중 하나에서 작업이 수행 될 때 특정 레코드의 websocket 구독자에게 메시지를 보내려고합니다.Spring이 Websocket Message Broker에 메시지를 보냅니다.

나는 Spring Websocket documentation을 읽으려고하고 있지만이 모든 것들을 함께 작동시키는 방법에 대해서는 모호합니다.

WebsocketConfiguration.java

@Override 
    public void configureMessageBroker(MessageBrokerRegistry config) { 
     config.enableStompBrokerRelay("/queue/", "/topic/", "/exchange/"); 
     config.setApplicationDestinationPrefixes("/app"); 
     config.setPathMatcher(new AntPathMatcher(".")); 
    } 

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

WebsocketSecurity.java

@Override 
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { 
    messages 
     // message types other than MESSAGE and SUBSCRIBE 
     .nullDestMatcher().authenticated() 
     // matches any destination that starts with /rooms/ 
     .simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN) 
     .simpDestMatchers("/topic/**").authenticated() 
     // (i.e. cannot send messages directly to /topic/, /queue/) 
     // (i.e. cannot subscribe to /topic/messages/* to get messages sent to 
     // /topic/messages-user<id>) 
     .simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll() 
     // catch all 
     .anyMessage().denyAll(); 
} 

컨트롤러 클래스 (단순 브로커를 구현에서 시도 : 여기

내 설치 파일 (이 BTW jHipster을 확장하고)있다 sockjs를 구독하고 응용 프로그램의 다른 곳에서 생성 된 메시지를 수신하는 방법을 테스트 할 수 있습니다.

@MessageMapping("/ws") 
@SendTo("/topic/sendactivity.{id}") 
public void activity(@DestinationVariable string id, @Payload String message){ 
    log.debug("Sending command center: "+message); 
} 

@RequestMapping(value = "/updateactivity", method = RequestMethod.PUT) 
public ResponseEntity<Membership> updateMembership(
     @RequestBody Membership membership) throws URISyntaxException { 
    // ... 
    String testString = "test"; 
    messagingTemplate.convertAndSend("/topic/commandcenter"+membership.getId().toString(), testString); 
    // ... 
} 

public void activity 메서드에 중단 점을 적용해도 아무 것도 얻을 수 없습니까?

+0

과 같은 몇 가지 예에서 sockJS 클라이언트 로그를 공유 할 수 있습니까? 어떤 메시지가 전송되었고 어떤 응답을 받았는지 확인할 방법이 없습니다. 또한 org.springframework.web.socket'을 DEBUG에 넣고 관련 로그도 공유 할 수 있습니까? –

+0

@BrianClozel - 아직 채널을 구독하지 않았습니다. 활성화되지 않았습니까?! 나는'convertAndSend'를 실행 한 후에'void activity' 메서드에서 중단 점에 도달해야한다고 생각했습니다. –

답변

0

메시징 템플릿을 사용하여 "/topic/commandcenterID"에게 메시지를 보내면 해당 메시지를 메시지 브로커로 보내고 메시지 브로커는 해당 메시지를 해당 주제에 가입 한 클라이언트로 보냅니다. 그래서 그것은 당신의 활동 방식을 통해 흐르지 않을 것입니다.

@MessageMapping 주석이 달린 메서드를 사용하는 경우이를 응용 프로그램 대상으로 선언합니다. 그래서 메시지를 "/app/ws에 보내야합니다. 그 경우에는 메서드 인수로 예상되는 대상 변수가 @MessageMapping 주석의 경로 정의에서 누락 되었기 때문에 작동하지 않을 수 있습니다. 또한, . 사실에서 @SendTo 주석은 당신이 여기에 물건을 혼합있어 보인다

방법에 의해 반환되는 값은 메시지로 변환하고, 지정된 목적지로 전송되어야한다고 봄을 알려줍니다, 그리고 당신이해야한다고 생각 :

관련 문제