如何在Spring中配置Controller以接收Websocket消息?

时间:2019-06-13 16:53:34

标签: spring websocket spring-websocket stomp

我的Spring websocket配置文件中包含以下代码。如何告诉MyWebSocketConfig类在ProcessClientMsgController()类中使用GetConfig()处理来自客户端(在/ topic / config通道上)的传入Websocket消息?

谢谢

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", {}, '');

0 个答案:

没有答案
相关问题