Spring WebSockets @SendTo映射中的路径变量
我认为,我有一个非常简单的Spring WebSocket应用程序。但是,我正在尝试将路径变量用于订阅以及消息映射。
我在下面发布了一个转述的例子。我希望注释根据他们的.即,一个 to 应该通知订阅者,但我没有看到这种行为。@SendTo
fleetId
POST
/fleet/MyFleet/driver/MyDriver
/fleet/MyFleet
值得注意的是,订阅文字是有效的。这是故意的吗?我是否缺少某些配置?或者这不是它的工作方式吗?/fleet/{fleetId}
我对WebSockets或这个春季项目还不是很熟悉,所以提前感谢。
控制器.java
...
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")
@SendTo("/topic/fleet/{fleetId}")
public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) {
return new Simple(fleetId, driverId);
}
...
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/live");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/fleet").withSockJS();
}
}
索引.html
var socket = new SockJS('/fleet');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
// Doesn't Work
stompClient.subscribe('/topic/fleet/MyFleet', function(greeting) {
// Works
stompClient.subscribe('/topic/fleet/{fleetId}', function(greeting) {
// Do some stuff
});
});
发送样品
stompClient.send("/live/fleet/MyFleet/driver/MyDriver", {}, JSON.stringify({
// Some simple content
}));