标记在方法上,用于将客户端以 “/app” 开头的订阅路由到对应的方法上。默认情况下,返回值会通过clientOutboundChannel直接返回(发送)给客户端,不经过broker,是一次性的。用户可以通过 @SendTo 或 @SendToUser 去修改这一行为,也就是将返回值发送个broker,然后广播出去。
Method argument | Description |
---|---|
Message | For access to the complete message. |
MessageHeaders | For access to the headers within the Message. |
MessageHeaderAccessor, SimpMessageHeaderAccessor, and StompHeaderAccessor | For access to the headers through typed accessor methods. |
@Payload | For access to the payload of the message, converted (for example, from JSON) by a configured MessageConverter. The presence of this annotation is not required since it is, by default, assumed if no other argument is matched. You can annotate payload arguments with @javax.validation.Valid or Spring’s @Validated, to have the payload arguments be automatically validated. |
@Header | For access to a specific header value — along with type conversion using an org.springframework.core.convert.converter.Converter, if necessary. |
@Headers | For access to all headers in the message. This argument must be assignable to java.util.Map. |
@DestinationVariable | For access to template variables extracted from the message destination. Values are converted to the declared method argument type as necessary. |
java.security.Principal | Reflects the user logged in at the time of the WebSocket HTTP handshake. |
一般用于初始化数据。如登入聊天室后,初始化在线人员列表和历史消息等。不是真正的订阅。
registry.enableStompBrokerRelay("/topic");
registry.setApplicationDestinationPrefixes("/app");
@SubscribeMapping("/chatroom")
public List<String> chatInit(){
List<String> onlineList = new ArrayList<>();
onlineList.add("Kleven");
onlineList.add("Dean");
return onlineList;
}
stompClient.subscribe('/app/chatroom', (msg) => {
// client logic
});
>>> SUBSCRIBE
id:sub-1
destination:/app/chatroom
<<< MESSAGE
destination:/app/chatroom
content-type:application/json
subscription:sub-1
message-id:e1vkwn0p-0
content-length:17
["Kleven","Dean"]