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