I have, what I think to be, a very simple Spring WebSocket application. However, I'm trying to use path variables for the subscription as well as the message mapping.
我有,我認為是一個非常簡單的Spring WebSocket應用程序。但是,我正在嘗試使用路徑變量進行訂閱以及消息映射。
I've posted a paraphrased example below. I would expect the @SendTo
annotation to return back to the subscribers based on their fleetId
. ie, a POST
to /fleet/MyFleet/driver/MyDriver
should notify subscribers of /fleet/MyFleet
, but I'm not seeing this behavior.
我在下面發布了一個釋義的例子。我希望@SendTo注釋能夠根據他們的fleetId返回給訂閱者。即,對/ fleet / MyFleet / driver / MyDriver的POST應該通知/ fleet / MyFleet的訂閱者,但我沒有看到這種行為。
It's worth noting that subscribing to literal /fleet/{fleetId}
works. Is this intended? Am I missing some piece of configuration? Or is this just not how it works?
值得注意的是訂閱literal / fleet / {fleetId}是有效的。這是有意的嗎?我錯過了一些配置嗎?或者這不是它的工作原理嗎?
I'm not very familiar with WebSockets or this Spring project yet, so thanks in advance.
我對WebSockets或Spring項目不是很熟悉,所以提前感謝。
Controller.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();
}
}
index.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
});
});
Send Sample
stompClient.send("/live/fleet/MyFleet/driver/MyDriver", {}, JSON.stringify({
// Some simple content
}));
76
Even though @MessageMapping
supports placeholders, they are not exposed / resolved in @SendTo
destinations. Currently, there's no way to define dynamic destinations with the @SendTo
annotation (see issue SPR-12170). You could use the SimpMessagingTemplate
for the time being (that's how it works internally anyway). Here's how you would do it:
即使@MessageMapping支持占位符,它們也不會在@SendTo目標中公開/解析。目前,無法使用@SendTo注釋定義動態目標(請參閱問題SPR-12170)。你可以暫時使用SimpMessagingTemplate(無論如何它都是內部工作的)。這是你如何做到這一點:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")
public void simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) {
simpMessagingTemplate.convertAndSend("/topic/fleet/" + fleetId, new Simple(fleetId, driverId));
}
In your code, the destination '/topic/fleet/{fleetId}' is treated as a literal, that's the reason why subscribing to it works, just because you are sending to the exact same destination.
在您的代碼中,目標'/ topic / fleet / {fleetId}'被視為文字,這就是為什么訂閱它的原因,只是因為您發送到完全相同的目的地。
If you just want to send some initial user specific data, you could return it directly in the subscription:
如果您只想發送一些初始用戶特定數據,可以直接在訂閱中返回:
@SubscribeMapping("/fleet/{fleetId}/driver/{driverId}")
public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) {
return new Simple(fleetId, driverId);
}
Update: In Spring 4.2, destination variable placeholders are supported it's now possible to do something like:
更新:在Spring 4.2中,支持目標變量占位符,現在可以執行以下操作:
@MessageMapping("/fleet/{fleetId}/driver/{driverId}")
@SendTo("/topic/fleet/{fleetId}")
public Simple simple(@DestinationVariable String fleetId, @DestinationVariable String driverId) {
return new Simple(fleetId, driverId);
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/11/20/9e9313ed19d584635af4d2069c0df4ed.html。