Spring 4.2.0 - 如何使用 SimpUserRegistry
Spring 4.2.0 - How to use SimpUserRegistry
根据 Spring 4.2.0 文档的第 5.5 项,我正在尝试使用 SimpUserRegistry 将用户列表连接到 websockets/STOMP 端点...但我很漂亮Spring 上的新功能,我只是不知道 where/how 使用这个 class。你能给我举个例子或给我指明正确的方向吗?
只需注入 SimpUserRegistry
作为依赖。这是打印所有连接用户的用户名的示例:
@Autowired private SimpUserRegistry userRegistry;
public void printConnectedUsers() {
userRegistry.getUsers().stream()
.map(u -> u.getName())
.forEach(System.out::println);
}
刚刚遇到了一个类似的问题 - 我想我会把这些发现留给未来的旅行者。
我试图使用包含上述自动连接 SimpUserRegistry
的 WebSocketSecurityInterceptor
来决定应该发送哪些消息。这涉及在 WebSocketConfig
中设置拦截器;因为我需要 Autowired 字段,所以我不能像平常那样使用拦截器的构造函数。
@Component
public class WebSocketSecurityInterceptor implements ChannelInterceptor {
@Autowired
private SimpUserRegistry simpUserRegistry;
...other stuff
}
和
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Autowired
private WebSocketSecurityInterceptor webSocketSecurityInterceptor;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/stream");
config.configureBrokerChannel().setInterceptors(webSocketSecurityInterceptor);
}
不幸的是,在上面,由于初始化顺序的一些怪癖,当你将 类 自动连接到 WebSocketConfig
时,configureMessageBroker(MessageBrokerRegistry config)
不再是 运行,等等没有添加拦截器。
我们能找到的唯一方法是在应用程序上下文中四处翻找,以获得正确的 bean:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Autowired private ApplicationContext applicationContext;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/stream");
config.configureBrokerChannel().setInterceptors(config.configureBrokerChannel().setInterceptors(applicationContext.getBean(WebSocketSecurityInterceptor.class));
}
根据 Spring 4.2.0 文档的第 5.5 项,我正在尝试使用 SimpUserRegistry 将用户列表连接到 websockets/STOMP 端点...但我很漂亮Spring 上的新功能,我只是不知道 where/how 使用这个 class。你能给我举个例子或给我指明正确的方向吗?
只需注入 SimpUserRegistry
作为依赖。这是打印所有连接用户的用户名的示例:
@Autowired private SimpUserRegistry userRegistry;
public void printConnectedUsers() {
userRegistry.getUsers().stream()
.map(u -> u.getName())
.forEach(System.out::println);
}
刚刚遇到了一个类似的问题 - 我想我会把这些发现留给未来的旅行者。
我试图使用包含上述自动连接 SimpUserRegistry
的 WebSocketSecurityInterceptor
来决定应该发送哪些消息。这涉及在 WebSocketConfig
中设置拦截器;因为我需要 Autowired 字段,所以我不能像平常那样使用拦截器的构造函数。
@Component
public class WebSocketSecurityInterceptor implements ChannelInterceptor {
@Autowired
private SimpUserRegistry simpUserRegistry;
...other stuff
}
和
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Autowired
private WebSocketSecurityInterceptor webSocketSecurityInterceptor;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/stream");
config.configureBrokerChannel().setInterceptors(webSocketSecurityInterceptor);
}
不幸的是,在上面,由于初始化顺序的一些怪癖,当你将 类 自动连接到 WebSocketConfig
时,configureMessageBroker(MessageBrokerRegistry config)
不再是 运行,等等没有添加拦截器。
我们能找到的唯一方法是在应用程序上下文中四处翻找,以获得正确的 bean:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
@Autowired private ApplicationContext applicationContext;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/stream");
config.configureBrokerChannel().setInterceptors(config.configureBrokerChannel().setInterceptors(applicationContext.getBean(WebSocketSecurityInterceptor.class));
}