跳到主要内容
版本:7.0.3

点作为分隔符

Hunyuan 7b 中英对照 Dots as Separators

当消息被路由到@MessageMapping方法时,它们会与AntPathMatcher进行匹配。默认情况下,模式预期使用斜杠(/)作为分隔符。这是Web应用程序中的良好惯例,类似于HTTP URL。然而,如果你更习惯于消息传递的惯例,你可以改用点(.)作为分隔符。

以下示例展示了如何操作:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

// ...

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

之后,控制器可以在@MessageMapping方法中使用点(.)作为分隔符,如下例所示:

@Controller
@MessageMapping("red")
public class RedController {

@MessageMapping("blue.{green}")
public void handleGreen(@DestinationVariable String green) {
// ...
}
}

客户端现在可以向 /app/red.blue.green123 发送消息。

在前面的例子中,我们没有更改“broker relay”的前缀,因为这些前缀完全取决于外部消息代理(external message broker)。请参阅你所使用的代理的STOMP文档页面,以了解它对目标头部(destination header)支持哪些约定(conventions)。

另一方面,“简单代理”确实依赖于配置的PathMatcher,因此,如果您更改了分隔符,这一变化也会影响代理以及代理从消息中匹配订阅模式中目标地址的方式。