Как настроить контроллер для получения сообщений websocket в Spring? - PullRequest
0 голосов
/ 13 июня 2019

У меня есть код ниже в моем конфигурационном файле веб-сокета Spring. Как мне сказать классу MyWebSocketConfig использовать GetConfig () в классе ProcessClientMsgController () для обработки входящих сообщений websocket от клиента (на / topic / config channel)?

Спасибо

package myPackage
import ...

@CompileStatic
@Configuration
@EnableWebSocketMessageBroker
class MyWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    void configureMessageBroker(MessageBrokerRegistry messageBrokerRegistry) {
        messageBrokerRegistry.enableSimpleBroker "/queue", "/topic"
        messageBrokerRegistry.setApplicationDestinationPrefixes "/app"
    }

    @Override
    void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry.addEndpoint("/stomp").setAllowedOrigins("*").withSockJS()
    }

    @Bean
    GrailsSimpAnnotationMethodMessageHandler grailsSimpAnnotationMethodMessageHandler(
        SubscribableChannel clientInboundChannel,
        MessageChannel clientOutboundChannel,
        SimpMessageSendingOperations brokerMessagingTemplate
    ) {
        def handler = new GrailsSimpAnnotationMethodMessageHandler(clientInboundChannel, clientOutboundChannel, brokerMessagingTemplate)
        handler.destinationPrefixes = ["/app"]
        return handler
    }

    @Bean
    GrailsWebSocketAnnotationMethodMessageHandler grailsWebSocketAnnotationMethodMessageHandler(
        SubscribableChannel clientInboundChannel,
        MessageChannel clientOutboundChannel,
        SimpMessageSendingOperations brokerMessagingTemplate
    ) {
        def handler = new GrailsWebSocketAnnotationMethodMessageHandler(clientInboundChannel, clientOutboundChannel, brokerMessagingTemplate)
        handler.destinationPrefixes = ["/app"]
        return handler
    }

}


@Controller
public class ProcessClientMsgController{
    def DBService
    SimpMessagingTemplate myBrokerMsgTemplate

    @MessageMapping("/getconfig")
    def GetConfig() {
        ObjectCommandResponse cR = DBService.getConfig()
          myBrokerMsgTemplate.convertAndSend('/topic/config', cR.configObject)
        }
    }

}

Передняя часть:

this.stompClient.send("/app/getconfig", {}, '');
...