我们知道我们可以使用他订阅的主题前缀从单足服务器向客户端发送消息
/topic/hello。我们也知道我们可以将消息发送给特定的用户,因为spring提供了
convertAndSendToUser(username,destination,message)API。它接受一个字符串用户名,这意味着如果我们为每个连接都拥有唯一的用户名,则我们应该能够向订阅主题的特定用户发送消息。
鲜为人知的是,该用户名来自何处?
该用户名是java.security.Principal
界面的一部分。每个
StompHeaderAccessor或
WebSocketSession对象有此主体的情况下,你可以从它那里得到的用户名。但是,根据我的实验,它不是自动生成的。
它必须由服务器为每个会话手动生成。
要首先使用此接口,您需要实现它。
class StompPrincipal implements Principal { String name StompPrincipal(String name) { this.name = name } @Override String getName() { return name }}然后,可以
StompPrincipal通过覆盖DefaultHandshakeHandler为每个连接生成唯一的。您可以使用任何逻辑来生成用户名。这是一种使用UUID的潜在逻辑:
class CustomHandshakeHandler extends DefaultHandshakeHandler { // Custom class for storing principal @Override protected Principal determineUser( ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes ) { // Generate principal with UUID as name return new StompPrincipal(UUID.randomUUID().toString()) }}最后,您需要配置websocket以使用自定义的握手处理程序。
@Overridevoid registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry .addEndpoint("/stomp") // Set websocket endpoint to connect to .setHandshakeHandler(new CustomHandshakeHandler()) // Set custom handshake handler .withSockJS() // Add Sock JS support}而已。现在,您的服务器已配置为为每个连接生成唯一的主体名称。它将把该主体作为
StompHeaderAccessor您可以通过连接事件侦听器,MessageMapping函数等访问的对象的一部分进行传递。
来自事件监听器:
@EventListenervoid handleSessionConnectedEvent(SessionConnectedEvent event) { // Get Accessor StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage())}从消息映射的API
@MessageMapping('/hello')protected void hello(SimpMessageHeaderAccessor sha, Map message) { // sha available in params}关于使用的最后一点说明
convertAndSendToUser(...)。向用户发送消息时,您将使用类似这样的内容
convertAndSendToUser(sha.session.principal.name, '/topic/hello', message)
但是,要订阅客户端,您将使用
client.subscribe('/user/topic/hello', callback)如果您订阅客户端,
/topic/hello您将只收到广播的消息。



